I tried to implement an action bar in my application.
menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/itemAdd" android:showAsAction="ifRoom|withText" android:title="ADD"> </item> <item android:id="@+id/itemRefresh" android:showAsAction="ifRoom|withText" android:title="REFRESH"> </item> <item android:id="@+id/itemHelp" android:title="HELP"> </item> </menu>
And created menu
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.menu, menu); return true; }
But it does not show the action bar even if minSdkVersion
is 11. What is the reason?
You may otherwise add the action bar by calling requestFeature(FEATURE_ACTION_BAR) or by declaring it in a custom theme with the windowActionBar property. Beginning with Android L (API level 21), the action bar may be represented by any Toolbar widget within the application layout.
if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar. findViewById(R.
In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users.
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.
Remove your theme for your actionbar activity in androidManifest file. Now it will work...
<application android:allowBackup="true" android:icon="@drawable/tasktodo" android:label="@string/app_name" >
Don't add any theme in your application manifest file. If you added one, please remove and try running it...
change Activity to AppCompatActivity in your class. That should be the easiest if you want to add it fast.. I'll add the code for someone who is new to Android OS:
public class YourActivity extends Activity
into
public class YourActivity extends AppCompatActivity
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