Android menu buttons visibility - android

I have a button in my actionbar that hides all the buttons and should show another three but it shows only two of them. The number of items is 7. The buttons's (that have to be showed) showAsAction is ifRoom.
Here is how I hide/unhide my buttons:
private void hideUnhideActionButtons(boolean hide) {
MenuItem item;
Log.d("Item numbers", Integer.toString(audioMenu.size()));
for(int i = 0; i < audioMenu.size(); ++i ) {
if(i < 4)
audioMenu.getItem(i).setVisible(!hide);
else
audioMenu.getItem(i).setVisible(hide);
}
audioMenu.getItem(6).setVisible(true);
}
Here is where I call this method:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.action_camera:
...
return true;
case R.id.action_audio:
hideUnhideActionButtons(true);
return true;
case R.id.action_record:
...
return true;
case R.id.action_play:
...
return true;
case R.id.action_return:
hideUnhideActionButtons(false);
return true;
case R.id.action_delete:
...
return true;
case R.id.action_save:
...
return true;
}
return super.onOptionsItemSelected(item);
}
My 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"
tools:context="com.example.victwo.NewNoteActivity" >
<item
android:id="#+id/action_save"
android:icon="#drawable/ic_action_save"
android:orderInCategory="100"
android:title="#+string/action_save"
app:showAsAction="always"/>
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_action_remove"
android:orderInCategory="100"
android:title="#+string/action_delete"
app:showAsAction="always"/>
<item
android:id="#+id/action_camera"
android:icon="#drawable/ic_action_camera"
android:orderInCategory="100"
android:title="#+string/action_camera"
app:showAsAction="always"/>
<item
android:id="#+id/action_audio"
android:icon="#drawable/ic_action_volume_on"
android:orderInCategory="100"
android:title="#+string/action_record"
app:showAsAction="always"/>
<item
android:id="#+id/action_record"
android:icon="#drawable/ic_action_mic"
android:orderInCategory="100"
android:title="#+string/action_record"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_play"
android:icon="#drawable/ic_action_play"
android:orderInCategory="100"
android:title="#+string/action_play"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_return"
android:icon="#drawable/ic_action_back"
android:orderInCategory="100"
android:title="#+string/action_return"
app:showAsAction="ifRoom"/>
</menu>
Can anybody tell me what is the problem?

I see you have a reference to your menu, which is audioMenu. So you can access to your menu items using findItem method and giving their id's instead of indexes.
Example :
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.action_camera:
...
return true;
case R.id.action_audio:
//hide
actionMode.findItem(R.id.id_1).setVisible(false);
actionMode.findItem(R.id.id_2).setVisible(false);
//show
actionMode.findItem(R.id.id_3).setVisible(true);
actionMode.findItem(R.id.id_4).setVisible(true);
return true;
case R.id.action_record:
...
return true;
}
return super.onOptionsItemSelected(item);
}

Related

Add options in an option menu of main toolbar in Android studio

I am new at android and am trying to create a customized toolbar. I was wondering if there is any way to add options to the settings menu(3 dots) when it is clicked.
First you need to add an item in menu_main.xml(res>menu) file like.
<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="in.amzbizsol.vts.MainActivity">
<item
android:id="#+id/action_changepassword"
android:orderInCategory="1"
android:title="Change Password"
app:showAsAction="never" />
<item
android:id="#+id/action_logout"
android:orderInCategory="2"
android:title="Logout"
app:showAsAction="never" />
then in your MainActivity create something like
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_changepassword) {
Intent intent=new Intent(getApplicationContext(),ChangePassword.class);
startActivity(intent);
return true;
}else if(id==R.id.action_logout){
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
Please Check this documentation - https://developer.android.com/guide/topics/ui/menus.html

How to use Setting's in overflow Icon

I tried my level of best but I couldnt find it.I am working in navigation drawer activty where I can see a overflow Icon in top right when I click it, a Settings button like thing pops out when I click it(Settings) nothing happens
I dont no how to assing an XML to this so that when it is clicked a new activty should open
I know to create an xml and also to assign a onClickListner to the button but i am unable to proceed further since I dont no where to call the setting activty when that button(Settings button in overflow Icon) is pressed
Try this -
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
Intent intent = new Intent(this, YourSettingfActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
menu_main.xml -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
You can use find these below methods in activity and there you can inflate the menu and do want you want
Menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_search"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="#+id/action_logout"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
and you have to write the code like below in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_search:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Menu is not showing in custom toolbar

I am trying to use custom menu in my android app. I want to add some menu items.
For the purpose, I add following in my menu_main.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"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="always" />
<item
android:id="#+id/contact"
android:icon="#drawable/ic_star"
android:orderInCategory="2000"
android:title="#string/Rate"
app:showAsAction="always" />
</menu>
And in MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
Toast.makeText(this,"Hello from settings",Toast.LENGTH_LONG).show();
return true;
}
if ( id == R.id.contact)
{
startActivity(new Intent(this,ContactUs.class));
return true;
}
return super.onOptionsItemSelected(item);
}
But, it is not working at all.
I tried some solutions on SO, but none of them worked.
e.g. this
Please help me to solve this.
You have to set your toolbar like
setActionBar(toolbar);
in onCreate()

how to show icon on action bar in android

I want to implement action bar in my application i write some code in activity and add xml file .
but icon on action bar is not show . I try much but i cant know
that what is wrong with this code
Java code is here:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.testmenu, menu);
return true;
}
/*public boolean onOptionsItemSelected1(MenuItem item)
{
switch(item.getItemId()){
case R.id.register:
finish();
}
return true;
}
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/* int id = item.getItemId();
if (id == R.id.action_settings) {
}
*/
switch (item.getItemId()) {
case R.id.register:
finish();
break;
default:
break;
}
return true;
// return super.onOptionsItemSelected1(item);
}
and xml code is
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/register"
android:title="#string/action_settings"
android:showAsAction="alway"
android:orderInCategory="100"
android:icon="#drawable/register"
/>
</menu>
There's a typo in your testmenu.xml
android:showAsAction="alway"
change to:
android:showAsAction="always"
android:showAsAction="alway"
alway -> always
UPDATE
try this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/register"
android:title="#string/action_settings"
android:showAsAction="always"
myapp:showAsAction="always"
android:visible="true"
android:icon="#drawable/register" />
</menu>

Action bar item not finding the id

I'm following the Adding Action Button tutorial on devolper.android.com and I've ran into yet another problem. When I go to reference the action bar id with R.id.action_search it says it doesnt exist, but R.id.action_settings shows up just fine...?
onOptionsItemSelected method:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
//int id = item.getItemId();
//if (id == R.id.action_settings) {
//return true;
//}
//return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
main.xml:
<menu>
<!-- Search should appear as an action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings should appear as an action button -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>

Categories

Resources