i am new at android.I am using three dots menu in my android app but when i click on item on on three dots menu..my app crashes.can someone help me here is my code:
enter code here#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
break;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
break;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
break;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
return true;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
return true;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
return true;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Seems there is nothing wrong with the code.
This could be issue with declration in manifest.
Make sure the activities account, help are declared in manifest.
Related
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 a beginner android programmer ... I want to do something simple:
can I use these function and show my action bar in all the activities without copy these function in every activity's file?
thank you for help
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.item1:
Intent intent2 = new Intent(MainActivity.this,Activity3.class);
startActivity(intent2);
break;
// action with ID action_settings was selected
case R.id.item4:
Intent intent3 = new Intent(MainActivity.this,Activity4.class);
startActivity(intent3);
break;
case R.id.item5:
Intent intent4 = new Intent(MainActivity.this,Bmicalc.class);
startActivity(intent4);
break;
case R.id.item6:
Intent intent5 = new Intent(MainActivity.this,Activity5.class);
startActivity(intent5);
break;
default:
break;
}
return true;
}
Keep these in a parent activity class and extend it inside all your activities instead of extending Activity
Keep a parent activity which has these 2 common methods.
public class ParentActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.item1:
Intent intent2 = new Intent(MainActivity.this,Activity3.class);
startActivity(intent2);
break;
// action with ID action_settings was selected
case R.id.item4:
Intent intent3 = new Intent(MainActivity.this,Activity4.class);
startActivity(intent3);
break;
case R.id.item5:
Intent intent4 = new Intent(MainActivity.this,Bmicalc.class);
startActivity(intent4);
break;
case R.id.item6:
Intent intent5 = new Intent(MainActivity.this,Activity5.class);
startActivity(intent5);
break;
default:
break;
}
return true;
}
}
Extend the parent class in your activity.
public class YourActivity extends ParentActivity {
}
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;
}
I'm trying to make my top action bar icon to allow users to go back to previous screen. I tried to implement these codes. But none are working. Can anyone please guide me on this. I know this looks simple, I'm new to android . Below are my codes.
Problem : When i tap on the icon button it just cleared my screen without going to the previous screen.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_item);
checkInternetConnection();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //<--THIS
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
Intent intent = new Intent(this, SingleViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the way I do it:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}
In your ressources(res)
go to menu
and add
this make sur u have some button back for in your drawable
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/back"
android:icon="#drawable/back1"
android:showAsAction="always|withText"
android:title="back"/>
now in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
case R.id.back:
Intent in = new Intent(this, <classname which you want to go back>.class);
startActivity(in);
break;
}
return false;
}
you may try this code
<item
android:id="#+id/back"
android:icon="#drawable/btn_back"
android:showAsAction="always|withText"
android:title="#string/txt_back"/>
>
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case R.id.back:
back_action();
return true;
default:
break;
}
return true;
}
>
i have created an options menu for my database class. Upon launching the options menu, I would like to the desired activity at the click of the specified button.
But the issue is that if i click on any option , i get directed to the MainMenu.class . Any ideas why this is happening ?
code:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
new MenuInflater(this).inflate(R.menu.optionmenu, menu);
return(super.onCreateOptionsMenu(menu));
}
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
}
case R.id.takesurvey:
{
Toast toast=Toast.makeText(this, "check", 2000);
toast.show();
Intent r1=new Intent(Database.this,SurveyActivity.class);
startActivity(r1);
}
case R.id.viewstats:
{ Intent r2=new Intent(Database.this,Stats.class);
startActivity(r2);
}
case R.id.changesort:
{ Intent r3=new Intent(Database.this,MainMenu.class);
startActivity(r3);
}
case R.id.menuexit:
{ Intent r4=new Intent(Database.this,MainMenu.class);
startActivity(r4);
}
}
return true;
}
It looks like you are missing a break statement in every case.
public boolean onOptionsItemSelected ( MenuItem item){
switch (item.getItemId())
{
case R.id.item1:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.takesurvey:
Toast.makeText(this, "check", 2000).show();
startActivity(new Intent(Database.this,SurveyActivity.class));
break;
case R.id.viewstats:
startActivity(new Intent(Database.this,Stats.class));
break;
case R.id.changesort:
startActivity(new Intent(Database.this,MainMenu.class));
break;
case R.id.menuexit:
startActivity(new Intent(Database.this,MainMenu.class));
break;
return true;
}
For each of your conditions in the Switch statement in onOptionsItemSelected() you must return true. If you handle the case then you must return true, if you don't then you should call the super class implementation of it.
case R.id.item1:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
return true;
}
Go through this for more details
http://developer.android.com/guide/topics/ui/menus.html#options-menu