I have implemented an action bar, but when I am clicking on the icon nothing happens.
public class MainActivity extends Activity {
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.tab1:
Toast.makeText(getBaseContext(), "You touched setting button", Toast.LENGTH_LONG);
case R.id.tab2:
Toast.makeText(getBaseContext(), "You touched Map button", Toast.LENGTH_LONG);
break;
}
return super.onOptionsItemSelected(item);
}
}
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="schemas.android.com/apk/res/android"; >
<item android:id="#+id/tab1" android:icon="#drawable/ic_action_overflow" android:title="#string/tab1" android:showAsAction="ifRoom" />
<item android:id="#+id/tab2" android:icon="#drawable/ic_action_go_to_today" android:title="#string/tab1" android:showAsAction="always" />
</menu>
Call the show() method on the toast object.
This should be like-
Toast.makeText(getBaseContext(), "You touched the settings button", Toast.LENGTH_LONG).show();
Related
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
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()
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>
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>
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);
}