Dynamically change Tab Content of TabHost - android

In my case I have several Tabs within TabHost and 1st tab should be able change it's view without navigation from TabHost (to be able to immediately access other tabs).
Is it possible to dynamically change Tab view for TabHost?

I have the same situation, my solution : get current tab, and setOnClickListener.
Example:
View tab = (View) tabHost.getCurrentTabView();
tab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
finish();
}
});
cheers :)

Related

Start activity instead of fragment in tabhost

I want to know if there is a way that I start an activity instead of launching a fragment on tabhost tab select.
What I want to acomplish if to launch an activity where the user can write a post and then save, why I want this? because when I want to edit I launch an activity and I don't want to have 2 classes that do the same thing with a little difference, and also the tab on the bottom uses so much space that leave my form area tiny.
My code actually looks like
#Override
protected void onCreate(Bundle savedBundleState) {
mTabHost.setup(getBaseContext(), getSupportFragmentManager(), R.id.realtabcontent);
// This starts the HomeFragment
mTabHost.addTab(newTabSpec("tabHome", R.layout.tab_home), HomeFragment.class, null);
// This throws an error
mTabHost.addTab(newTabSpec("tabPost", R.layout.tab_post));
mTabHost.setOnTabChangedListener(this);
}
private TabSpec newTabSpec(String tag, int layout) {
View view = getLayoutInflater().inflate(layout, null);
return mTabHost.newTabSpec(tag).setIndicator(view);
}
#Override
public void onTabChanged(String tabId) {
if (tabId == "tabAddPublication") {
startActivity(new Intent(getBaseContext(), PostActivity.class));
}
}
I got this error
you must specify a way to create a tab content
Regards
From the Documents
public TabHost.TabSpec setContent (Intent intent)
Specify an intent to use to launch an activity as the tab content.
You need to set the content for TabSpec
Intent postActivityIntent = new Intent(this, PostActivity.class);
mTabHost.newTabSpec(tag).setContent(postActivityIntent);
Hope this helps.

Keeping tabhost while switching intent from button click

I am adding a new tab layout to my app. My app has 4 activities.
Before, the navigation was done with buttons displayed in activities, for example a button in activity 1 make you go to section 4. Each button was starting a new activity with a new intent. To go back user could hit his native device back button.
Example of button:
Button b1= (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, ActivityTab4.class);
startActivity(i);
}
});
Now, with the tabhost, user goes straight to desire activity, each activity is a child of my tabhost. However, I still need in my layout to keep some buttons that jump directly to a particular activity.
The problem, with these buttons, is that when they start a new activity, the tabhost disappears. I need to keep it at all time.
So how can I use the tabhost normally, but on the top of it also use custom buttons inside the layout of my sections that would keep the tabhost when I hit them?
My tabhost structure is very basic:
public class TabWidget extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.global_tabs);
setTabs() ;
}
private void setTabs() {
addTab("Act1", R.drawable.tab1, ActivityTab1.class);
addTab("Act2", R.drawable.tab2, ActivityTab2.class);
addTab("Act3", R.drawable.tab1, ActivityTab3.class);
addTab("Act4", R.drawable.tab2, ActivityTab4.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);
// SET TITLETAB
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.titleTab);
title.setText(labelId);
// SET IMAGETAB
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}}
Thanks for your help
EDIT: I would rather avoid fragments because it's going to take me a lot of time to implement them and on the top of that to make it compatible with API<11
All right, I've found it, thanks to this answer: setCurrentTab of a tabHost
BAsically in the main tabhost activity, add this
private static Main theInstance;
public static Main getInstance() {
return Main.theInstance;
}
public Main () {
Main.theInstance = this;
}
In the child, you can have a button sending the user to any tab + corresponding activity:
Button b1_3= (Button) findViewById(R.id.b1_3);
b1_3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabWidget.getInstance().getTabHost().setCurrentTab(3);
}
});
Any reason why you use tabhost and activities instead of Fragments? I think you would find Fragments more suited to what you're trying to do.
As for your question - you can switch the displayed tab with
void setCurrentTab(int index)
void setCurrentTabByTag(String tag)
so there's no need to manually starting an activity on button press, just switch the displayed tab using these methods.

making more than one activity in a tab

I really want to move from an activity within a tab view to another activity within the same tab.
public void onClick (View view) {
// This creates an intent to call the 'Called' activity
i1 = new Intent(this.getBaseContext(),Called.class);
// calls the method to replace View.
replaceContentView("Called", i1);
}
try this...
TabSpec spec=...//our tabSpec
Intent intent = new Intent(this.getBaseContext(), Called.class);
spec.setContent(videosIntent);

How to call tab Onclick and OnTabChange for same tab

In my project, I have two tabs and a button. For two tabs,I have two activities and button calling different activity. the thing is I am showing result of button on first tab. i.e tab0 is active on tab0Event and on button click event too. And am able to change the tab events using tabHost.setOnTabChangedListener, but now what i further want is, say suppose i click on button so now button view is displaying(tab0 is active) but again if i click on tab0, tab0 activity should be displayed.
I tried many solutions for clicking on tab, one is
getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener
(new View.OnClickListener() {
#Override public void onClick(View v) {
System.out.println(getTabHost().getCurrentTab());
} });
But when i used this code with tabChnageListner, tab change not working and i got very unexpected results.
Would you please suggest solution for my problem.
Thanks.
code that is working for tab changed is as: (working fine for tab change, need to add Tab Onclick in it)
public class TabLayoutUsingTabChangeEventActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
final TabHost.TabSpec sp1 = tabHost.newTabSpec("TAB1");
TabHost.TabSpec sp2 = tabHost.newTabSpec("TAB2");
//Creating First Tab
Intent intent1 = new Intent(this, Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.addTab(sp1);
//Creating Second Tab
Intent intent2 = new Intent(this, Tab2Activity.class);
sp2.setIndicator("TAB2").setContent(intent2);
tabHost.addTab(sp2);
//Tab Changed Event
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
Log.i("TabId :", tabId);
if(tabId.equals("TAB2")){
Log.i("TAB1", "TAB1 Changed");
Intent intent1 = new Intent().setClass(getApplicationContext(), Tab1Activity.class);
sp1.setIndicator("TAB1").setContent(intent1);
tabHost.setCurrentTab(0);
}
}
});
Button addNewButton = (Button)findViewById(R.id.add_new_ticket_btn);
addNewButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent in = new Intent().setClass(getApplicationContext(), AddNewTicketActivity.class);
sp1.setContent(in);
tabHost.setCurrentTab(0);
//startActivity(in);
}
});
}
}
You can implement this listener:
host.setOnTabChangedListener(new OnTabChangeListener()
{
#Override
public void onTabChanged(String tabId)
{
if (getTabHost().getCurrentTabTag().equals("tab0"))
{
host.getCurrentTabView().setOnTouchListener(
new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
if (getTabHost().getCurrentTabTag().equals("tab0")
{
getTabHost().setCurrentTabByTag("tab1");
getTabHost().setCurrentTabByTag("tab0");
}
return false;
}
return false;
}
});
}
}
});
Also when you add tabs set tag for every tab:
host.addTab(host.newTabSpec("tab0"));
host.addTab(host.newTabSpec("tab1"));
You can used Fragment to layout
You cant. Views in TabWidget already have onClickListener and it is required fo normal workflow. If you remove it you will break TabWidget's logic.
When you set your onClickListener you remove previuos onClickListener and you break TabWidget logic.
Usually in such cases compound click listener is created (a click listener which handles click event and calls another click listener). But not in your case because there is no way to get old click listener because View doesn't have getOnClickListener method.
This is TabWidget source code. All related values are private... so we can't fix anithing from this side.
The only way to achieve your goal is a hack with Reflections because they allows to read private vars. So before set View's new onlick listener you should get old one (using Reflections), then create compound onClickListener which will do your event handling code and then call old onClickListener. Set compound click listener to the View.

replace view in activitygroup with slide in animation in android

I am having following code :
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnSrch){
Intent i = new Intent(this, SearchByCode.class);
View view = SearchGroup.group.getLocalActivityManager()
.startActivity("SearchByCode",i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
SearchGroup.group.replaceView(view);
}
}
But when I am clicking the button it shows the next screen abruptly. I want to show the slide-in animation here.
I can't quite tell but I think you want to know how to launch another activity called SearchByCode from your current activity.
To do this you need to create an Intent object, optionally add flags or bundles to the intent, then call startActivity(Intent).
Here is a code sample:
Intent i = new Intent(this, SearchByCode.class)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(i);

Categories

Resources