How to use setCurrentTab method in an activity of tab? - android

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.

Related

How to make an activity under a tab scrollable programmatically?

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
}
}

Deliver intent for a tab activity to the main activity

I am writing a tab using SomeActivity extends TabActivity.
Here is the main process:
The main activity will call the SomeActivity. And the SomeActivity will put two activity (activityone and activitytwo) to the two tabs.
Here is my question, when the subactivity of the SomeActivity has finished, how could I pass the intent with some extra to the mainactivity?
Here is the main code of SomeActivity, which will be called by the MainActivity.
public class SomeActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(Activity.RESULT_OK);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabSpec spec;
Intent intent; // Reusable Intent for each tab
//TAB1
intent = new Intent(this,ActivityOne.class);
spec = tabHost.newTabSpec("tab1")
.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_media_play))
.setContent(intent);//
tabHost.addTab(spec);//
//TAB2
intent = new Intent(this,ActivityTwo.class);
spec = tabHost.newTabSpec("tab2")
.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_media_play))
.setContent(intent);//
tabHost.addTab(spec);//
tabHost.setCurrentTab(0);
}
Here is where I want to send an intent to the MainActivity.
public class ActivityOne extends Activity{
private void SendIntent()
{
Intent intent = new Intent();
intent.putExtra("information", "someinformation");
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
finish();
}
}

How to Show Second Tab in Android while calling from an Activity

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

Android getSupportActionbar() is Null when Activity is called from tabhost

I have a support action bar that works fine if I trigger the event with an intent manually. But if I leave it up to the tabhost to call it then the actionbar returned from getSupportActionbar() is null.
I've heard this referenced on Stack in another question, but no one has supplied and answer. (Apparently it only occurs on Android 3 and above). Does anyone have any ideas?
My tabhost:
public class NavTab extends TabActivity {
TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
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
// Initialize a TabSpec for each tab and add it to the TabHost
intent = new Intent().setClass(this, SummaryPage.class);
spec = tabHost.newTabSpec("Summary");
spec.setIndicator("Account", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
//Feedback
intent = new Intent().setClass(this, FeedbackPage.class);
spec = tabHost.newTabSpec("Feedback");
spec.setIndicator("Feedback", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
//Payment Locations
intent = new Intent().setClass(this, PaymentLocationsActivity.class);
spec = tabHost.newTabSpec("Payment Locations");
spec.setIndicator("Pay Loc", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
//Usage Alert
intent = new Intent().setClass(this, UsageAlertPage.class);
spec = tabHost.newTabSpec("Usage Alerts");
spec.setIndicator("Alerts", getResources().getDrawable(R.drawable.tab_icon_summary));
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
My activity
public class PageWithActionBar extends SherlockActivity implements ActionBar.OnNavigationListener {
private static String TAG = "mymeter-Main";
private List<Account> accounts = new LinkedList<Account>();
private LocationAdapter locationAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setDisplayShowTitleEnabled(false);
accounts.add(new Account("123456789", "4-15 Rose Rd", "Auckland 1021"));
accounts.add(new Account("0987654321", "49 Ronaki Rd", "Auckland 1043"));
locationAdapter = new LocationAdapter(this, accounts);
actionBar.setListNavigationCallbacks(locationAdapter, this);
}
}
Your TabActivity needs to extend SherlockActivity, check out the ActionBarSherlock samples on Tabs
TabNavigation
FragmentTabs

Problems with OnClickListener for a tab in android

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" />

Categories

Resources