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);
}
}
}
Related
So i have made a tabhost in my app (4.2.2) but when i fill the tab indicator with the title of the tabs the images disapear, and when i only put the images path it shows the images but i want to show both. can anyone help me
In this case the images will appear but as y can see the title is not filled
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
tabHost.clearAllTabs();
// create an intent for the tab which points at the class file for that tab
intent = new Intent().setClass(this, AllProductsActivity.class);
//give the tab a name and set the icon for the tab
spec = tabHost.newTabSpec("Pesagem").setIndicator("", res.getDrawable(R.drawable.icon_config)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AllContActivity.class);
spec = tabHost.newTabSpec("Contagem").setIndicator("", res.getDrawable(R.drawable.icon_config)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AllGuiaActivity.class);
spec = tabHost.newTabSpec("Guia").setIndicator("", res.getDrawable(R.drawable.icon_config)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
if i do this, it only appears the title:
intent = new Intent().setClass(this, AllGuiaActivity.class);
spec = tabHost.newTabSpec("Guia").setIndicator("Pesagem", res.getDrawable(R.drawable.icon_config)).setContent(intent);
tabHost.addTab(spec);
What can i do to have title and an image on the tab
View view = LayoutInflater.from(context).inflate(R.layout.tabs, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
ImageView iv = (ImageView) view.findViewById(R.id.tabsImage);
iv.setImageResource(image);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(view)
.setContent(new Intent(this, activity));
mTabHost.addTab(setContent);
The layout can just be a LinearLayout with a TextView and an ImageView.
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 hope you can help, I can't seem to get the current tab to reload if a tab is currently selected and then a user click on it again, could someone point out what I'm doing and perhaps modify my code to show me what I'm doing wrong as I have viewed a lot of threads on here and via google but now one seems to know the answer, that or I'm just dumb :D thank you :)
public class HelloTabWidget extends TabActivity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
final 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, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
// Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());
switch (tabHost.getCurrentTab()) {
case 0:
//do what you want when tab 0 is selected
test();
break;
case 1:
//do what you want when tab 1 is selected
break;
default:
break;
}
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
First create one variable in application class like below:
public class GlobalClass extends Application {
public int displayTab = 0;
public int getDisplayTab() {
return displayTab;
}
public void setDisplayTab(int displayTab) {
this.displayTab = displayTab;
}
}
and modify your Activity as below:
public class HelloTabWidget extends TabActivity implements OnClickListener {
GlobalClass globel;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
globel = (GlobalClass) getApplication();
Resources res = getResources(); // Resource object to get Drawables
final 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, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(globel.getDisplayTab());
//Here Handling first tab click
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
test();
Log.d("Clickedddddddddddd", "1"
+ getTabHost().getCurrentTabTag());
HelloTabWidget.this.finish();
globel.setDisplayTab(0);
Intent i = new Intent(HelloTabWidget.this, HelloTabWidget.class);
startActivity(i);
}
});
}
public void test (){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Warning");
alert.setMessage("You are about to self-destruct!");
alert.show();
}
}
i want to make an intent from a tab to another.. how can i do that?
i only now how to make between activity's. and i need to go from a tab to another..
i have this code for the tabs
public class Main extends TabActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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, Contas.class);
Intent a = new Intent(Main.this, Contas.class);
Intent b = new Intent(Main.this, Registros.class);
Intent c = new Intent(Main.this, Relatorios.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Contas").setIndicator("Contas",
res.getDrawable(R.drawable.ic_tab_accounts))
.setContent(a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
// Do the same for the other tabs
//intent = new Intent().setClass(this, Registros.class);
spec = tabHost.newTabSpec("Registros").setIndicator("Registros",
res.getDrawable(R.drawable.ic_tab_registry))
.setContent(b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
//intent = new Intent().setClass(this, Relatorios.class);
spec = tabHost.newTabSpec("Relatorios").setIndicator("Relatorios",
res.getDrawable(R.drawable.ic_tab_reports))
.setContent(c.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Rather then calling intent you can just set the desired tab selected.
tabHost.setCurrentTab(index)
Rather then calling intent you can just set the desired tab selected.
Use
intent.putExtra("tabIndex", index);
and call the activity. Now in the calling Activity's onCreate() or onResume() use
int index = getIntent().getExtraInt("tabIndex", -1);
if(index != -1)
tabHost.setCurrentTab(index);
Is there a way we can add setOnLongClickListener in Tabs?? Or is there any other way where i can call one activity when clicked on a tab and a different activity when long clicked on the same tab??
public class HelloTabWidget extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setOnLongClickListener(new OnLongClickListener(){
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
//// Intent i=new Intent(getApplicationContext(),LongClickStuff.class);
// startActivity(i);
// return true;
Toast.makeText(getApplicationContext(), "into long click", Toast.LENGTH_LONG).show();
return false;
}
});
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
}
Tested and verified where "0" is the index of the tab to be long clicked:
tabHost.getTabWidget().getChildAt(0).setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "long click", 1).show();
return true;
}
});