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) {
}
});
Related
I am using ViewPager/TabLayout on Android TV, and I have 2 questions.
1) Currently, if I press DPAD_UP, the focus goes to the tabs, but the page does not actually change to the one corresponding to the focused tab until DPAD_CENTER is pressed. So, there is a difference between focused (shown as a grey box around the tab text) and selected (shown as an underline). I am wondering if it is possible to have whatever focused to be selected?
2) I have 3 tabs in my ViewPager where the fragment in tab1 needs to have focus in order to display things properly. When I switch from tab3 to tab1, the focus is not given to the fragment but stays with the tab. This causes my fragment to not display properly. I am wondering if there is a way to take focus away from the tab? I dug into the source code of ViewPager and TabLayout, and I know the focus is with TabView of TabLayout, but no matter what I try, I cannot take away the focus state of TabView (i.e. the grey box surrounding the tab text does not go away until I use DPAD_DOWN to "go down" to the fragment).
Tried
((ViewGroup) tabLayout.getChildAt(0)).getChildAt(desiredPosition).clearFocus() and it doesn't help.
I don't want to use viewPager.setOffscreenPageLimit(2);
// This is my viewpager activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
adapterViewPager = new MyPagerAdapter(getFragmentManager());
viewPager.setAdapter(adapterViewPager);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
// Tried to clear focus of tab here, didn't help
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages.
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for a particular page.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new OneFragment; // This is the fragment that needs focus
case 1:
return new TwoFragment();
case 2:
return new ThreeFragment();
default:
return null;
}
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "Fragment 1";
} else if (position == 1) {
return "Fragment 2";
} else {
return "Fragment 3";
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
android:theme="#style/AppThemeTab" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
Any help is appreciated, thank you!
One liner solution :) . To suppress focus on Tablayout children views use the code fragment below assuming the tab is scrollable - i.e. tabs selection is scrolling by digipad:
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
This is already being implemented in Google Play and I want to use it in my app.
I've implemented the sliding tabs and toolbar using the android design library. My application features 4 tabs and I want to achieve this kind of color change among these tabs. Here are the screenshots of the Google Play app which I was referring to.
First Tab
Second Tab
Third Tab - same root as above links with the extension /Mc3a7.png (Apologies for this, but I'm not able to post more than 2 links due to a low reputation.)
Please note that a solution which is being implemented in JAVA will be preferred by me over one which suggests using XML for this purpose :)
if your are using view-pager you can set listener on it and then change toolbar color
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0:
toolbar.setBackgroundColor(Color.YELLOW);
break;
case 1:
toolbar.setBackgroundColor(Color.GREEN);
break;
case 2:
toolbar.setBackgroundColor(Color.RED);
break;
default:
break;
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I am exploring different Android app UIs. I am trying to make one that has two columns, and on each column is a button that shifts into a different screen, but the two columns remain stationary.
An example to what I'm trying to replicate: http://i.imgur.com/a2IqkV1.jpg
The 'list' and 'number' screens each have their own separate layouts, I think I need to do something with fragments, but I don't know where to start.
Thanks
When you dont know where to start you can go for TabLayout : TabLayout and ViewPager : ViewPager and ofcourse Fragment :Fragment .You can create a layout like in the image by using this 3 items. Also you can follow this guide to create a screen like in the image, guide is so good for beginners : google-play-style-tabs-using-tablayout
Additionally if you want to switch your Fragment when any of tab clicked, add this code :
tablayout.addOnTabSelectedListener(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) {
}
});
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.
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);
}
});