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.
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 2 tab while select first tab the accept menu icon should not visible.
while select second tab the accept menu icon should visible. I tried but i am getting null pointer exception.
And also should get the text from the second activity while select accept menu.
How can i do this ?
Here is my Code :
public class DetailTabActivity extends TabActivity implements OnTabChangeListener {
String name,address,rating,reference,lat,lng;
StringConstants constants;
Float rate;
MenuItem menuitem;
String url;
TabHost tabHost;
Menu menu1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.overridePendingTransition(R.anim.anim_out,
R.anim.anim_in);
constants=new StringConstants();
setContentView(R.layout.layout_detail_tab);
Intent intent = getIntent();
name = intent.getStringExtra("name");
Log.e("name",name+"");
address = intent.getStringExtra("address");
Log.e("address",address+"");
rating = intent.getStringExtra("rating");
Log.e("rating",rating+"");
reference = intent.getStringExtra("reference");
Log.e("reference",reference+"");
lat = intent.getStringExtra("lat");
Log.e("lat",lat+"");
lng = intent.getStringExtra("lng");
Log.e("lng",lng+"");
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
// Tab for Photos
TabSpec detailspec = tabHost.newTabSpec("Detail");
// setting Title and Icon for the Tab
detailspec.setIndicator("Detail");
Intent detailintent = new Intent(this, DetailActivity.class);
detailintent.putExtra("name",name);
detailintent.putExtra("address",address);
detailintent.putExtra("rating",rating);
detailintent.putExtra("reference",reference);
detailspec.setContent(detailintent);
// Tab for Songs
TabSpec reviewspec = tabHost.newTabSpec("Review");
reviewspec.setIndicator("Review/Rating");
Intent songsIntent = new Intent(this, ReviewAndRating.class);
songsIntent.putExtra("name",name);
songsIntent.putExtra("lat",lat);
songsIntent.putExtra("lng",lng);
reviewspec.setContent(songsIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(detailspec); // Adding photos tab
tabHost.addTab(reviewspec); // Adding songs tab
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#ffffff"));
// Set Tab1 as Default tab and change image
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#134960"));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.layout_detail_tab, menu);
menuitem = menu.findItem(R.id.action_accept);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_accept:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onTabChanged(String tabId) {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
if(i==0)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
else if(i==1)
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
Log.i("tabs", "CurrentTab: "+tabHost.getCurrentTab());
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
}
}
set invalidateOptionsMenu();as,
if(tabHost.getCurrentTab()==1)
{
menuitem.setVisible(true);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
else if(tabHost.getCurrentTab()==0)
{
menuitem.setVisible(false);
invalidateOptionsMenu();
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#134960"));
}
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 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
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);
}
}