Here is the Code which I'm Trying. How to go to Tab2 from Sample? Please help me to Solve this.
public class DemoTab extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_and_login_tab);
setTabs() ;
}
private void setTabs()
{
addTab("Tab1", R.drawable.tab_search, Tab1.class);
addTab("Tab2", R.drawable.tab_home, Tab2.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Code for Tab2
public class Tab2 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
}
Code for Tab1
public class Tab1 extends Activity implements OnClickListener {
Button check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
check= (Button) findViewById(R.id.check);
check.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.check:
//Do the Task Search Here
Intent i = new Intent(getApplicationContext(), Sample.class);
startActivity(i);
break;
}
}
}
Code for Sample
public class Sample extends Activity implements OnClickListener {
Button redirect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
redirect= (Button)findViewById(R.id.redirect);
redirect.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.redirect:
Here What should i do to get view of Tab2,By default the DemoTab's Current Tab will be Tab1.
Intent i = new Intent(getApplicationContext(), DemoTab.class);
startActivity(i);
break;
default:
break;
}
}
}
Just try like this...
when call the tabactivity call like this
Intent go=new Intent(this,TabActivity.class);
Bundle b=new Bundle();
b.putString("condition","tab2");
go.putExtras(b);
startActivity(go);
then in tabAcitivity class
public class Demo extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle b=getIntent().getExtras();
String con=b.getString("condition");
TabHost tab = getTabHost();
TabSpec tab1 = (TabSpec) tab.newTabSpec("tb1");
TabSpec tab2 = (TabSpec) tab.newTabSpec("tb2");
tab1.setIndicator("Tab1").setContent(
new Intent(this, Tab1.class));
tab2.setIndicator("Tab2").setContent(
new Intent(this, Tab2.class));
tab.addTab(tab1);
tab.addTab(tab2);
if(con.equals("tab2")
{
tab.setCurrentTab(1);
}
}
}
I have two Classes "One.Java" and "Two.Java". My main Activity which is extends 'TabActivity'. In onCreate() method :
Intent one = new Intent(this, One.class);
Intent two = new Intent(this, Two.class);
TabHost t = (TabHost) findViewById(R.id.tabhost);
t.setup();
TabSpec tspecOne = t.newTabSpec("CLICK ONE").setIndicator("CLICK ONE")
.setContent(one);
t.addTab(tspecOne);
TabSpec tspecTwo = t.newTabSpec("CLICK TWO")
.setIndicator("CLICK TWO").setContent(two);
t.addTab(tspecTwo);
setCurrentTab(index) opens the tab to be displayed by default, specified by the index position of the tab. If you set tabHost.setCurrentTab(1); property while creating your TabActivity, it will show the tab on 1st index as you desire( 0 will set it to a tab on 0th index). You might want to check some of the above questions as well:
android setting default tab in tab-activity
Start Android App in a specific tab
setCurrentTab of a tabHost
Edit
Android TabHost setCurrentTab()
public class DemoTab extends TabActivity {
//Declare this outside the elements
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
}
private void setTabs()
{
// ...
addTab("Tab1", R.drawable.tab_search, Tab1.class);
addTab("Tab2", R.drawable.tab_home, Tab2.class);
// Here you may try adding
tabHost.setCurrentTab(1);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
tabHost = getTabHost(); v//edit this line
//....
}
Hope this helps
Related
i am developing an application where in i am displaying a tabbed layout on click of a button. now i want all my tabs to be scrollable. any help would be appreciated.
Here is my code
AndroidTabLayoutActivity class :
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, MyInfo.class);
spec = tabHost.newTabSpec("MyInfoTab").setIndicator("MyInfo",
res.getDrawable(R.drawable.myinfo))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Notes.class);
spec = tabHost.newTabSpec("NotesTab").setIndicator("Notes",
res.getDrawable(R.drawable.notes))
.setContent(intent);
tabHost.addTab(spec);
}
}
MyInfo.class
public class MyInfo extends Activity {
ScrollView scroll;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myinfo);
}
}
Notes.class
public class Notes extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
// TODO Auto-generated method stub
}
}
In my app, I have one tab which includes two activities say 1 and 2. Activity 1 extends ActivityGroup, which has one child activity say A. Now scenario becomes like this,
In first tab --> Activity 1 and its child activity A.
In second tab --> Activity 2. After doing some functionality in Activity 1 of first tab, user moves to child activity A of first tab. Now what I want to do, when user presses back button from child activity A, user should move back to its parent activity 1. How to do that? Below is my code..
MainActivity.java
public class MainActivity extends TabActivity {
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources ressources = getResources();
tabHost = getTabHost();
Intent intentProfile = new Intent().setClass(this, Tab1.class);
TabSpec tabProfile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentProfile);
Intent intentFriends = new Intent().setClass(this, Tab2.class);
TabSpec tabFriends = tabHost
.newTabSpec("Friends")
.setIndicator("Friends", ressources.getDrawable(R.drawable.ic_launcher))
.setContent(intentFriends);
tabHost.addTab(tabProfile);
tabHost.addTab(tabFriends);
tabHost.setCurrentTab(0);
}
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
#Override
public void onBackPressed() {
// Called by children
MainActivity.this.finish();
}
}
Tab1.java
public class Tab1 extends ActivityGroup {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Tab1.this, Second.class);
replaceContentView("activity3", in);
}
});
}
public void replaceContentView(String id, Intent newIntent)
{
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
}
Second.java
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Tab2.java
public class Tab2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
}
#Override
public void onBackPressed() {
// this.getParent().onBackPressed();
MainActivity parentTab = (MainActivity) this.getParent();
parentTab.switchTabBar(0);
}
}
Here I can move to tab 1 from tab 2 on back button pressed. But, I can't move back to parent activity from child activity of tab 1.
You have to override back button in parent activity and make a call from child. For more info please check this link : Navigate Between Activities in an ActivityGroup
In Tab1:
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public void onPause()
{
super.onPause();
finish();
}
in Tab1
I've been having this problem for 1 week.
I have a MainActivity extends TabActivity and A, B two activities in each tab.
Now I want to press a button in activity A to set the current page to the tab of activity B
But I can't use setCurrentTab method except in MainActivity.
How can I make this function work?
public class MainActivity extends TabActivity
public TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, StartActivity.class);
spec = tabHost.newTabSpec("tab1").setIndicator("A",res.getDrawable(R.drawable.start))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, WeatherActivity.class);
spec = tabHost.newTabSpec("tab2").setIndicator("B",res.getDrawable(R.drawable.weather))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
public class AActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_layout);
Button b = (button)findViewById(R.id.bt);
b.setOnClickListener(this);
}
public void onClick(View v) {
tabHost.setCurrentTab(1);
//Can't work here
}
}
Try making the following changes :
In Main activity :
Make the tabHost variable Static.
private static TabHost tabHost;
Add a new function to fetch the current tabhost.
public static TabHost getCurrentTabHost(){
return tabHost;
}
In AActivity, Use like this :
MainActivity.getCurrentTabHost().setCurrentTab(1);
If required, you can go for a null check.
I have a tabhost and I add there 3 Activities(one activity per tab).
I need to know how to call a new intern in an activity each time i change a tab.
I added a listener for the tabhost.When I use the clearAllTabs(); method and add all the tabs again inside the listener then the app crash.
when I use code toto delete from the view the specific tab that the user clickes tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(i));
tabHost.addTab(the tab I want to replace);
then the new tab is positioned in the end of the tabhost.
I just need an example of how to reload the proportionate activity each time the user clickes the specific tab.
my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ActionBar bar = getSupportActionBar();
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Resources res = getResources();
LocalActivityManager mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam);
TabHost.TabSpec spec;
Intent intent;
// TabHost tabHost = getTabHost();
// tabHost.setup();
TabSpec specAll = tabHost.newTabSpec("All");
specAll.setIndicator("All");
Intent allIntent = new Intent(this, allActivity.class);
specAll.setContent(allIntent);
// specAll.setContent(R.id.allList);
Log.d("SpecAll",""+specAll.setContent(allIntent));
TabSpec specIn = tabHost.newTabSpec("in");
specIn.setIndicator("In");
Intent inIntent = new Intent(this, inActivity.class);
specIn.setContent(inIntent);
TabSpec specOut = tabHost.newTabSpec("Out");
specOut.setIndicator("Out");
Intent outIntent = new Intent(this, outActivity.class);
specOut.setContent(outIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(specAll); // Adding all tab
tabHost.addTab(specIn); // Adding in tab
tabHost.addTab(specOut); // Adding out tab
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
int i = tabHost.getCurrentTab();
//Log.i("######## ANN CLICK TAB NUMBER", "------" + i);
if (i == 0) {
Log.d("TAB","" +i);
} else if (i == 1) {
Log.d("TAB","" +i);
}
else
Log.d("TAB", ""+i);
}
});
}
It seems there is a matter with activity and tabhost.in order to reload an activity you just have to do:
specAll.setContent(yourIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Just before the tabHost.addTab
In my simple case when I do not need to persist tab fragments I use the next code
int currentTabId = mTabHost.getCurrentTab();
mTabHost.clearAllTabs();
setupTabs();
mTabHost.setCurrentTab(currentTabId);
I also face the same problem but i resolve this issue like this way...
This is my TabActivity....
public class MainActivity extends TabActivity {
TabHost tabhost;
String cTab = "0";
String nTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
tabhost = getTabHost();
TabSpec one = tabhost.newTabSpec("0");
// setting Title and Icon for the Tab
one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent = new Intent(this, FirstActivity.class);
one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
one.setContent(songsIntent);
TabSpec two = tabhost.newTabSpec("1");
// setting Title and Icon for the Tab
two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent1 = new Intent(this, SecondActivity.class);
two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
two.setContent(songsIntent1);
TabSpec three = tabhost.newTabSpec("2");
// setting Title and Icon for the Tab
three.setIndicator("",
getResources().getDrawable(R.drawable.ic_launcher));
Intent songsIntent4 = new Intent(this, ThirdActivity.class);
three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
three.setContent(songsIntent4);
TabSpec four = tabhost.newTabSpec("3");
// setting Title and Icon for the Tab
four.setIndicator("", getResources()
.getDrawable(R.drawable.ic_launcher));
Intent songsIntent5 = new Intent(this, FourthActivity.class);
four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
four.setContent(songsIntent5);
tabhost.addTab(one);
tabhost.addTab(two);
tabhost.addTab(three);
tabhost.addTab(four);
tabhost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
cTab = "" + tabhost.getCurrentTab();
}
});
int numberOfTabs = tabhost.getTabWidget().getChildCount();
for (int t = 0; t < numberOfTabs; t++) {
tabhost.getTabWidget().getChildAt(t)
.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
String currentSelectedTag = MainActivity.this
.getTabHost().getCurrentTabTag();
nTab = currentSelectedTag;
System.out.println(" nTab " + nTab);
System.out.println(" cTab " + cTab);
if (cTab.equals(nTab)) {
if (nTab.equals("0")) {
Intent intent = new Intent();
intent.setAction("com.ensis.first");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("1")) {
Intent intent = new Intent();
intent.setAction("com.ensis.second");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("2")) {
Intent intent = new Intent();
intent.setAction("com.ensis.third");
MainActivity.this.sendBroadcast(intent);
}
if (nTab.equals("3")) {
Intent intent = new Intent();
intent.setAction("com.ensis.fourth");
MainActivity.this.sendBroadcast(intent);
}
}
}
return false;
}
});
}
}
#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;
}}
This is my FirstActivity.java
public class FirstActivity extends ActivityGroup{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ok();
IntentFilter filter = new IntentFilter("com.ensis.first");
registerReceiver(myReceiver, filter);
/**/
}
private void ok() {
// TODO Auto-generated method stub
setContentView(R.layout.firstscreen);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it=new Intent(FirstActivity.this,SubActivity.class);
replaceContentView("activity3", it);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
Log.v("22222222222222222", "22222222222");
ok();
}
};}
I'm new to android programing and I'm having problems with OnClickListener for tabs in my app. I found on stack a solution how it should be done, but for some reason it's not working.
I'm trying to use the 2nd answer
For some reason I'm getting 2 errors.
First one is on the name on of my activity: The type DragonLords must implement the inherited abstract method View.OnClickListener.onClick(View).
Second one is on the OnClick method: The method onClick(View) of type new View.OnClickListener(){} must override a superclass method.
Here is a part of my code:
public class DragonLords extends TabActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
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, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("home",
res.getDrawable(R.drawable.hometab))
.setContent(intent);
tabHost.addTab(spec);
getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
After that I'm creating more tabs. With out the onclicklistener it's working, the thing is I need to be able to reload the tabs when they are active.
Anyone have an idea what I'm doing wrong?
I added the necessary imports.
Gatz
You must implement the onClick method not in an anonymous inner class like you have done in your code.
Try using new TabWidget.OnClickListener instead of just the normal OnClickListener
Something akin to the following:
public class TestActivity extends TabActivity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getTabWidget().getChildAt(0).setOnClickListener(new TabWidget.OnClickListener() {
public void onClick(View v) {
if (getTabHost().getCurrentTab()==0) {
getTabHost().setCurrentTab(0);
}else
{
getTabHost().setCurrentTab(0);
}
}
});
}
public void onClick(View theView) {
// Do something with view here
}
}
you are doing it pretty hard way.. i did it like this...
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tablayout);
res = getResources();
tabHost=(TabHost)this.findViewById(android.R.id.tabhost);
tabHost.getTabWidget().setDividerDrawable(R.drawable.vertical_seperator);
setupTab(new TextView(this), "Login",new Intent().setClass(this,loginForm.class));
setupTab(new TextView(this), "Can't Login",new Intent().setClass(this,ForgotPwd.class));
setupTab(new TextView(this), "Register",new Intent().setClass(this,RegisterUser.class));
}
private void setupTab(final View view, final String tag,final Intent myIntent)
{
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(
new TabContentFactory()
{
public View createTabContent(String tag)
{return view;}
}).setContent(myIntent);
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
hope this helps....
tabs_bg is just an xml with
<TextView android:id="#+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="15dip"
android:textColor="#drawable/tab_text_selector" />