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?