How to implement the fixed Actionbar below, android version 2.x or higher?

Asked

Viewed 2,188 times

1

I’m implementing Action Bar on android version 2.x or higher, however, I’m having difficulties with the way of displaying the elements. In version 2.x the bar appears below, but without menu names, and in version 3.0 or higher the bar action is aligned at the top. I want the action bar to be aligned at the bottom, with menu names as much in version 2.x as in the others. I am using the Suport class of the action bar import android.support.v7.app.ActionBarActivity

package com.lucas;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // inflate the layout, etc...
        getSupportActionBar().setTitle("Titulo");
        getSupportActionBar().setIcon(R.drawable.icont);
        getSupportActionBar().setSubtitle("Subtitulo");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main2, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        super.onOptionsItemSelected(item);

        switch(item.getItemId()){
            case R.id.phone:
                Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
                break;

            case R.id.computer:
                Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
                break;

            case R.id.gamepad:
                Toast.makeText(getBaseContext(), "You selected Gamepad", Toast.LENGTH_SHORT).show();
                break;

            case R.id.camera:
                Toast.makeText(getBaseContext(), "You selected Camera", Toast.LENGTH_SHORT).show();
                break;

            case R.id.video:
                Toast.makeText(getBaseContext(), "You selected Video", Toast.LENGTH_SHORT).show();
                break;

            case R.id.email:
                Toast.makeText(getBaseContext(), "You selected EMail", Toast.LENGTH_SHORT).show();
                break;
            }
        return true;
    }

}

xml layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Xml menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/phone"
        android:icon="@drawable/phone"
        android:title="@string/phone"
        myapp:showAsAction="ifRoom|withText"/>
    <item
        android:id="@+id/computer"
        android:icon="@drawable/computer"
        android:title="@string/computer"
        myapp:showAsAction="ifRoom|withText"/>
    <item
        android:id="@+id/gamepad"
        android:icon="@drawable/gamepad"
        android:title="@string/gamepad"
        myapp:showAsAction="ifRoom|withText"/>
    <item
        android:id="@+id/camera"
        android:icon="@drawable/camera"
        android:title="@string/camera"
        myapp:showAsAction="ifRoom|withText"/>
    <item
        android:id="@+id/video"
        android:icon="@drawable/video"
        android:title="@string/video"
        myapp:showAsAction="ifRoom|withText"/>
    <item
        android:id="@+id/email"
        android:icon="@drawable/email"
        android:title="@string/email"
        myapp:showAsAction="ifRoom|withText"/>

</menu>

Manifest xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lucas"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"

        android:theme="@style/Theme.AppCompat" >
        <!--  <android:hardwareAccelerated="true"  -->

        <activity android:uiOptions="splitActionBarWhenNarrow"
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data android:name="android.support.UI_OPTIONS"
                   android:value="splitActionBarWhenNarrow" />

        </activity>
    </application>

</manifest>

What am I doing wrong? How to implement Actionbar so it stays down and with the icons' name, version 2.x or higher?

3 answers

2


First a few considerations: Keep in mind that an Actionbar has its place defined by Google’s Guide Lines (recommendations). This place is the top of the screen. Anything you do out there is not an Actionbar.

Actionbar was created in the Honycomb version (3.0) and with them the menus of previous versions were removed (menus with icons and names at the bottom). That is, if you want an Actionbar, using the class ActionBarActivity she’ll be at the top, because it’s her place.

But if you want the menus with icons and names at the bottom that existed in the Ginberbread versions and below(2.3), you will have to implement a menu normally (without support and without Actionbar) and change the property targetSdkVersion for a maximum of 10, what highly NOT recommended, because you will lose all the features of the new devices (in addition to staying with this crude and outdated layout of the old devices).

1

I’ve had several problems with actionbar in my 2.x projects they used sherlock (I know it’s not your case) so I decided to create a view who did the same thing. Then I deactivated the actionbar putting there in the AndroidManisfest (You can also programmatically disable).

  • I understand Othon, I tried to use Sherlock’s compatibility too, but it got even worse the result..

  • the actionbar of Sherlock does not allow Voce to use the layout very well. It’s an implementation bug of their own! You better make your own view

  • Othon you would have some material, something that would give me a view of how to create my own view?

1

As stated in the previous answer, there are guidelines that specify the exact position of Actionbar. The application whenever you run on devices with a smaller screen will try to decrease the items and names in actionbar so you can save space and put all items that do not have high priority in the "overflow". But you can use the Split Actionbar that will create an actionbar above and below if you are on a device with a small screen and put all items on top in devices like tablets that space in the actionbar and much larger. To do this just put in the manifest:

<manifest ...>
<activity uiOptions="splitActionBarWhenNarrow" ... >
    <meta-data android:name="android.support.UI_OPTIONS"
               android:value="splitActionBarWhenNarrow" />
</activity>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.