Android onclicklistener for tabs - android

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.

Related

android bottom bar setting default tab on create of an activity

Myself trying to develop a sample android app based on this tutorial with bottom bar.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdactivity);
BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
Intent myAct = new Intent();
switch (itemId) {
case R.id.item1:
myAct = new Intent(findViewById(itemId).getContext(), mainactivity.class);
break;
case R.id.item2:
myAct = new Intent(findViewById(itemId).getContext(), secondactivity.class);
break;
case R.id.item3:
myAct = new Intent(findViewById(itemId).getContext(), thirdactivity.class);
break;
}
startActivity(myAct);
}
});
}
But how can i set the third tab as default oncreate the activity. The above code highlights the first tab as selected and no even listened when clicking on first tab. Also later tabs opens the respective activities but not highlights as current tab.
EDIT: Myself can use bottomBar.setDefaultTabPosition(desiredTabId); and it working but it uses high memory. What is the fix?
It is solved by having startActivity() as,
BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
bottomBar.setItemsFromMenu(R.menu.bottom_menu, new OnMenuTabSelectedListener() {
#Override
public void onMenuItemSelected(int itemId) {
Intent act = new Intent();
if (R.id.item1 == itemId) {
act = new Intent(findViewById(itemId).getContext(), act1.class);
startActivity(act);
overridePendingTransition(R.anim.open_translate, R.anim.close_scale);
}
if (R.id.item2 == itemId) {
act = new Intent(findViewById(itemId).getContext(), act2.class);
}
if (R.id.item3 == itemId) {
act = new Intent(findViewById(itemId).getContext(), act3.class);
startActivity(act);
overridePendingTransition(R.anim.open_translate, R.anim.close_scale);
}
}
});
bottomBar.setDefaultTabPosition(2);
bottomBar.setActiveTabColor("#F3C030");
Try setting the default tab before your set the listener
bottomBar.setDefaultTab(R.id.tab_default);
bottomBar.setOnTabSelectListener(this);
This applies for version 2.0.2
If you set the listener before the default tab, it will be called twice.
Once for tab position 0 and then for anything you set as default, which is usually not desired.
Would be nice to set the default tab before the the creation of bottombar, or a warning in the readme.

How do you make a new activity start when a button is clicked?

I already know how to start a one new activity when you click a button, but I have three buttons on the one layout. And I want each of the three buttons on that ONE activity to link to three other activities.
I have on the activity I have called 'main', Button 1 which is called services and I want to link it to the services activity. Button 2 which is called Search and I want it to go to the Search activity and thirdly 'map' which I want to link to the map activity.
Can someone help me do this please? Thanks
EDIT:Also, I'm a beginner with Android coding, could you explain in a little bit more detail please?
So where is problem? Just set that your class will implements View.OnClickListener and override onClick() method a you got it.
public class MainActivity extends Activity implements View.OnClickListener {
// body
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.serviceBtn:
Intent serviceIntent = new Intent(this, ServiceActivity.class);
startActivity(serviceIntent);
break;
case R.id.searchBtn:
Intent searchIntent = new Intent(this, SearchActivity.class);
startActivity(searchIntent);
break;
case R.id.mapBtn:
Intent mapIntent = new Intent(this, MapActivity.class);
startActivity(mapIntent);
break;
}
}
Tie an onclick listener to all three buttons. In the listener, retrieve the ID of the button. Make a variable of type Class. Depending on the value of the button ID, initialize it to the class of the activity to invoke. Then construct an Intent for that class, and call startActivity().
EDIT for hawaii.five-0: here's how I'd do it:
#Override
public void onClick(View view) {
Class c = null;
switch (view.getId()) {
case R.id.serviceBtn:
c = ServiceActivity.class;
break;
case R.id.searchBtn:
c = SearchActivity.class;
break;
case R.id.mapBtn:
c = MapActivity.class;
break;
}
Intent i = new Intent(YourActivity.this, c);
startActivity(i);
}
EDIT2:
class CurrentActivity extends Activity
implements OnClickListener
{
void onCreate(Bundle b)
{
//Other initialization goes here...
((Button)findViewById(R.id.MyButton1)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton2)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton3)).setOnClickListener(this);
}
}
Its very easy dear. Just put this code on button click event
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
which is very easy to start a new activity when the button is clicked. I have given the example for this.
// R.id.btnAdd -> which is present in your layout page
Button btnStart = (Button) findViewById(R.id.btnAdd); // declare button
// declare listener evernt for button
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
// declare the Intent for moving another activity
Intent view = new Intent(YourCurrentClassName.this,
anotherClassName.class);
// startActivity is used to navigating the view
startActivity(view);
}
};
// set the listener evernt to button
btnStart.setOnClickListener(listener);

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.

How to call activity for the same tab which is currently active

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

OnClickListener on Tabs not working

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

Categories

Resources