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

1

I'm implementing Action Bar in android version 2.x or higher, however, I'm having difficulty with how the elements are displayed. In version 2.x the bar appears below, only without the names of the menus, and in version 3.0 or higher the action bar is lined up at the top. I want the action bar to be aligned below, with the names of the menus as much in version 2.x as in the others.   I'm using the action bar's share class 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 that it is below and named after the icons, version 2.x or higher?

    
asked by anonymous 14.02.2014 / 17:02

3 answers

2

First things first: Keep in mind that an ActionBar has its place well defined by Google's Guide Lines. This place is the top of the screen. Anything you do out there is not an ActionBar.

ActionBar was created in the Honycomb (3.0) version and with them the menus from previous versions have been removed (menus with icons and names on the bottom). That is, if you want an ActionBar, using the ActionBarActivity class, it will be at the top, because it's the place of it.

But if you want the menus with icons and names on the bottom that existed in the Ginberbread and below (2.3) versions, you will have to implement a menu normally (without support and without ActionBar) and change the targetSdkVersion property to maximum 10, what highly NOT recommended , because you will lose all the features of the new devices (besides getting that rough and outdated layout of the old handsets).

    
15.02.2014 / 13:20
1

I already had several problems with actionbar in my projects for 2.x that used sherlock (I know it's not your case) so I decided to create a view myself doing the same thing. Then I deactivated the actionbar by putting it in AndroidManisfest (You can deactivate programatically too).

    
14.02.2014 / 18:34
1

As stated in the previous response, there are guidelines that specify the exact position of ActionBar. The application whenever running on devices with a smaller screen will try to decrease the items and names in the actionbar so that space can be saved and all items that do not have high priority overflow. But you can use the Split Actionbar that will create an actionbar on top and one on the bottom if you are on a small screen device and put all items on top devices like tablets that space in the actionbar is 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>

    
16.02.2014 / 18:29