ActionBar options and icons [duplicate] - android

This question already has answers here:
Can't display an icon in the actionbar
(2 answers)
Closed 7 years ago.
I'm trying create an ActionBar for my Android app with Android Studio, but when I execute, the items don't appear. I set the showAsAction to always or ifRoom but nothing happened.
Menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/itmp"
android:icon="#mipmap/ic_launcher"
android:title="Profile"
android:showAsAction="always"/>
<item android:id="#+id/itap"
android:icon="#mipmap/ic_launcher"
android:title="My order"
android:showAsAction="ifRoom" />
<item android:id="#+id/ithc"
android:icon="#mipmap/ic_launcher"
android:title="Historic"
android:showAsAction="ifRoom" />
<item android:id="#+id/itsr"
android:icon="#mipmap/ic_launcher"
android:title="Exit"
android:showAsAction="ifRoom"/>
</menu>
MainActivity code:
public class MainScreen_Activity extends AppCompatActivity {
GridView grid;
Button c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainscreen);
c = (Button) findViewById(R.id.btnmaincar);
c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
}
when I execute the app in my smartphone anything doesn't appear. Only an option icon with all the items.

When using AppCompatActivity, you need to use app:showAsAction instead of android:showAsAction.
Your menu XML should look more like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/itmp"
android:icon="#mipmap/ic_launcher"
android:title="Profile"
app:showAsAction="always"/>
</menu>

Related

Don't display icon on MenuBar in Android [duplicate]

This question already has answers here:
showAsAction = "always" is ignored in Toolbar
(2 answers)
Android 4.3 menu item showAsAction="always" ignored
(13 answers)
Closed 5 years ago.
I'm building an app with AndroidStudio. I want to display an ActionBar in my application.
I want to add a menu into my AppCompactActivity. This is the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_save"
android:showAsAction="always"
android:title="Salva"/>
</menu>
This is the layout that I can see from Android Studio preview.
This is my AppCompactActivity:
public class setting extends AppCompatActivity{
public Program program;
public TextView textTargetUri;
public TextView labelUrl;
public ImageView targetImage;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
img = (ImageView)findViewById(R.id.ImageView01);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_setting, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
getSchermataHome();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
But this is the layou that I can see:
How can I fixed it?
Try using the following code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_save"
android:title="Salva"
app:showAsAction="ifRoom|always"/>
</menu>
change always to ifRoom|always with app attribute
Change your android:showAsAction="always" to app:showAsAction="always"
So now your code would be
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_save"
app:showAsAction="always"
android:title="Salva"/>
</menu>
Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices.

How to Add a CheckBox in Toolbar with custom background - android

Hi i'm new in android and here
i want to Have a CheckBox in my Toolbar with a custom background
my use case is : i want to add current post(in my activity) to favorites list by a checkbox in toolbar
and i wanna use a star(on/off) icon.
i tried this but the checkBox is null
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/shwo_menu_download_mp3"
android:title="#string/download_mp3"
app:showAsAction="never"/>
<item android:id="#+id/show_menu_add_to_fav"
android:checked="true"
android:enabled="true"
app:showAsAction="always"
/>
<item android:id="#+id/show_menu_setting"
android:title="#string/show_menu_setting"
app:showAsAction="never" />
and my Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_speech);
initCheckBox();
...
}
CheckBox checkBoxFav;
private void initCheckBox() {
checkBoxFav = (CheckBox) findViewById(R.id.show_menu_add_to_fav);
checkBoxFav.setText("some Text");
}
with toolbar you can do this
setSupportActionBar(toolbar);
View view= getLayoutInflater().inflate(R.layout.view_, null);
Checkbox chbox = view.findViewById(..);
//chbox.do what u want with it
toolbar.addView(logo);
the view layout contains a checkbox and design it as you wish
You have to follow these steps:
1. Create a toolbar and add it to your activity. I'm not going to explain that since there are a ton of tutorials on the matter.
2. Create a menu with a checkbox widget in it:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/menu_star"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom"
android:title="#string/favorite" />
</menu>
Add the set of icons in the drawable folder. One icon for the unchecked state and one icon for the checked state.
Create a resource file and name it whatever you want (In this example the name is star.xml), add it to the drawable folder and insert the following code in it:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/star_black"
/>
<item
android:state_selected="true"
android:drawable="#drawable/star_black"
/>
<item
android:state_checked="false"
android:drawable="#drawable/star_border_black"
/>
<item
android:drawable="#drawable/star_border_black"
/>
</selector>
create an onCreateOptionsMenu method in your activity and insert the following code. The icon of the checkbox will change automatically after getting checked or unchecked.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);
CheckBox checkBox = (CheckBox)menu.findItem(R.id.menu_star).getActionView();
checkBox.setButtonDrawable(R.drawable.star);//set the icon to star.xml
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//set the action of the checkbox
}
});
return super.onCreateOptionsMenu(menu);
}

How to show menu item in action bar?

I want the menu items to show on action bar. There is a lot of space on the ActionBar, but still they don't show up. Every item shown as three dots, why?
.java file
public class GalleryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/tools">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/shareId"
android:title="share"
android:icon="#drawable/share"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/delete"
android:title="delete"
android:icon="#drawable/delete"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/searchId"
android:title="search"
android:icon="#drawable/search"
MissionAndroid:showAsAction="ifRoom"/>
</menu>
Currently, you're using "http://schemas.android.com/tools" which does not offer the functionality you're looking for and your showAsAction modifiers are being ignored.
Try updating your main_menu.xml with the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/apk/res-auto">
...
</menu>
Better yet, to follow convention, replace MissionAndroid in this file with app:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
app:showAsAction="always"/>
...
</menu>

how to show menu item under overflow icon in android api 8+

How I can force menu item to show under overflow icon. I am providing app support from api level 8 and above. for now it is showing as actionview .
My menuLayout is as follows.
//main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_menu_setting"
android:title="#string/menuItemSetting"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_signout"
android:title="#string/menuItemLogout"
app:showAsAction="ifRoom"/>
</menu>
in Java class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
I have tried with other options of showAsAction but none of them worked. Please suggest me how I can show above two menu itme under overflow icon(when i click on over these two will appearer as actionlist.)
layout for custom action bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/header"
android:orientation="vertical" >
<TextView
android:id="#+id/tvActionBarTitle"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button.../>
<!-- your code for button -->
</LinearLayout>
You can call below function in onCreate and define button in this function as I have defined appName text view and its OnClick event
private void createCutomActionBarTitle()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
this.getActionBar().setHomeButtonEnabled(true);
}
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
this.getActionBar().setBackgroundDrawable(drawable);
this.getActionBar().setIcon(logo);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.custom_action_bar, null);
((TextView) v.findViewById(R.id.tvActionBarTitle)).setText(Global.ACTIVITY_NAME);
this.getActionBar().setCustomView(v);
}
Hope this will be useful for you :)
But clicking physical button of device will not trigger this button you have to look for it, as I don't have any idea about it
Could you post java code ? Do you have code below in your activity ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.menu, menu);;
return super.onCreateOptionsMenu(menu);
}
Edit :
I alway use code like this(when i click action_settings it will show action list contains setting and bar, i think just replace android:title="#string/action_settings" with android:icon)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:enabled="true"
android:visible="true"
android:title="setting"
android:id="#+id/setting">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="B"
android:id="#+id/bar">
</item>
</menu>
</item>
<item
android:id="#+id/action_settings1"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Hope it helps

actionbar menu item onclick?

I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<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" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />

Categories

Resources