Disable/enable a tab on Android depending on condition - android

I have a Tabhost Activity with 3 child Activities Tab1 Tab2 and Tab3, i want to disable one of the 3 tabs if a condition is true, if not it should be enabled. this is how proceeded :
//tab is my TabHost,
tab.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Only logged users may have access to my Tab 3
if (v==tab.getTabWidget().getChildAt(2))
{
if(!LoginManager.getInstance().isLoggedIn())
tab.getTabWidget().getChildTabViewAt(2).setEnabled(false);
else
tab.getTabWidget().getChildTabViewAt(2).setEnabled(true);
}
}
});
Apparently my approach is not right, that's why i need you, to help me solve this problem.

Related

How to change the selected tab in a tabLayout programmatically

I have a tab layout with a view pager and 5 different tabs, if the user is not registered only one option is available, so I want to disable the click on the other tabs. What I did is override the onTabSelected to change the current item in the viewPager.
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (User.current != null) {
viewPager.setCurrentItem(tab.getPosition());
} else {
viewPager.setCurrentItem(0);
}
}
It works perfectly but it has one problem, the tab indicator change to the selected tab, so I want to keep the tab indicator on the first tab.

android tab activity go to previous tab

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

How do I make all tabs in a TabHost unselected

I need to show tab content on occasion, otherwise the area must be filled with "non-tabhost" data.However, tabs should be visible and when user clicks any of those tabs "non-tabhost" must be hidden and appropriate tab content must become visible.
It's something connected to a fake tab creation ?
Give an example of creating TabHost with tabs unselected.
Thanks.
What I usually do is, add an extra Tab and use setVisibility(View.GONE) to hide it. THis will just hide the tab button from the user, and the Tab will still be there, in the "background" and you can programmatically select it, by using tabHost.setCurrentTab(0). I also usually keep this tab as the first one.
1.copy the code where you want to tabs make unselected
tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
tabLayout.setTabTextColors(Color.BLACK, Color.BLACK);
2.Override on Tabselected Listener and paste the following code
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override`enter code here
public void onTabSelected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
tabLayout.setTabTextColors(Color.BLACK, Color.RED);
viewPager.setCurrentItem(position);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
tabLayout.setTabTextColors(Color.BLACK, Color.RED);
viewPager.setCurrentItem(position);
}
});

Android TabHost with only selected tab on stack

I have a TabHost with 4 tabs. I need only the selected tab activity to be available on the stack. When user changes the tab, how to finish the activity under previous tab. I tried the following code. Here showing code for first tab. It similar for remaining tabs:
spec = tabHost.newTabSpec("tab1").setIndicator("Tab1",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(new Intent(this, Tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP));
But the above code is deleting the Tab1 Activity on stack/heap only when the user comes again to that tab but not when user changes to new tab.
I've had a look at this, what is your reason for this?
How do you know what's on the stack? Are you depending on onDestroy() or something?
I haven't got a full answer but you can see which tab is active:
Let your class implement OnTabChangeListener
public class YourClass extends TabActivity implements OnTabChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Load all your normal objects as well as TabHost
// make your tabhost listen for tab changes
mTabHost.setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tabId) {
int currentTabNumber = mTabHost.getCurrentTab();
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
if(i != currentTabNumber){
System.out.println("I'm not a currently active tab");
}
}
}
}

how to disable a tab in android screen?

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
}

Categories

Resources