0

I want to add custom view for my tabs.I researched the internet but I can't find good tutorial.I am adding tabs with this code in onCreate event.

        private String[] tabs = { "Tab 1", "Tab 2"};
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

I don't have any xml file for tabs.

How can I do this ? Can you give an example ?

Tolgay Toklar
  • 4,151
  • 8
  • 43
  • 73

1 Answers1

1

first of all i suggest you use :

https://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

and do not use actionbar tabs.

that is a source code of tabs of google play app. after that you can create your own layout use mSlidingTabLayout.setCustomTabView(int layoutResId, int textViewId) to inflate a custom layout for the SlidingTabLayout tab views. look at:

Android SlidingTabLayout with icons

for complete example.

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

    <com.example.android.common.view.SlidingTabLayout
          android:id="@+id/sliding_tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

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

and in your fragment or activity:

mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new MyAdapter());
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);

your adapter of mViewPager must override below method

 @Override
        public CharSequence getPageTitle(int position) {
            return your tab title;
        }

UPDATE:

first step is creating custom layout for each tabs so:

     <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>

then when you want to add tabs to actionbar you must do :

    ActionBar.Tab tab1=actionBar.newTab();
    tab1.setTabListener(this);
    tab1.setCustomView(R.layout.tab);
    TextView txt1 = (TextView)tab1.getCustomView().findViewById(R.id.text1);
    txt1.setText("Tab 1");
Community
  • 1
  • 1
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • I can't change my tab system.Because I am using this:http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1 Is there another way ? – Tolgay Toklar Aug 29 '14 at 16:52
  • why? every thing here is straight forward , all you need is just add that in layout and set viewpager for it and also overwrite getTitle(), if you want i can post code to run it ! but anyway do what you like!! – mmlooloo Aug 29 '14 at 16:59
  • I will try.But I don't want to change my system because I finished everything in my project this is the last problem for me. – Tolgay Toklar Aug 29 '14 at 17:19
  • do whatever you like my brother ;-) but i do not think you need a lot change !! – mmlooloo Aug 29 '14 at 17:22
  • Can you review my code ? Is this avaible for change ? This is main : http://pastebin.com/MgkWFBr2 and this is xml http://pastebin.com/vpELpNF3 – Tolgay Toklar Aug 29 '14 at 17:27
  • i could not open www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1 so i do not know exactly what you have implemented. you sync viewpager by tabs, do not you? everything is ok but what is ` – mmlooloo Aug 29 '14 at 17:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60250/discussion-between-tolgay-toklar-and-mmlooloo). – Tolgay Toklar Aug 29 '14 at 17:37
  • i have solved it in two ways (actionbar and SlidingTab) and will post it in 12hrs know i must go somewhere , do not be worry about that!! – mmlooloo Aug 30 '14 at 04:50
  • i can show badge but after that the click listener dose not work, am trying to fix it!! – mmlooloo Aug 30 '14 at 10:09
  • i also implement it in another way but : https://stackoverflow.com/questions/25584010/badgeview-and-slidingtabstrip-clicking-error – mmlooloo Aug 30 '14 at 17:26