I am trying to add a button to my action bar in android
Below is my code in the menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_compose"
android:icon="#drawable/help"
android:title="help"/>
</menu>
and I added this to my Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
But when I run the app the Actionbar is still the same, I'm not sure if it matters but I have a Navigation drawer in my app to.
use actionbar sherlock library and use below code.
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class HomeActivity extends SherlockFragmentActivity implements TabListener
{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.issue240);
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(YOUR_BUTTON_LAYOUT);
}
}
Related
the button more information is not visible!!!!
xml of menu :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/more_information"
android:title="#string/action_mnn"
/>
</menu>
main activity :-
package com.example.yashchaudhary.modernartui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.more_information:
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
screenshot after clickin on 3 dots
In my case it was a Theme specific for the action bar in the activity layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
.../>
Removing the theme property solved my problem
<androidx.appcompat.widget.Toolbar android:id="#+id/toolbar" android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
Removing the theme attribute will resolve this problem.
Try to add to the item on the menu xml
android:orderInCategory="100"
and add break; after
case R.id.more_information:
return true;
I´m trying to add in my ActionBar my app icon, but I was reading on Google Developers and I can get the solution. I´m doing this on my ActivityMain:
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
But it doesn´t work.
This is my first day with Android and I just want make an ActionBar with the main icon on the left and an icon search.
Thank You.
With API21 you should use the new Toolbar class.
Put the Toolbar in your layout:
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="attr/actionBarSize"
android:background="?attr/colorPrimary" />
Then in your code (onCreate in your Activity for example):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_myNavigationIcon);
You can find more info in the official post.
Add:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setLogo(R.drawable.ic_launcher)
You should be changing the logo, which is by default the same as the launcher icon.
You should do the following steps to implement ActionBar :
1 - Extend ActionBarActivity like this :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Notice to import this :
import android.support.v7.app.ActionBarActivity;
2 - Add lines below into onCreate :
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
3- Create your xml menu under res/menu/your_menu.xml
somthing like this :
your_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
4- Inflate the menu to the action bar, and handle the action bar item clicks :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.your_menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Hope this help you!!!
I have
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class ActivityHome extends SherlockFragmentActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
...........
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
but the menu is not showing in a title bar, but in the bottom of a screen(by clicking "menu" button on device). Like by normal activity... what am i doing wrong??
menu xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_add_record"
android:icon="#drawable/ic_action_plus"
android:title="#string/add"
>
</item>
</menu>
please help! :)
you can get individual menu items to show in the ActionBar by adding the android:showAsAction="always" attribute
http://developer.android.com/guide/topics/resources/menu-resource.html
I am doing menu item. But i am not able to view the menu item in the screen. Can anybody tell me what is getting wrong or what other need to be added to get the code working. I have got totally stuck.
package com.example.androiddemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MenuItem extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.menu.menupage);
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menupage, menu);
return true;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/about"
android:icon="#drawable/images"
android:title="About"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/setting"
android:icon="#drawable/ic_launcher"
android:title="App Setting"
/>
</menu>
Here you have to set your activity's xml file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Your activity's xml file.
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menupage, menu); // Here you are setting the menu whatever options you want.
return true;
}
}
After running this, just press on menu, It should show your options. If not post back.
I am using ActionbarSherlock library to implement action bar for Android 2.1 API 7 platform. The Sherlock library is imported to my own project successfully.
Then, in my project, I have res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/test1"
android:title="#string/test_1"
android:showAsAction="ifRoom"
/>
<item android:id="#+id/test2"
android:title="#string/test_2"
android:showAsAction="ifRoom"
/>
</menu>
My Activity:
import android.support.v4.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
public class MyActivity extends FragmentActivity {
#Override
protected void onStart() {
super.onStart();
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
}
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
}
With the above code, My application has an action bar. But the problem is that all the items defined in the above menu xml file are showing on the right hand side of the action bar, and the left side part of action bar is left blank.
You probably already noticed in my Activity code, I have:
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
The above two line codes are defined in my Activity onStart() callback which disable the action bar default text and icon.
But how can I have my custom items defined in menu xml file to be shown evenly on the action bar instead of only show on the right hand side of the action bar?