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.
Related
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 :)
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.
I have been trying to solve this issue now for longer than I care to admit. I'm looking to set up an onClicklistener for my tabs so even if a user is on that tab and they click it the tab reloads. Could someone please point out my error, below is the code i'm using made up from examples from stack overflow so thanks so far! I'm using the getTabHost().setCurrentTab(3) to set it to be run only on the tab 3 but how would I get it so that it calls the specific tab the user clicks on?
I have been using this as a reference: OnClickListener on Tabs not working
public class DamTabs extends TabActivity implements OnClickListener{
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
Intent intent = new Intent(this, Featured.class);
tabHost.addTab(tabHost.newTabSpec("Tab 1")
.setIndicator("Featured", res.getDrawable(R.drawable.ic_tab_main))
.setContent(intent));
Intent intent2 = new Intent(this, Deals.class);
tabHost.addTab(tabHost.newTabSpec("Tab 2")
.setIndicator("Deals", res.getDrawable(R.drawable.ic_tab_setup))
.setContent(intent2));
Intent intent3 = new Intent(this, Events.class);
tabHost.addTab(tabHost.newTabSpec("Tab 3")
.setIndicator("Events", res.getDrawable(R.drawable.ic_tab_setup))
.setContent(intent3));
getTabWidget().getChildAt(3).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(sort)) {
getTabHost().setCurrentTab(3);
}
}
});
} //End of onCreate
If I understood your problem, I think you should use setOnTabChangedListener() method. Something like this:
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());
switch (mTabHost.getCurrentTab()) {
case 0:
//do what you want when tab 0 is selected
break;
case 1:
//do what you want when tab 1 is selected
break;
case 2:
//do what you want when tab 2 is selected
break;
default:
break;
}
}
});
And remove the implements OnClickListener.
getTabWidget().getChildAt(3).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(sort)) {
Events.reloadMe()
}
}
});
I would create a static method in the Activity you want to reload and call it like this. I think this is the easiest way to solve it. You will need some private static variables in the Activity to use this. And make sure these private static Objects aren't null.
I need to implement tabs in my project.i have a layout, in which 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 tab0 event and on button click event. 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 but again if i click on tab0, tab0 activity should be displayed.
i used onClick but after using this my TabChangeListner not works.
would you suggest solution for my problem.
Thanks.
Below is my code that is working fine :
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 need to use AvtivityGroup to open new activities in the same tab.
Here is the complete example.
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
Greetings,
I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.
Am i registering for the wrong View?
Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?
public class TestDroidViewTab extends TabActivity
implements TabContentFactory
, OnTabChangeListener, OnClickListener {
private static final String LOG_KEY = "TEST";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
ts.setIndicator("1");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_2");
ts.setIndicator("2");
ts.setContent(this);
tabHost.addTab(ts);
ts = tabHost.newTabSpec("ID_3");
ts.setIndicator("3");
ts.setContent(this);
tabHost.addTab(ts);
tabHost.setOnClickListener(this);
tabHost.setOnTabChangedListener(this);
}
public void onClick(View v) {
Log.d(LOG_KEY, "OnClick");
}
public void onTabChanged(String tabId) {
Log.d(LOG_KEY, "OnTabChanged");
}
If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.
The hierarchy of views in a tab implementation is:
TabHost
TabWidget
(tab)
(tab)
FrameLayout
The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));
You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);
In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.
Here is a full example, which lets you intercept the click event, and change the content below the tab:
final String myTabTag = "My Tab";
final int myTabIndex = 3;
getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );
getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
getTabHost().setCurrentTab(myTabIndex );
}
}
});
use setOnTabChangedListener instead of OnClickListener ;)
static TabHost tabHost;
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
}
});
Your clause is wrong, use:
...
if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
getTabHost().setCurrentTab(myTabIndex );
}
...
into my code, it shows some errors and ask me to create new methods in
those names like getTabWidget(), getTabHost(), etc. Waiting for your
response.
Try this
tabHost.getTabHost().setCurrentTab(myTabIndex);