I'm having troubles with the ActionBar of my application, the problem is that is still showing the old android menu. I have checked other awnsers to this question but nothing has worked. Mi minimum app SDK allowed is 7 but I'm running in a device with Android 4.2.
I imported
import android.support.v7.app.ActionBarActivity;
This is my onCreate() method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputTextView = (EditText) findViewById(R.id.inputText);
outputTextView = (TextView) findViewById(R.id.outputText);
inputTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
showMessage((View) textView);
return true;
}
});
registerForContextMenu(inputTextView);
}
This is my onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
This is my XML
<item
android:id="@+id/menu"
android:orderInCategory="0"
android:showAsAction="always"
android:icon="@drawable/abc_ic_cab_done_holo_dark"
>
</item>
<item
android:id="@+id/lol"
android:orderInCategory="0"
android:showAsAction="ifRoom"
android:icon="@drawable/abc_ic_go">
</item>
Here is my appManifest.xml, I know that it miss the package (I removed it and I'm trying to add it)
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19"/>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:label="@string/app_name" android:name="com.ricardo.message.MainActivity" android:theme="@style/Theme.AppCompat.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Thanks for the help!
The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and customizable in terms of appearance and functionality. Unlike ActionBar, its position is not hardcoded i.e., not at the top of an activity.
The toolbar bar (formerly known as action bar) is represented as of Android 5.0 via the Toolbar view group. It can be freely positioined into your layout file. It can display the activity title, icon, actions which can be triggered, additional views and other interactive items.
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button. In general an ActionBar consists of the following four components: App Icon: App branding logo or icon will be displayed here.
Per the Adding Action Items guide, you must use the http://schemas.android.com/apk/res-auto
schema version of showAsAction
, rather than android:showAsAction
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu"
android:orderInCategory="0"
app:showAsAction="always"
android:icon="@drawable/abc_ic_cab_done_holo_dark" />
<item
android:id="@+id/lol"
android:orderInCategory="0"
app:showAsAction="ifRoom"
android:icon="@drawable/abc_ic_go" />
</menu>
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
OR
Make a XML layout call the tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:src="@drawable/ic_action_search"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Now in you main activity add this line
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With