one activity to Tabhost Activity with current item - android

I am creating a android app.
which have a welcome screen where four button (a,b,c,d) have.
when click on the any button(a,b,c,d) its goes to the second activity.
next activity have (tabhost)four tabs(a,b,c,d).
how can it work--
when i click on "a" button in welcome screen it goes to "a tab" of second activity,and another tabs are also working.
when i click on "b" button in welcome screen it goes to "b tab" of second activity,and another tabs are also working.
when i click on "c" button in welcome screen it goes to "c tab" of second activity,and another tabs are also working.
when i click on "d" button in welcome screen it goes to "d tab" of second activity,and another tabs are also working.
public class Dashboard extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
}
public void helpB(View v) {
Button clickedButton = (Button) v;
switch (clickedButton.getId()) {
case R.id.points:
Intent i = new Intent(getApplicationContext(),AndroidActivity.class);
startActivity(i);
break;
case R.id.Search:
Intent i1 = new Intent(getApplicationContext(),AppleActivity.class);
startActivity(i1);
break;
case R.id.AboutUs:
Intent i2 = new Intent(getApplicationContext(),BlackBerryActivity.class);
startActivity(i2);
break;
case R.id.ContactUs:
Intent i3 = new Intent(getApplicationContext(),WindowsActivity.class);
startActivity(i3);
break;
}
}
}
and second Activity code -->
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this,
AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Points",
ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Search",
ressources.getDrawable(R.drawable.icon_apple_config))
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this,
WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setIndicator("About us",
ressources.getDrawable(R.drawable.icon_windows_config))
.setContent(intentWindows);
// Blackberry tab
Intent intentBerry = new Intent().setClass(this,
BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setIndicator(
"Contact us",
ressources
.getDrawable(R.drawable.icon_blackberry_config))
.setContent(intentBerry);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);
// set Windows tab as default (zero based)
tabHost.setCurrentTab(0);
}
}

Do you need this?
public void helpB(View v) {
Button clickedButton = (Button) v;
int tab = 0;
Intent i = new Intent(getApplicationContext(),MainActivity.class);
switch (clickedButton.getId()) {
case R.id.points:
tab = 0;
break;
case R.id.Search:
tab = 1;
break;
case R.id.AboutUs:
tab = 2;
break;
case R.id.ContactUs:
tab = 3;
break;
}
i.putExtra("extra.tab", tab);
startActivity(i);
}
And MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this,
AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("Points",
ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("Search",
ressources.getDrawable(R.drawable.icon_apple_config))
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this,
WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setIndicator("About us",
ressources.getDrawable(R.drawable.icon_windows_config))
.setContent(intentWindows);
// Blackberry tab
Intent intentBerry = new Intent().setClass(this,
BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setIndicator(
"Contact us",
ressources
.getDrawable(R.drawable.icon_blackberry_config))
.setContent(intentBerry);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);
// set Windows tab as default (zero based)
int tab = getIntent().getExtras().getInt("extra.tab");
tabHost.setCurrentTab(tab);
}

Related

Android tab listener wont refresh on selected tab

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();
}
}

Activity isn't called when clicking on a tab for the second time

I am developing one application and in which i have to display a five tabs and on each tab i am calling a different activity.And in my last tab i displaying menus.
The main problem is that my activities are called only on tab changed events.when i am clicking on last tab first time it displays a menu and after selecting any menu if i click on that tab again then nothing happens.
So how to solve this kind of problem? is there any other way to do this task? Any help will be appreciated.Thanks
Here is my code:
public class More extends ActivityGroup{
Button btn1;
ExpandableListView elw;
Context ctx;
public ListView modeList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx=this;
Toast.makeText(More.this,"in more",200).show();
setContentView(R.layout.more);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("More Option");
modeList = new ListView(this);
String[] stringArray = new String[] { "About", "Settings","Invite By Message","Privacy Policy","Deactivate Account","Exit" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();
modeList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String item=(String) modeList.getItemAtPosition(position);
Toast.makeText(More.this,item,200).show();
switch(position) {
case 0:
setContentView(R.layout.about);
dialog.dismiss();
TabHost tabHost = welcome.self.getTabHost();
tabHost.setCurrentTab(4);
// tabHost.setCurrentTab(4);
break;
case 1:
setContentView(R.layout.settings);
dialog.dismiss();
break;
case 2:
setContentView(R.layout.messages);
dialog.dismiss();
break;
case 3:
setContentView(R.layout.policy);
dialog.dismiss();
break;
case 4:
Toast.makeText(More.this,"Deactivate the Account",200).show();
dialog.dismiss();
break;
default:
Toast.makeText(More.this,"Exit the application",200).show();
dialog.dismiss();
welcome.self.finish();
}
}
});
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("TAB1");
tabs.addTab(spec1);
TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("TAB2");
tabs.addTab(spec2);
add this in your method, and for details take a look http://www.mkyong.com/android/android-tablayout-example/.
let me know if you have problem , click right if it is solved. Your activity must contain layout.
modeList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String item=(String) modeList.getItemAtPosition(position);
Toast.makeText(More.this,item,200).show();
switch(position) {
case 0:
setContentView(R.layout.about);
dialog.dismiss();
// TabHost tabHost = welcome.self.getTabHost();-> Intent abc;
// tabHost.setCurrentTab(4);---->abc.getClass(More.this , NewClass.class);
// tabHost.setCurrentTab(4);
break;
case 1:
setContentView(R.layout.settings);
dialog.dismiss();
break;
case 2:
setContentView(R.layout.messages);
dialog.dismiss();
break;
case 3:
setContentView(R.layout.policy);
dialog.dismiss();
break;
case 4:
Toast.makeText(More.this,"Deactivate the Account",200).show();
dialog.dismiss();
break;
default:
Toast.makeText(More.this,"Exit the application",200).show();
dialog.dismiss();
welcome.self.finish();
}
}
});
Now your NewClass.java
public class NewClass extends TabActivit{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newclass);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// Android tab
Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
.newTabSpec("Android")
.setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
.setContent(intentAndroid);
// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Apple")
.setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Windows")
.setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
.setContent(intentWindows);
// Blackberry tab
Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
.newTabSpec("Berry")
.setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
.setContent(intentBerry);
// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
}
}
here are 4 tabs you can use as many as you want.
Register in android manifest.
Make sure your tabhost in layout file has the id same as in your code.
click right if you accept my answer.

change from a tab to another

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);

move to other tab

I have an application with 3 tab (tab1, tab2, tab3) How can I move to the tab2 if a press a bottom in tab1.
I have this....
Resources res= getResources();
TabHost tabHost= getTabHost();
TabHost.TabSpec spec;
Intent intent;
// initialize a TabSpect for each tab and add it to the TabHost
intent= new Intent().setClass(this, StockMarket.class);
spec= tabHost.newTabSpec("1").setIndicator("Stock Market", res.getDrawable(R.drawable.grafica))
.setContent(intent);
tabHost.addTab(spec);
intent= new Intent().setClass(this, Info.class);
spec= tabHost.newTabSpec("2").setIndicator("Data", res.getDrawable(R.drawable.puzzle)).setContent(intent);
tabHost.addTab(spec);
intent= new Intent().setClass(this, Profile.class);
spec= tabHost.newTabSpec("3").setIndicator("Profile", res.getDrawable(R.drawable.toolbox)).setContent(intent);
tabHost.addTab(spec);
intent= new Intent().setClass(this, Graphic.class);
spec= tabHost.newTabSpec("Graphic").setIndicator("Graphic", res.getDrawable(R.drawable.chart)).setContent(intent);
tabHost.addTab(spec);
thanks
Try this, It was worked for me.
set a method to my main class, which extends TabActivity let's call it "MainActivity"
public TabHost getMyTabHost() {
return tabHost;
}
Then add my tab activity class;
MainActivity ta = (MainActivity) this.getParent();
TabHost t = ta.getMyTabHost();
t.setCurrentTab(0);
you can set tabs with int in setCurrentTab(0).
This might help you -
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = getTabHost().getCurrentTab();
Log.i("TAB NUMBER", + i);
}
});

How to keep the header of a Tab in a SubActivity?

i've got this Tabhost:
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
/** Initialization Tab1 */
intent = new Intent(this, Tab1.Class);
spec = tabHost.newTabSpec("Tab1").setIndicator("Tab1").setContent(intent);
tabHost.addTab(spec);
/** Initialization Tab2 */
intent = new Intent(this, Abstand.class);
spec = tabHost.newTabSpec("Tab2").setIndicator("Tab2").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
The content of Tab2 has some buttons, when one of these is clicked, it starts a SubActivity with:
final Intent i = new Intent(this, SubActivity.class);
final Bundle b = new Bundle();
this.mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
b.putString("key", "value");
i.putExtras(b);
startActivityForResult(i, REQUEST_CODE);
}
});
Within the SubActivity, the Tabhost isnt available anymore... so how can the Code be changed, showing the Tabhost in a SubActivity?

Categories

Resources