I am trying to show icons in my tab, I am using tablayout and viewpager , but when I m calling tablayout.setupWithViewPager(viewpager) my icons are disappearing
Min sdk is 16
Target is 24
Support library 24.2.1
this is how I am setting my Tabs :-
private void setUpTabs(){
tabs.setupWithViewPager(viewpager);
TabLayout.Tab discoverTab = tabs.newTab();
TabLayout.Tab placesTab = tabs.newTab();
TabLayout.Tab rewardTab = tabs.newTab();
Drawable discover = ContextCompat.getDrawable(this, R.drawable.discover);
discover.setAlpha(255);
discover.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
Drawable places = ContextCompat.getDrawable(this, R.drawable.places);
places.setAlpha(55);
places.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
Drawable rewards = ContextCompat.getDrawable(this, R.drawable.rewards);
rewards.setAlpha(55);
rewards.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
discoverTab.setIcon(discover);
placesTab.setIcon(places);
rewardTab.setIcon(rewards);
tabs.addTab(discoverTab,0);
tabs.addTab(placesTab,1);
tabs.addTab(rewardTab,2);
}
ViewPager :-
private void setUpViewPager(){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(DiscoverFragment.getFragment(null),"Discover");
adapter.addFrag(PlacesFragment.getFragment(null),"Places");
adapter.addFrag(RewardsFragment.getFragment(null),"Rewards");
viewpager.setAdapter(adapter);
}
ViewPagerAdapter :-
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}
}
my XML layout :-
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/action_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="#android:color/white"
app:tabMode="fixed"
app:tabSelectedTextColor="?attr/colorAccent"
app:tabTextColor="#android:color/black" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Just try this way it will work for you
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
For more check Tablayout
Related
I we know we can set any TextView for TabsLayout tabs without any problem, but at this time i dont have problem for setting TextView for tabs, but i want to have Image on tabs instead of text, for using this custom views i have this method:
private void setupTabIcons() {
inflater = LayoutInflater.from(context);
timeline = inflater.inflate(R.layout.joined_cafe_webs, null);
TabLayout.Tab contentJoinedCafeWebsTab = tabsLayout.getTabAt(0);
Objects.requireNonNull(contentJoinedCafeWebsTab).setCustomView(timeline);
grid_list = inflater.inflate(R.layout.user_channels, null);
TabLayout.Tab grid_listTab = tabsLayout.getTabAt(1);
Objects.requireNonNull(grid_listTab).setCustomView(grid_list);
post_items_view_pager.setCurrentItem(1);
...
}
joined_cafe_webs layout:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/grid_feeds"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:padding="5dp"
android:tint="#color/grey_40"
app:srcCompat="#drawable/ic_time_line_black_24dp" />
after compile and run project on phone, tabs are empty
Please check this :
https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
And
Add Icons+Text to TabLayout
You can use following code :
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
I am trying to create a chat app for android based on chat21 sdk app on github, with chat21 demo app also in github and the thing is that I want to add Icons to TabLayouts and I am stuck there because I tried a couple times on my own and I just can't get it to work, so here is an image of how I want my chat tabs to look,
here is tab_activity.xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/windowBackground"
tools:context=".TabActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/white"
app:tabIndicatorHeight="3dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>`
and here it is TabActivity in java:
package chat21.android.demo;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import org.chat21.android.core.ChatManager;
import org.chat21.android.ui.ChatUI;
import java.util.HashMap;
import java.util.Map;
public class TabActivity extends AppCompatActivity {
private static final String TAG = TabActivity.class.getName();
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
ChatUI.getInstance().processRemoteNotification(getIntent());
}
#Override
protected void onResume() {
ChatManager.getInstance().getMyPresenceHandler().connect();
super.onResume();
}
#Override
protected void onDestroy() {
ChatManager.getInstance().getMyPresenceHandler().dispose();
super.onDestroy();
}
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is
present.
// getMenuInflater().inflate(R.menu.menu_tab, menu);
// return true;
// }
//
// #Override
// public boolean onOptionsItemSelected(MenuItem item) {
// // Handle action bar item clicks here. The action bar will
// // automatically handle clicks on the Home/Up button, so long
// // as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
//
// //noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
//
// return super.onOptionsItemSelected(item);
// }
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int tabsCount;
private Map<String, Item> tabsMap;
private String[] tabsTags;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
tabsMap = new HashMap<>();
// create a map with all tabs
tabsMap.put(getString(R.string.tag_home),
new Item(getString(R.string.tab_home_title), 1));
tabsMap.put(getString(R.string.tag_chat),
new Item(getString(R.string.tab_chat_title), 1));
tabsMap.put(getString(R.string.tag_profile),
new Item(getString(R.string.tab_profile_title), 1));
// retrieve tab tags
tabsTags = getResources().getStringArray(R.array.tab_tags);
tabsCount = tabsTags.length; // count tab tags
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
String tabTag = getTagByPosition(position);
if (tabTag.equals(getString(R.string.tag_home))) {
return HomeFragment.newInstance();
} else if (tabTag.equals(getString(R.string.tag_chat))) {
return ChatFragment.newInstance();
} else if (tabTag.equals(getString(R.string.tag_profile))) {
return UserProfileFragment.newInstance();
} else {
// default load home
return ChatFragment.newInstance();
}
}
#Override
public int getCount() {
return tabsCount;
}
private String getTagByPosition(int position) {
return tabsTags[position];
}
private String getTitleByTag(String tag) {
return tabsMap.get(tag).getTitle();
}
private int getIconByTag(String tag) {
return tabsMap.get(tag).getIcon();
}
#Override
public CharSequence getPageTitle(int position) {
String tabTag = getTagByPosition(position);
return getTitleByTag(tabTag);
}
private class Item {
private String title;
private int icon;
private Item(String title, int icon) {
setTitle(title);
setIcon(icon);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
#Override
public String toString() {
return "Item{" +
"title='" + title + '\'' +
", icon='" + icon + '\'' +
'}';
}
}
}
}
I think you are looking for this:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
(this is the result)
This is the code which adds the Images:
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
and if you want the exact same like in your picture take a look at this page:
https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
at the point: 6. Custom Tab View with Icon & Text
Here is my code:
package demo.stackowerflow_demos;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.content.res.AppCompatResources;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TabLayout tab = findViewById(R.id.tab);
final ViewPager pager = findViewById(R.id.pager);
pager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager(), this));
setupTabWithViewPager(tab, pager, R.layout.tab_layout);
}
//tabs
enum Tab {
HOME(R.string.tab_home, R.drawable.ic_home),
CHAT(R.string.tab_chat, R.drawable.ic_chat),
PROFILE(R.string.tab_profile, R.drawable.ic_profile);
Tab(#StringRes int title,
#DrawableRes int icon) {
this.title = title;
this.icon = icon;
}
final #StringRes
int title;
final #DrawableRes
int icon;
static final Tab[] VALUES = values();
}
//simple interface
interface IconedTabPagerAdapter {
CharSequence getPageTitle(int position);
Drawable getPageIcon(int position);
}
//adapter for pager
static class SectionsPagerAdapter
extends FragmentPagerAdapter
implements IconedTabPagerAdapter {
private final Context mContext;
public SectionsPagerAdapter(FragmentManager fm,
Context context) {
super(fm);
mContext = context;
}
#Override
public Fragment getItem(int position) {
final Tab tab = Tab.VALUES[position];
switch (tab) {
case CHAT:
return ChatFragment.newInstance();
case PROFILE:
return UserProfileFragment.newInstance();
// default load home
case HOME:
default:
return HomeFragment.newInstance();
}
}
#Override
public int getCount() {
return Tab.VALUES.length;
}
#Override
public CharSequence getPageTitle(int position) {
final #StringRes int title = Tab.VALUES[position].title;
return mContext.getString(title);
}
#Override
public Drawable getPageIcon(int position) {
final #DrawableRes int icon = Tab.VALUES[position].icon;
return AppCompatResources.getDrawable(mContext, icon);
}
}
static void setupTabWithViewPager(#NonNull TabLayout tabLayout,
#NonNull ViewPager viewPager,
#LayoutRes int tabCustomView) {
final PagerAdapter adapter = viewPager.getAdapter();
if (!(adapter instanceof IconedTabPagerAdapter)) {
tabLayout.setupWithViewPager(viewPager);
return;
}
final IconedTabPagerAdapter iconedAdapter = ((IconedTabPagerAdapter) adapter);
for (int position = 0; position < adapter.getCount(); position++) {
final Drawable icon = iconedAdapter.getPageIcon(position);
final CharSequence title = adapter.getPageTitle(position);
tabLayout.addTab(tabLayout.newTab()
.setCustomView(tabCustomView)
.setIcon(icon)
.setText(title));
}
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="#color/tab_background"
app:tabIndicatorColor="#color/tab_indicator" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatTextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#color/tab_title_color" />
</LinearLayout>
</RelativeLayout>
Result:
I want to show icon left side of the text but the problem is that the icon is showing above the text. it's taking much more space.
I want keep the icon left sode of the text(title) . what should i do?
The main activity is given below
java.class
public class Tabsadar extends AppCompatActivity {
ExpandableRelativeLayout
expandableLayout1,
expandableLayout2,
expandableLayout3,
expandableLayout4;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_action_name,
R.drawable.thanatottho,
R.drawable.proyojonio
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabsadar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new tabsadarc1(), "ফিচার");
adapter.addFrag(new tabsadarc2(), "তথ্যাবলি");
adapter.addFrag(new tabsadarc3(), "প্রয়োজনীয়");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}'
i want something like that
in my case something happend like that
any solution?
Just use app:tabInlineLabel="true"
Like:-
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabInlineLabel="true"
app:tabGravity="fill"/>
As per your requirement I'll suggest you to create a custom view for tabs.
custom_tab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_6sdp"
android:background="#drawable/ic_menu_gallery"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/_3sdp"
android:textColor="#drawable/tab_text_selector"
android:textSize="#dimen/_15sdp" />
</LinearLayout>
</RelativeLayout>
And this layout of custom tab with PagerAdapter as
//On Create
for (int i = 0; i < mPagerAdapter.getCount(); i++) {
View customView = mPagerAdapter.getCustomView(getActivity(), i);
mainTabLayout.getTabAt(i).setCustomView(customView);
}
//Pager Adapter
public static class ViewPagerAdapter extends SmartFragmentStatePagerAdapter {
private List<Map<String, Object>> maps = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public View getCustomView(Context context, int pos) {
View mView = LayoutInflater.from(context).inflate(R.layout.custom_tab_view, null);
TextView mTextView = (TextView) mView.findViewById(R.id.textView);
ImageView imageView = (ImageView) mView.findViewById(R.id.imageView);
mTextView.setGravity(Gravity.CENTER);
mTextView.setTypeface(Typeface.createFromAsset(context.getAssets(),
"fonts/aller_regular.ttf"));
mTextView.setText(getPageTitle(pos));
return mView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new CompletedTaskFragment();
case 1:
return new PendingTaskFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
Hope it'll help you.
I want to create n number of tabs dynamically, using Rest APIs, Since every tab is associated with there Fragments while swiping, can I create tabs + Fragments dynamically using the number of tabs given in JSON (Rest API) or I have to create all individually?
Yes, you can:
<android.support.design.widget.TabLayout
android:id="#+id/htab_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#color/colorDivider"
app:tabIndicatorColor="#android:color/white" />
<android.support.v4.view.ViewPager
android:id="#+id/htab_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.htab_viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.htab_tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//jsonResArray get from web service
for (int i = 0; i < jsonResArray.size(); i++) {
adapter.addFrag(new NewFragment(jsonResArray.get(i)), jsonResArray.get(i).getName());
}
viewPager.setAdapter(adapter);
}
static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List < Fragment > mFragmentList = new ArrayList < >();
private final List < String > mFragmentTitleList = new ArrayList < >();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
I want to set image/icon in Tabsin the following code:
in my application i set Tabs in Bottom and now i want to set image in the space of the text.. i tried tutorials also but not getting output as i wanted to
Here Tab.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white"/>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
/>
</LinearLayout>
Now Tab.Java
package com.example.sachin.navigationwithswipe;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.sliding_tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new PrimaryFragment();
case 1 : return new SocialFragment();
case 2 : return new UpdatesFragment();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
}
return null;
}
}
}
i want to set Image in the Tabs using following code.
Use this to set icon in tab
tabLayout.setupWithViewPager(viewPager);
//You tab icons
private int[] icons = {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setIcon(icons[i]);
}
Also If you want only Icon then on Adapter pass pageTitle null
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}
Use this for Selector. This Might Work
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_on" android:state_selected="true"/>
<item android:drawable="#drawable/icon_off"/> <!-- default -->
</selector>
Set icon like this way..
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.yourIcon));
To set both icon and text:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_tab_favourite,
R.drawable.ic_tab_call,
R.drawable.ic_tab_contacts
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Setting only icon to tab is same as setting text and icon except the method getPageTitle() in ViewPagerAdapter class returns null instead of tab label.
Modify MainActivity.java and modify the getPageTitle() method as :
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}