Android: show menu options on an Activity with Tabs - android

I want to show menu options in an activity which hosts tab views. Here is the code of my tab view activity.
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("photo list")
.setContent(new Intent(this, List8.class)));
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.removeGroup(0);
menu.add(0, 0, 0, "Home").setIcon(
android.R.drawable.ic_menu_preferences);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0:
setResult(10);
finish();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
Now when i am pressing android menu button, onPrepareOptionsMenu is called which is correct but when i select the menu option, nothing happens. I have also debugged my code and control is not reaching in onMenuItemSelected.
Please help.

I got my solution.. i dont whether it is perfect way to do it but it is working..
instead of using onMenuItemSelected , i just used onOptionsItemSelected and my code is working.
Here is the final code:
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("photo list")
.setContent(new Intent(this, List8.class)));
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.removeGroup(0);
menu.add(0, 0, 0, "Home").setIcon(
android.R.drawable.ic_menu_preferences);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
setResult(10);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}

Related

Option menu option not directing to another activity

I want to navigate to another Activity when click on the option menu item 'settings' from the menu bar. Nothing actual happens.I have checked similar issues posted here, but i can understand why this is not working for option menu.
see the code below:
Can't go to a new activity from selected option from option menu
<item
android:id="#+id/mySettings"
android:title="#string/action_settings" />
<item
android:id="#+id/logout"
android:title="log out" />
code:
public class Dashboard extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_bar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
public void openConfigure(){
Intent intent = new Intent(this,Configure.class);
this.startActivity(intent);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Use onOptionsItemSelected instead of onContextItemSelected cause you are using OptionMenu not ContextMenu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return super.onOptionsItemSelected(item);
}
to select an options menu item, you have to override onOptionItemSelected() :
try below code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.mySettings:
openConfigure();
break;
}
return true;
}

saving the edittext value to another activity on click of save actionbutton in android

how to save the text given to edit text, after clicking the save action button in action bar that should save to another activity.
my first activity Add.java
public class Add extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_add_action, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:save();
return true;
case R.id.cancel:cancelnavi();
return true;
}
return super.onOptionsItemSelected(item);
}
private void save() {
EditText titlename;
titlename=(EditText)findViewById(R.id.edittitle);
String title=titlename.getText().toString();
if (title.equalsIgnoreCase("")){
Toast.makeText(Add.this, "Script title should not be empty", Toast.LENGTH_LONG).show();
} else {
Intent i;
i=new Intent(getApplicationContext(), Scripts.class);
i.putExtra("n", titlename.getText().toString());
startActivityForResult(i, 0);
titlename.setText("");
}
}
}
It should be saved to scripts.java
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:editscript();
break;
}
return true;
}
private void editscript() {
Intent editinIntent;
editinIntent=new Intent(Scripts.this, Edit.class);
startActivity(editinIntent);
}
}
Maybe this can help you.
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
//Extract the data
String yourTitle = getIntent().getStringExtra("n");
}
}

onbackpressed() method crash

I am using authentication to enter into a page, after authenticated only the user enters into the page. i wrote a code for onbackpressed(), but it is not working. Here DatabaseDemo and Login are the two classes. when i press the back button the login class with username and password is displaying.
public class DatabaseDemo extends TabActivity {
DatabaseHelper dbHelper;
GridView grid;
TextView txtTest;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SetupTabs();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, 1, 1, "Add Employee");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
//Add employee
case 1:
Intent addIntent=new Intent(this,AddEmployee.class);
startActivity(addIntent);
break;
}
super.onOptionsItemSelected(item);
return false;
}
void SetupTabs()
{
TabHost host=getTabHost();
TabHost.TabSpec spec=host.newTabSpec("tag1");
Intent in1=new Intent(this, AddEmployee.class);
spec.setIndicator("Add Employee");
spec.setContent(in1);
TabHost.TabSpec spec2=host.newTabSpec("tag2");
Intent in2=new Intent(this, GridList.class);
spec2.setIndicator("Employees");
spec2.setContent(in2);
host.addTab(spec);
host.addTab(spec2);
}
#Override
public void onBackPressed()
{
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}
}
You have it to start the Login on back pressed, so if it's crashing, then there is something wrong with your login activity more than likely, and not here.

How to make auto popup menu items of android without clicking the menu button

I have made some menu items, and the items popup when I click menu button of my android phone. But I want , when I shall enter the activity The menu items will auto popup without clicking the menu button...
public class SalesTrackerRoot extends Activity {
private static final int ORDER_ID = Menu.FIRST+1;
private static final int STORE_ID = Menu.FIRST+2;
private static final int SETTINGS_ID = Menu.FIRST+3;
private SalesTrackerDBAdapter mDbHelper;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salestrackerroot);
mDbHelper=new SalesTrackerDBAdapter(this);
mDbHelper.open();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order List").setAlphabeticShortcut('o');
menu.add(Menu.NONE, STORE_ID, Menu.NONE, "Store Entry").setAlphabeticShortcut('s');
menu.add(Menu.NONE, SETTINGS_ID, Menu.NONE, "Settings").setAlphabeticShortcut('e');
return(super.onCreateOptionsMenu(menu));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ORDER_ID:
startActivity(new Intent(this, SalesOrderList.class));
return(true);
case STORE_ID:
startActivity(new Intent(this, AddNewStoreName.class));
return(true);
case SETTINGS_ID:
startActivity(new Intent(this, SalesTrackerSettings.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
In your onCreate simply call to openOptionsMenu().

Intent error - process stop unexpectdly

I need some help in my application. I have these classes:
Ajuda.class:
public class Ajuda extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajuda);
}
}
testes.class:
public class teste extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnuAjuda:
Intent i = new Intent (this.getApplicationContext(),
Ajuda.class);
this.startActivity(i);
return true;
default: return super.onOptionsItemSelected(item);
}
}
}
On method onOptionsItemSelected in case R.id.mnuAjuda: if I got
Intent i = new Intent (this.getApplicationContext(),
Ajuda.class);
this.startActivity(i);
instead of
setContentView(R.layout.ajuda);
the application stop unexpectdly and I dont know why.
Did you put something like
<activity android:name=".Ajuda" android:label="Ajuda" />
in AndroidManifest.xml?

Categories

Resources