Tab icon and text both using android design support library
Asked Answered
R

2

8

on startup the tab are looking like thisthis is my project on startup..all the tabs are black but i want the first one to be orange(its icon and text)[![this is the second tab when i move to this working fine[![when i get back to the first tab it is working what i want to be..the main isssue is when the app starts]1]1i used android design support tablayout and i got both icon and text on tab .. i used a customtabview to align icon and text vertically..And i want to change the color of the icon and text when selected/unselected.

i made selector file for different tab icon..but the thing is when i run the project the first default tab is unselected i want them to get selected by default..and if i go to other tab and then swipe back to the first tab it works perfectly fine..the main problem is i cant make the first tab fragment selected..please help me as i m stuck for past 2 days..and i dont want to use any library from github

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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;
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 android.view.LayoutInflater;
import android.widget.TextView;

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.image1,
            R.drawable.image2,
            R.drawable.image3
    };

    @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() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("ONE");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("TWO");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image2, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("THREE");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image3, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
    }

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

image1.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/x1" android:state_selected="true" />
        <item android:drawable="@drawable/x2" />
    </selector>

crested three file like above for three different image.

custom_tab.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tab"
    android:textColor="@color/selector"
    android:textSize="@dimen/tab_label"
    android:fontFamily="@string/font_fontFamily_medium"/>

selector.xml for text

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:color="#fe5d55" /> <!-- pressed -->
    <item android:state_pressed="true"
        android:color="#fe5d55" /> <!-- focused -->
    <item android:color="#000000" /> <!-- default -->
</selector>

Can anyone help ?

Romaine answered 18/9, 2015 at 22:1 Comment(6)
Have you tried addTab(TabLayout.Tab tab, boolean setSelected)?Parting
actually i m using the custom view for the tablayout.... i dont know how to use the above or how to make changes...please tell me,refer to the above codeRomaine
Add your selector and custom view code as well.Parting
@hoomiedited the post with the files u asked..if u have time cud u please run it on ur system and tell me where i am lackingRomaine
Firstly I am not sure if you need to use the custom layout as it is only a TextView. Secondly even if you use your custom layout addTab(TabLayout.Tab tab, boolean setSelected) should still work with the correct selector.Parting
Here a good tutorial: androidhive.info/2015/09/…Capablanca
S
4

What do you mean by unselected. Can you share an image of what you are trying to achieve and where you are at now..

I wouldn't recommend your approach. It does a lot of things which are not needed to solve your problem. I suggest making use of the icon and text from the TabLayout class and just set your icons(drawables with states) and text. Or, even a custom layout if required, but making use of the text1 and icon from the TabLayout. Do you have any issues doing this?


Update..

Try the below:

IconTextTabLayout.java:

package example.customtabs;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;

import sample.customtabs.example.com.customtabs.R;


public class IconTextTabLayout extends TabLayout {


    public IconTextTabLayout(Context context) {
        super(context);
    }

    public IconTextTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IconTextTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
        this.removeAllTabs();
        int i = 0;
        for (int count = adapter.getCount(); i < count; ++i) {
            this.addTab(this.newTab().setCustomView(R.layout.custom_tab)
                    .setIcon(YourIcons[i])
                    .setText(adapter.getPageTitle(i)));
        }
    }
}

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ImageView
        android:id="@android:id/icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

In Your activity xml use IconTextTabLayout. Like :

<?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">

    <example.customtabs.IconTextTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white"/>

</LinearLayout>

also, set view pager to it in your activity like :

IconTextTabLayout tabLayout = (IconTextTabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);

Override getPageTitle in your viewpager adapter to provide title.

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[i];
}

Additionally, if you want you can get icons too from the adapter by writing a method that returns drawables, just like the getPageTitle method. That's optional. Just a matter of design.

--

YourIcons[] = This is an array of drawables. In your case - xml files, with selected and non-selected images...

Software answered 20/9, 2015 at 2:28 Comment(3)
i uploaded two image....the first one is what i m getting as of now...as u see all the three tabs are unselected(as i select the unselected color to black)...and the second is what i want...the first tab should be selected (orange color) when the app starts.. but when i move back to the first tab it works fine its just i havent figured out the way to selected the first tab by defult...i havnt tried ur way....but please help me with this....i m not an android expert...or u cud provide me ur email i will send the full project thereRomaine
hey buddy i cant see the tabtext and icon..... i move on to another thing i skip this...i have to submit my project... if u have some time cud u please do this part for me..u know everything..give me ur email or something i can send u the project.... please...its high time...thanks....and i know ur answer wud work i accept this answer but i have to do the main working of my app... please reply..thanksRomaine
I've got this problem : Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class example.customtabs.IconTextTabLayoutArjan
N
3

Instead of customizing the TabLayout as specified by @Abhilash answer just add a line i.e. tabOne.setSelected(true); in below code

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("ONE");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
tabOne.setSelected(true);  //This will make your tab by default selected
tabLayout.getTabAt(0).setCustomView(tabOne);
Nefertiti answered 18/11, 2015 at 9:24 Comment(1)
how do you get back that compunddrwable to set colorfilter when selected?Economizer

© 2022 - 2024 — McMap. All rights reserved.