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?
I understand Othon, I tried to use Sherlock’s compatibility too, but it got even worse the result..
– Reiko Dev
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
Othon you would have some material, something that would give me a view of how to create my own view?
– Reiko Dev