TabLayout selected indicator - must select tab twice - android

I'm able to navigate between the tabs in my TabLayout, but only if I select the tab twice does the indicator highlight follow that selection.
For example, from the home tab I will select the quiz tab. The layout for the quiz displays, but the selected indicator under the tab icon remains on home. If I select quiz a second time, then the indicator will follow.
I'm not sure what I'm doing wrong as I thought what I'd been doing was pretty standard, but here's my code that handles tab selects.
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 1:
Intent infoIntent = new Intent(MainActivity.this, about.class);
MainActivity.this.startActivity(infoIntent);
break;
case 2:
Intent studentIntent = new Intent(MainActivity.this, student.class);
MainActivity.this.startActivity(studentIntent);
break;
case 3:
Intent surveyIntent = new Intent(MainActivity.this, survey.class);
MainActivity.this.startActivity(surveyIntent);
break;
case 4:
Intent quizIntent = new Intent(MainActivity.this, quiz.class);
MainActivity.this.startActivity(quizIntent);
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
am i missing something?

It might not be the best way, but I solved this by simply putting
tabLayout.getTabAt(index).select();
in each of my activities onCreate. Thanks for the suggestions

Related

Make a layout similar to Google's Play Store

I'm trying to make a layout similar to Google's Play Store. I'm having a little trouble identifying the following views.
Yellow - Is this an ActionBar.Tab or a ViewPager or something else?
Red - Is this just a RadioGroup with RadioButtons in a Horizontal ScrollView?
the view highlighted with yellow is a TabLayout from support library and the red one is a Horizontal ListView with Chip View
https://github.com/Plumillon/ChipView
Or you can make it your self using a TextView with a rounded corner shape drawable as background
Edit: Then you should use a FrameLayout instead of ViewPager and with addOnTabSelectedListener get selected tab on TabLayout and show the appropriate Fragment inside FrameLayout
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
page = tabLayout.getSelectedTabPosition();
switch (page) {
case 0:
fragmentManager.beginTransaction().replace(R.id.container, fragment1).commitAllowingStateLoss();
break;
case 1:
fragmentManager.beginTransaction().replace(R.id.container, fragment2).commitAllowingStateLoss();
break;
default:
fragmentManager.beginTransaction().replace(R.id.container, fragment3).commitAllowingStateLoss();
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

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 to move to a specific tab from an activity in android

I am working on the following screen named FriendListActivity:
As you can see here , I am 3 tabs named CHAT,GROUPS and CONTACTS .Each tab is displaying some contents.For example Groups Tab is displaying the number of groups.On clicking each Group ,Group Chat Screen is opened .Now In Group Chat screen ,i have a toolbar .Consider the following code:
Toolbar toolbarGroupChat = (Toolbar) findViewById(R.id.toolbarGroupChat);
setSupportActionBar(toolbarGroupChat);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(upperCase(group_name));
//ViewPager
viewPager = (ViewPager) findViewById(R.id.viewPager);
//Initializing PagerAdapter
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
PagerAdapter.java
public class PagerAdapter extends FragmentStatePagerAdapter {
private int noOfTabs;
public PagerAdapter(FragmentManager fm, int noOfTabs) {
super(fm);
this.noOfTabs = noOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
ChatFragment chatFragment = new ChatFragment();
return chatFragment;
case 1:
GroupsFragment groupsFragment = new GroupsFragment();
return groupsFragment;
case 2:
ContactsFragment contactsFragment = new ContactsFragment();
return contactsFragment;
default:
return null;
}
}
#Override
public int getCount() {
return noOfTabs;
}
}
On clicking the arrow on the toolbar ,my application is moved to the screen containing three tabs.My problem is current selected tab is always CHAT tab ,when i am moving from an activity to FriendListActivity .When i am moving from Group Chat Screen to FriendListActivity,the current selected tab should be GROUPS Tab.
Solved.
Edited Working Code:
When we are moving from an Activity to a specific tab ,We need to pass the TAb number using intents from the Activity.Use the following code for handling click on the back button of the Toolbar .Override the getSupportParentActivityIntent() method:
public Intent getSupportParentActivityIntent() {
final Bundle bundle = new Bundle();
final Intent intent = new Intent(this, FriendsListActivity.class);
bundle.putString("TabNumber", tab_number);
intent.putExtras(bundle);
return intent;
}
Now inside the activity containing 3 tabs,use the following code:
final Intent intent = getIntent();
if (intent.hasExtra("TabNumber")) {
String tab = intent.getExtras().getString("TabNumber");
Log.e("TabNumberFriendList",tab);
switchToTab(tab);
}
public void switchToTab(String tab){
if(tab.equals("0")){
viewPager.setCurrentItem(0);
}else if(tab.equals("1")){
viewPager.setCurrentItem(1);
}else if(tab.equals("2")){
viewPager.setCurrentItem(2);
}
}
Use the setCurrentItem(int position) method of your ViewPager object to move to a specific tab.
You might have created a tabHost, so when it is done making, you should use -
tabHost.setCurrentTab(1);
and you can use same code with different parameters whenever you want to set another tab.
Ref
From the screenshot you must be using Tablayout, so from there you can use these two in build functions.
tabLayout.getSelectedTabPosition();
Save this tab in some variable and then onCreate set that integer value in this function..
tabLayout.getTabAt(<Your last selected tab>);
Hope this helps..:)
You can also use this piece of code:
Add public static TabToOpen = 1 in the group chat activity(say group_chat_activity).
then use setCurrentTab(group_chat_activity.TabToOpen) in onResume() of activity where your ViewPager object is to move to a specific tab. This is particularly useful when you need a button to access a particular tab in sliding activity.

How to deselect all the tabs of TabLayout in Android because of option menu?

I am developing an Android app. I am using TabLayout for tabs. But I am not using pager view for fragment on tab selected. Instead I am replacing fragment on event listener to a view. I want to deselect all the tabs when an item in option menu is selected. But I saw an identical question in Stack Overflow. The answer is cannot do this. Because a tab must be selected.
This is UI design of my app:
As you can see in above design, PODCASTS tab is selected by default. This is where the issue begins.
Here are the screenshot of my option menu and function
The problem is when I select the settings while I am on PODCASTS tab, settings fragment is replaced. But the PODCASTS tab is still selected. But when I click again on, podcasts list fragment is not replaced back. How can I solve that problem ?
This is how I am adding tabs to tab layout in MainActivity:
private void setupTabs() {
tabLayout.addTab(tabLayout.newTab().setText("Podcasts"));
tabLayout.addTab(tabLayout.newTab().setText("Playlist"));
tabLayout.addTab(tabLayout.newTab().setText("Downloads"));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 1:
setPlaylistFragment();
commitFragment();
break;
case 2:
setDownloadsFragment();
commitFragment();
break;
default:
setPodcastListFragment(0, getResources().getString(R.string.app_name), "ALL_FRAGMENT");
commitFragment();
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
I got the answer. I just need to override the onTabReselected as well. So my listener will be like this.
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 1:
setPlaylistFragment();
commitFragment();
break;
case 2:
setDownloadsFragment();
commitFragment();
break;
default:
setPodcastListFragment(0, getResources().getString(R.string.app_name), "ALL_FRAGMENT");
commitFragment();
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 1:
setPlaylistFragment();
commitFragment();
break;
case 2:
setDownloadsFragment();
commitFragment();
break;
default:
setPodcastListFragment(0, getResources().getString(R.string.app_name), "ALL_FRAGMENT");
commitFragment();
break;
}
}
});
You can change the background of the selected tab when setting button pressed
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#393939"));
and set the selected tab to some other tab i.e setSelected(1)
Your flow will be like that.If you are on tab-1 and you click setting button then on button click you can change your selected tab and background of selected tab.Now when user select the first tab he can see that tab content
P.s : because of less reputation i didn't comment it on your question
I will recommend you use a boolean to hold the states, for example, if I don't want a view A to show, I will make it conditional by creating a boolean say boolean showA and set it to false, on page rendered, I will set it to true, on click of the option Item you said, I will set that boolean to false again which will means A wont show as it depends on the boolean. That should do it. Hope this help.

Android onclicklistener for tabs

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.

Categories

Resources