manishKungwani
manishKungwani

Reputation: 925

Prevent ActionBar tabs from horizontal scrolling

I have 4 tabs in an actionBar. I'm using a RelativeLayout with an ImageView and a TextView as a customView for the tabs.

The actionBar overflows the width of the screen and the tabs become horizontally scrollable.

The actual width of my customView is not much, but the Tabs take up extra space (varies on different devices).

How can I restrict the tabs to occupy not more than 1/4th of the screen width.

On an Sony Xperia Z2 running 4.4.2

On a Moto E2 running Android 5.0

Upvotes: 0

Views: 1124

Answers (2)

Xcihnegn
Xcihnegn

Reputation: 11597

Tab view width is based on content/yourImage size, so to make tab view width smaller, you have to control the content/Image size. You can use CustomView for your Tab by putting ImageView inside FrameLayout.

For detail please go to the answer for post ActionBar tabs set dynamic width according to screen width.

Upvotes: 1

kandroidj
kandroidj

Reputation: 13932

Instead of Using the ActionBar, you will need to either implement a ToolBar or something similar in your UI which you can control the width of that has Tab's inside it...

ActionBar is meant to take up the full width of the Screen whereas you can specify the width of a Toolbar yourself...

create a tab_layout.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v7.widget.Toolbar 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="200dp"
      android:layout_height="wrap_content"
      app:popupTheme="@style/Theme.AppCompat.Light.NoActionBar">

    <!-- Add your Tabs here -->

  </android.support.v7.widget.Toolbar>

Then just include this layout in your activity:

<include layout="@layout/tab_layout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

Upvotes: 1

Related Questions