7

enter image description here

I need to use segmented control in Android. Is there a default control for the same? What might be the best and efficient way to do so?

Vinodh
  • 5,262
  • 4
  • 38
  • 68
Satish Kumar
  • 131
  • 2
  • 10
  • I think you are came with IOS background, what is the segmentControl? what you want to do exactly? – Shayan Pourvatan Apr 26 '17 at 10:43
  • Similar to this post - [StackOverflow Post](http://stackoverflow.com/questions/20538421/segment-control-in-android) – hsm59 Apr 26 '17 at 10:45
  • 2
    Possible duplicate of [Segment control in android](http://stackoverflow.com/questions/20538421/segment-control-in-android) **and** [Segmented control in Android](http://stackoverflow.com/questions/6302079/segmented-control-in-android?noredirect=1&lq=1) – lukkea Apr 26 '17 at 11:53
  • Possible duplicate: [iOS SegmentedControl equivalent in Android](http://stackoverflow.com/questions/3952991/ios-segmentedcontrol-equivalent-in-android) **and** [How to create this toggle button?](http://stackoverflow.com/questions/34020155/how-to-create-this-toggle-button?noredirect=1&lq=1) **and** [How to Implement Segment Control in android studio?](http://stackoverflow.com/questions/43631883/how-to-implement-segment-control-in-android-studio?noredirect=1&lq=1) – lukkea Apr 26 '17 at 12:01
  • 1
    please always do a search of SO **before** you ask a question. You should also have been presented with some, if not all of the duplicates I have outlined in my other comments before you posted your question. Please always review those suggestions before you post a question. If you feel the duplicates that have been pointed out in the comments here aren't duplicates to your question please edit your question and, taking each one in turn, demonstrate why they don't answer your particular question. – lukkea Apr 26 '17 at 12:04
  • @lukkea most of the other answers are old and useless. stuff is added to android every year, so many of these basic questions need to be re-asked every year unfortunately. in fact, I was about to ask the same thing now in 2019. at some point, android might get native segmented controls! :) – BjornW Feb 21 '19 at 13:50

2 Answers2

10

This will work:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/padding_extreme"
        android:stretchColumns="*">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/padding_very_less"
        android:background="@color/colorBlack"
        android:padding="@dimen/padding_very_less">

        <TextView
            android:id="@+id/seg_one"
            style="@style/barGrapButtons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:background="@color/colorBlack"
            android:text="SegmentOne"
            android:textColor="@color/colorWhite" />

        <TextView
            android:id="@+id/seg_two"
            style="@style/barGrapButtons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SegmentTwo" />

        <TextView
            android:id="@+id/seg_three"
            style="@style/barGrapButtons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SegmentThree" />
    </TableRow>
</TableLayout>

res/values/styles.xml

<style name="barGrapButtons">
    <item name="android:textColor">@color/colorBlack</item>
    <item name="android:textSize">@dimen/small_text</item>
    <item name="android:gravity">center|center_horizontal</item>
    <item name="android:clickable">true</item>
    <item name="android:padding">@dimen/padding_five_dp</item>
    <item name="android:background">@color/colorWhite</item>
    <item name="android:layout_marginLeft">1dp</item>
</style>

enter image description here

Suhayl SH
  • 1,213
  • 1
  • 11
  • 16
3

I guess you are speaking about fragments with tabs on the top. There will be many answers if you search for Tabbed Activity. However, a simple way to start off with is

Right-click in the package - > New -> Activity - > Tabbed Activity

An activity with tabs and fragments will be automatically created which works as the flow which you have shown in the image.

Anudeep
  • 1,520
  • 1
  • 19
  • 38