Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement Segment Control in android studio?

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?

like image 401
Satish Kumar Avatar asked Apr 26 '17 10:04

Satish Kumar


People also ask

Does Android have a segmented control?

Android Segmented control is a linear segment made up of multiple segments with each segment functioning as a button. The segments are the same size and allow users to select between multiple contexts.

How do you implement segment control in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and drag a Segmented Control to the top of the main view. Also drag a Label to the view and place it below the Segmented Control. Select the label and give it a text of First Segment selected.

What is segmented control?

A segmented control is a linear set of two or more segments, each of which functions as a button. Within a segmented control, all segments are usually equal in width. Like buttons, segments can contain text or images. Segments can also have text labels beneath them (or beneath the control as a whole).


2 Answers

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

like image 134
Suhayl SH Avatar answered Sep 23 '22 07:09

Suhayl SH


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.

like image 31
Anudeep Avatar answered Sep 19 '22 07:09

Anudeep