I have an OptionMenu inside my activity but when I choose an option it shows nothing. I've found some tutorials but they show what I already do. What is wrong?
Thank you for the replies.
This is the code at the moment:
public class Listino extends TabActivity
{
final Context context = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
deleteFile("Ordinazioni.txt");
setContentView(R.layout.show_listino);
TabHost tabHost = getTabHost();
//Primi
Intent intentPrimi = new Intent().setClass(this, Primi.class);
TabSpec tabSpecPrimi = tabHost
.newTabSpec("Primi")
.setIndicator("Primi")
.setContent(intentPrimi);
//Secondi
Intent intentSecondi = new Intent().setClass(this, Secondi.class);
TabSpec tabSpecSecondi = tabHost
.newTabSpec("Secondi")
.setIndicator("Secondi")
.setContent(intentSecondi);
// Dolci
Intent intentDolci = new Intent().setClass(this, Dolci.class);
TabSpec tabSpecDolci = tabHost
.newTabSpec("Dolci")
.setIndicator("Dolci")
.setContent(intentDolci);
// Pizze
Intent intentPizze = new Intent().setClass(this, Pizze.class);
TabSpec tabSpecPizze = tabHost
.newTabSpec("Pizze")
.setIndicator("Pizze")
.setContent(intentPizze);
// Bevande
Intent intentBevande = new Intent().setClass(this, Bevande.class);
TabSpec tabSpecBevande = tabHost
.newTabSpec("Bevande")
.setIndicator("Bevande")
.setContent(intentBevande);
// Contorni
Intent intentContorni = new Intent().setClass(this, Bevande.class);
TabSpec tabSpecContorni = tabHost
.newTabSpec("Contorni")
.setIndicator("Contorni")
.setContent(intentContorni);
tabHost.addTab(tabSpecPrimi);
tabHost.addTab(tabSpecSecondi);
tabHost.addTab(tabSpecPizze);
tabHost.addTab(tabSpecDolci);
tabHost.addTab(tabSpecBevande);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.opzioni_menu, menu);
return true;
}
public boolean OnOptionsItemSelected(MenuItem item)
{
Toast.makeText(context, item.getItemId(), Toast.LENGTH_SHORT).show();
switch (item.getItemId())
{
case R.id.fineordinazione:
Intent intent = new Intent(context, AggiungiProdotto.class);
startActivity(intent);
return true;
case R.id.modificaordinazione:
break;
}
return false;
}
}
Aaaw.
There is a typo.
Not
public boolean OnOptionsItemSelected(MenuItem item)
but
public boolean onOptionsItemSelected(MenuItem item)
The initial letter of the method name is a lower case.
You are supposed to inflate menus through getMenuInflater(), not layouts.
Change:
menuInflater.inflate(R.layout.opzioni_menu, menu);
To this:
menuInflater.inflate(R.menu.opzioni_menu, menu); //use menu, not layout
You should use R.menu.bla_bla_bla instead of R.layout.bla_bla_bla.
For more info, read this.
create opzioni_menu.xml in res/menu/ (if you don't have menu folder, create one)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/fineordinazione" android:title="fineordinazione"/>
<item android:id="#+id/modificaordinazione" android:title="modificaordinazione"/>
</menu>
then change
menuInflater.inflate(R.layout.opzioni_menu, menu);
to
menuInflater.inflate(R.menu.opzioni_menu, menu);
Good luck hope this help !!
edit
public boolean OnOptionsItemSelected(MenuItem item)
to
#Override
public boolean onOptionsItemSelected(MenuItem item)
On <<<< o in lower case !! please
Related
I have tabhost, and it has 4 tabs : Detail, Material, Leg, Other Photo. I have successfully to display all of them. And now my question is how to get text value from EditText on Detail tabs , when I clicked the menu on right corner (on onOptionsItemSelected)? Here is my code :
public class ViewDetailItem extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_detail_item);
Global.ViewDetailItem=this;
Bundle param=getIntent().getExtras();
String tseparate=(String) param.get("tseparate");
String tgroup=(String) param.get("tgroup");
String titemcode=(String) param.get("titemcode");
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, DetailTab.class);
intent.putExtra("titemcode", titemcode);
intent.putExtra("tseparate", tseparate);
intent.putExtra("tgroup", tgroup);
spec = tabHost.newTabSpec("detail").setIndicator("Detail Item",res.getDrawable(R.layout.activity_tab_detail)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MaterialTab.class);
intent.putExtra("titemcode", titemcode);
intent.putExtra("tseparate", tseparate);
intent.putExtra("tgroup", tgroup);
spec = tabHost.newTabSpec("material").setIndicator("Mat Color",res.getDrawable(R.layout.activity_tab_materialcolor)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LegTab.class);
intent.putExtra("titemcode", titemcode);
intent.putExtra("tseparate", tseparate);
intent.putExtra("tgroup", tgroup);
spec = tabHost.newTabSpec("leg").setIndicator("Leg Color",res.getDrawable(R.layout.activity_tab_legcolor)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, OtherPhotoTab.class);
intent.putExtra("titemcode", titemcode);
intent.putExtra("tseparate", tseparate);
intent.putExtra("tgroup", tgroup);
spec = tabHost.newTabSpec("other").setIndicator("Other Photos",res.getDrawable(R.layout.activity_tab_legcolor)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menudetailitem, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuaddtocontainer:
// here is the code
// how to get value from edittext (id: EdtQty)on DetailTab
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I have two tabs in my application and I want the menu to change depending on the Tab.
TabHost tabHost = tabHost = getTabHost();
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.photo));
Intent photosIntent = new Intent(this, Photos.class);
photospec.setContent(photosIntent);
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.songs));
Intent songsIntent = new Intent(this, Songs.class);
songspec.setContent(songsIntent);
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int currentTab = tabHost.getCurrentTab();
if (currentTab == 0)
startActivity(new Intent(this, Photosoptions.class));
if (currentTab == 1)
{
startActivity(new Intent(this, Songsoptions.class));
}
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
int currentTab = tabHost.getCurrentTab();
if (currentTab == 0){
menu.clear();
inflater.inflate(R.menu.first, menu);
}
if (currentTab ==1){
menu.clear();
inflater.inflate(R.menu.second, menu);
}
return super.onPrepareOptionsMenu(menu);
}
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
closeOptionsMenu();
}});
Now, when I switch to the songs tab the photos menu is still there until I click on it then the songs menu appears. I want the menu to get updated once I click on the tab
you are preparing the option menus in the TabActivity. prepare the option menus in the individual activity instead. So that when you are on that activity only the option menu for that will be shown.
I have seen some similar posts here, but I haven't found any answers. I want to add a swipe gesture to my tab activity, so that I'm able to change tabs with the gesture.
Here's my code:
public class Domiciliaria extends TabActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
//requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seleccion);
//ActionBar bar = getActionBar();
//bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cd3b05")));
final TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
setTabColor(tabHost);
}
}
);
// Tab for primera
TabSpec primeraspec = tabHost.newTabSpec("info 1");
primeraspec.setIndicator("Datos", getResources().getDrawable(R.drawable.primera));
Intent primeraIntent = new Intent(this, Parte_primera.class);
primeraspec.setContent(primeraIntent);
// Tab for segunda
TabSpec segundaspec = tabHost.newTabSpec("info 2");
segundaspec.setIndicator("Motivos", getResources().getDrawable(R.drawable.segunda));
Intent segundaIntent = new Intent(this, Parte_segunda.class);
segundaspec.setContent(segundaIntent);
// Tab for tercero
TabSpec tercerospec = tabHost.newTabSpec("info 3");
tercerospec.setIndicator("Examen", getResources().getDrawable(R.drawable.tercera));
Intent terceroIntent = new Intent(this, Parte_tercera.class);
tercerospec.setContent(terceroIntent);
// Tab for Cuarto
TabSpec cuartospec = tabHost.newTabSpec("info 4");
cuartospec.setIndicator("otro", getResources().getDrawable(R.drawable.cuarta));
Intent cuartoIntent = new Intent(this, Parte_cuarta.class);
cuartospec.setContent(cuartoIntent);
// Tab for Final
TabSpec finalspec = tabHost.newTabSpec("info 4");
finalspec.setIndicator("Final", getResources().getDrawable(R.drawable.pago));
Intent finalIntent = new Intent(this, Final.class);
finalspec.setContent(finalIntent);
// agregando TabSpec to TabHost
tabHost.addTab(primeraspec); // Adding photos tab
tabHost.addTab(segundaspec); // Adding songs tab
tabHost.addTab(tercerospec);
tabHost.addTab(cuartospec);
tabHost.addTab(finalspec);
tabHost.getTabWidget().setStripEnabled(true);
tabHost.getTabWidget().setRightStripDrawable(R.drawable.barra_mamei);
tabHost.getTabWidget().setLeftStripDrawable(R.drawable.barra_mamei);
setTabColor(tabHost);
}
#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, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
case R.id.CasoEspecial:
startActivity(new Intent(this, CasoEspecial.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
// respond to menu item selection
}
protected void setTabColor(TabHost tabHost) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E04006")); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fbfbf3")); // selected
}
}
You can achieve the TabSwipe using ViewPager with Fragments and ActionBar.
If you wish to work with Minimum API 11, the following link will show you how. Since API 11, ActionBar has the way to create tabs in application with fragments.
If you need to give support to the older versions, you should make use of Support Library
or ActionBarSherlock
Check out this Link as well
This is the best tutorial for horizontal swipe views straight from google.
Creating Swipe Views with Tabs
I would recommened to use Fragments and FragmentActivity instead of TabActivity
I'd like to customize options menu in my app. After some googling and making a lot of effort I am still unable to do it. Here is what I want:
menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_first"
android:icon="#drawable/phone_selector"
android:title="#string/first"/>
<item
android:id="#+id/menu_second"
android:icon="#drawable/butterfly_selector"
android:title="#string/second"/>
<item
android:id="#+id/menu_third"
android:icon="#drawable/umbrella_selector"
android:title="#string/third"/>
</menu>
OptionsMenuActivity.java:
public class OptionsMenuActivity extends Activity {
private Menu currentMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
this.currentMenu = menu;
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
setMenuItemIconState(menu, MyApplication.getSelectedMenuItemDrawIcon(), false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_phone:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.phone_active);
Intent intent = new Intent(OptionsMenuActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_butterfly:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.butterfly_active);
intent = new Intent(OptionsMenuActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.menu_umbrella:
setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
MyApplication.setSelectedMenuItemDrawIcon(R.drawable.umbrella_active);
intent = new Intent(OptionsMenuActivity.this, ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setMenuItemIconState(Menu menu, int menuItemIconRes, boolean switchLastActiveMenuItemIcon) {
int menuItemId = 0;
int lastDeActiveDrawIcon = 0;
switch (menuItemIconRes) {
case R.drawable.phone_active:
menuItemId = R.id.menu_first;
lastDeActiveDrawIcon = R.drawable.phone;
break;
case R.drawable.butterfly_active:
menuItemId = R.id.menu_second;
lastDeActiveDrawIcon = R.drawable.butterfly;
break;
case R.drawable.umbrella_active:
menuItemId = R.id.menu_third;
lastDeActiveDrawIcon = R.drawable.umbrella;
break;
}
if (switchLastActiveMenuItemIcon) {
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(lastDeActiveDrawIcon);
return;
}
MenuItem menuItem = menu.findItem(menuItemId);
menuItem.setIcon(MyApplication.getSelectedMenuItemDrawIcon());
}
}
In MyApplication.java I have only a getter and a setter of selectedMenuItemDrawIcon.I managed to set the icons properly, but failed with the others.
Is it possible to customize options menu like that? If not, is there any other approaches for creating an xml file and make it appear and disappear smoothly just like basic menu of android.
Any help would be appreciated. Thanks in advance, (please provide with code example).
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);
}
}