hi can you tell me how to disable a tab in the UI of android code.. (eclair code)
If you mean to disable one tab button on TabWidget, then try this code:
// tabHost = ... (get TabHost)
tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);
If you want to disable tab widget in overall, then:
// tabWidget = ... (get TabWidget)
tabWidget.setEnabled(false);
Read SDK Help for references:
TabHost
TabWidget
Extend TabHost and override methods:
#Override
public void setCurrentTab(int currentTab) {
if (currentTab != 2) // position of the tab that should not get selected
super.setCurrentTab(currentTab);
else
// in my case I want to trigger something here but I don't want the button to get selected
}
#Override
public void setCurrentTabByTag(String tag) {
if (!"\"plus_tab\"".equals(tag)) // tag of the tab that should not get selected
super.setCurrentTabByTag(tag);
else
// in my case I want to trigger something here but I don't want the button to get selected
}
Related
so i have a standard tabLayout that is in a viewpager.
But the very last tab needs to open a new activity. its only purpose is to open a new activity. and when the user returns from the activity by pressing the back button the last tab the user was at should be selected.
my tabs are custom views and there are 4 of them but the last one should be a button to trigger an event. so it will look just like a tab but really will be a button with a onclick listener. how can i acheive this ? i am wondering if a framelayout could be used and activated for the very last tab icon . thats the only way i can see to do it is to get the custom view and set its parent to be clickable that way it absorbs the click event. then i can set a onclick listener on the parent to open the activity. but then how to handle swipping to that tab ? swipping should open that activity as well.
Assuming you're using TabLayout.setupWithViewPager(), you can accomplish what you want by simply adding an extra tab and changing the OnTabSelectedListener.
// normal setup
tabs.setupWithViewPager(pager);
// our extra tab
tabs.addTab(tabs.newTab().setText("extra"));
// remove the `OnTabSelectedListener` created by `setupWithViewPager()`
tabs.clearOnTabSelectedListeners();
// add our own `OnTabSelectedListener`
tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == pager.getAdapter().getCount()) {
// special case for the last tab in the list
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
else {
// otherwise, handle as normal
super.onTabSelected(tab);
}
}
});
I'm using scrollable tabs with FragmentPagerAdapter and I need to switch between the tabs programmatically
After clicking the create group button, I need to switch to the next tab i.e. manage group. How to do that?
For just moving 1 step ahead you can do:
int current = mPager.getCurrentItem();
mPager.setCurrentItem(current + 1, true);
If you want to scroll to any specific index you can just pass that too:
mPager.setCurrentItem(index, true);
You can also set smoothScroll flag as true/false which will give you smooth transition effect.
EDIT:
In your activity make a method :
public void scrollPager(int index) {
if (mPager != null) {
mPager.setCurrentItem(index, true);
}
}
From your fragment when you hit the button you can either create a call back or simply call activity's method:
((ActivityName) getActivity()).scrollPager(1);
I want to have a tabbar with tabs. And I want just to catch tab pressing to update single view. I attempt to set setContent of TabHost to null but I've got error. How to implement this feature?
You cannot enter null to setContent in TabHost. One thing you can do is, create a dummy class and set that intent inside setContent() method for the particular tab that you just want to update view as you say and you can update your view by setting on click listener to that child in your tab activity as below
getTabWidget().getChildAt(3).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Update your view here
});
}
here you can pass the tab number inside getChildAt() function, e.g. I have set it on 4th tab, being child number 3, as it starts count from 0.
I have an activity with five tabs. Everything looks okay when I go from tab 1 to tab 2 or tab 3. How can I go back programatically from tab 2 to tab 1?
Intent myIntent = new Intent(this, Tab1.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
This is not working properly because it starts activity 1 without any tab.
When going from tab 1 to tab 2 I can both see tab 1 and tab 2 (current tab activated). But when going from tab 2 to tab 1, both tab 1 and tab 2 disappear from the activity. What could cause this?
This will surely help you.
TabHost tabHost = (TabHost) getParent().findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);
OR you can refer to this link
How to programmatically switch tabs using buttonclick in Android
Thanks :)
just use finish() method
public void onClick(View v)
{
finish();
startActivity(new Intent(Activity2.this, Activity1.class));
}
I don't know about the Intent.FLAG_ACTIVITY_CLEAR_TOP, never needed that, but the mentioned effect of loosing your tabs is produced by calling startActivity() from your TabHost, not one of your tabs. If that's the case, move the call there and your tabs should stay.
I have a similar situation but seems none of the answers help. so, I post my solution here:
// tab selection history, each tab has a tag which is a string
private List<String> tabIdHistory = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
// this layout contains TabHost and TabWidget
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
tabIdHistory.remove(tabId); // ensure uniqueness
tabIdHistory.add(tabId);
}
});
// continue your tab initialisation, such as
// tabHost.addTab(tabHost.newTabSpec(TAG)
// .setContent(...).setIndicator(...));
}
#Override
public void onBackPressed() {
if (tabIdHistory.size() > 1) {
// pop the current last item, we want the second last
tabIdHistory.remove(tabIdHistory.size() - 1);
tabHost.setCurrentTabByTag(tabIdHistory.get(tabIdHistory.size() - 1));
} else {
super.onBackPressed();
}
}
If use select tab#1, tab#3, tab#2, tab#1, then the back stack is "3, 2, 1" and app will exit to main screen if user press back button three times. If you want to keep full history, comment out this line:
tabIdHistory.remove(tabId);
I have used tabbar with activity group in my application. I have four tab like home, stock, citn, article. In my application first display home page from the home page user click in webview it will go to homepage1 activity. From home page1 activity user click stock tab it will go to stock activity. From the stock activity user click home tab it will go to homepage1 activity. I want to display home activity can any body tell how to do?
My question is switching between tab using activity group it will display last activity. I want to display first activity?
ok i will attach my code
spec = tabHost.newTabSpec("FirstGroup").setIndicator("FirstGroup",
getWallpaper()).setContent( new Intent(this,FirstGroup.class));
tabHost.addTab(spec);
View view =
getLocalActivityManager().startActivity("CitiesActivity",
new
Intent(this,CitiesActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
)).getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
run this example
http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity
switching between activity and tab
I have posted in pastebin, my link is
http://pastebin.com/1zG0HJgv
Hi Did u tried the tabchanged event as shown below
tabHost.addTab(tabHost.newTabSpec("tab1").setContent(
R.id.content_movies).setIndicator("",
getResources().getDrawable(R.drawable.icon)));
tabHost.addTab(tabHost.newTabSpec("tab2").setContent(
new Intent(this, Sample.class)).setIndicator("",
getResources().getDrawable(R.drawable.menu_icon)));
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
if(arg0.equals("tab1"))
{
/*write the code here to show the view
Currentclass, the class where you have used ontabchanged function and
Newclass is the class where you want to navigate*/
Intent obj_intent = new Intent(CureentClass.this,Newclass.class);
startActivity(obj_intent);
}
else if (arg0.equals("tab2")) {
// write the code here to show the view
}
//similarly for other tabs
});