how can I link my setting buttons in the action bar with a another activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.about:
about(); // shows a dialogbox
break;
case R.id.settings:
// I want to link this with my setting screen
break;
}
return super.onOptionsItemSelected(item);
}
I linked up my about action bar button with a dialog box but for my settings, I want it to go to another activity. How can I do this? The activity I want to link to is called settings.java
How about this?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.about:
about(); // shows a dialogbox
break;
case R.id.settings:
startActivity(this, settings.class);
// This doesn't work?
break;
}
return super.onOptionsItemSelected(item);
}
Related
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//action
break;
}
return super.onOptionsItemSelected(item);
}
How can I change the Icon from the Back Button (android.R.id.home) in OptionsMenu?
Thank you!
switch (item.getItemId()) {
case android.R.id.home:
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.icon_converted));
break;
}
Here menu.getItem(0) will the first menu, it should behave accordingly.
I have the following in my code and I want to switch to this new activity when I select it from the menu, but the app just keeps closing:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent("com.my.project.BAAS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Please help, 2 days so far.....
You have to pass context and class which is to be opened.
Your code should be like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent(getContext(),BAAS.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Hope it helps:)
I have an activity menu and on one fragment I want to replace an item inside that menu. The item when pressed launches an activity. My fragment menu item works, but it also calls the activity menu item intent. I need to remove the duplicate main activity menu item being selected too.
Main Activity menu:
public boolean onOptionsItemSelected(MenuItem item) {
else if(itemId == R.id.action_settings)
startActivity (new Intent(getApplicationContext(),
PreferencesActivity.class));
return super.onOptionsItemSelected(item);
}
Fragment menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getActivity(),
PreferencesFragment.class));
return false;
default:
break;
}
return false;
}
It is only if the activity doesn't use the action that it is passed to the fragment. You may be able to add in the activity a boolean that causes the switch to be bypassed for that item.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (!isMyFragmentPresent()) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(getApplicationContext(),
PreferencesActivity.class));
return true;
default:
break;
}
}
return false;
}
public void setMyFragmentIsPresent(boolean isMyFragmentPresent) {
this.isMyFragmentPresent = isMyFragmentPresent;
}
UPDATE: I realise this doesn't work if you have more items than the one you want to change. Maybe you would be better having a default Fragment that loads in the activity that does nothing but load the R.id.action_settings settings button and what it does. This fragment would then be replaced by your new Fragment.
Can you please tell me what is wrong with my code ? (I bet it's something stupid but i can't find it..)
My code :
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.action_go_home:
//go home action
Intent i = new Intent(this, UserProfileActivity.class);
startActivity(i);
return true;
break;
case R.id.action_select_categories:
//select categories
return true;
case R.id.action_refresh:
//refresh timeline
return true;
default :
return super.onOptionsItemSelected(item);
}
}
You have a return statement before your switch.
You should fix it like:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()){
....
I am using android-support-v7-appcompat.
In an activity I want to show in the actionbar the back button.
I do:
public class News extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_news_screen);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
}
}
And:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println(item.getItemId()); // 16908332
System.out.println(R.id.home); // 2131034132
System.out.println(R.id.homeAsUp); // 2131034117
switch(item.getItemId())
{
case R.id.home:
onBackPressed();
break;
case R.id.homeAsUp:
onBackPressed();
break;
case 16908332:
onBackPressed(); // it's works
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
If I use numerical filter by id works, but I think that ID is generated by R and therefore can change, is therefore used R.id. . Any idea?
The home/back icon in the actionbar has the id android.R.id.home.
You can look for that id.
The values in android.R.* will never change and are linked statically.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.home:
onBackPressed();
break;
case R.id.homeAsUp:
onBackPressed();
break;
case android.R.id.home:
onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}