Search code examples
androidtextviewradio-buttonandroid-linearlayout

Align horizontally 5 text views in Android


I need your help to solve this problem: I have a linear layout with horizontal orientation. Inside this layout I have 5 textviews with visibility:gone. Above this layout I have a radio group with 5 radio button aligned horizontally.

When I check a radio button the corresponding textviews changes his visibility in visible. I want to show a text view below every radio button. Actually they are alle in the same position.

<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 >

   <TextView />
   <TextView />
   <TextView />
   <TextView />
   <TextView />
 </LinearLayout>

How can I do that?


Solution

  • Set LinearLayout weigthSum to 1.0. Make each textview weight to 0.2 & visibility to invisible. View with gone visibility doesn't take any space.

    I made second textview visible. But textview with text "1" will occupy space before textview with text of "2".

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="net.techdesire.HomeActivity"
        tools:showIn="@layout/app_bar_home">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radio_layout">
    
            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:id="@+id/value_group">
    
            <RadioButton
                android:text="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton1"
                android:layout_weight="0.2"/>
            <RadioButton
                android:text="2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:layout_weight="0.2"/>
            <RadioButton
                android:text="3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton3"
                android:layout_weight="0.2"/>
            <RadioButton
                android:text="4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton4"
                android:layout_weight="0.2"/>
            <RadioButton
                android:text="5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton5"
                android:layout_weight="0.2"/>
            </RadioGroup>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/fragment_container"
            android:orientation="horizontal"
            android:layout_below="@+id/radio_layout"
            android:weightSum="1.0">
    
            <TextView
                android:id="@+id/text1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:visibility="invisible"
                android:text="1"/>
            <TextView
                android:id="@+id/text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:visibility="visible"
                android:text="22345678dsada"
                android:maxLines="1"
                android:ellipsize="marquee"/>
            <TextView
                android:id="@+id/text3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:visibility="invisible"
                android:text="3"/>
            <TextView
                android:id="@+id/text4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:visibility="invisible"
                android:text="4"/>
            <TextView
                android:id="@+id/text5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:visibility="invisible"
                android:text="5"/>
        </LinearLayout>
    </RelativeLayout>
    

    On Radio group set onCheckChangeListener then compare id of checked radio button and according to it hide/show textview.See following

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                /*hide all*/
                TextView tv;
                if(checkedID==R.id.radioButton1)
                    tv=(RadioButton)findViewById(R.id.text1);
                elseif(checkedID==R.id.radioButton2)
                    tv=(RadioButton)findViewById(R.id.text2);
                /* Similarly other else conditions*/
                tv.setVisibility(View.VISIBLE);
            }
        });