• 4
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have a radiobutton with text and a picture on the right, I want the width to be the parent_width, but drawableright is close to the text on the right. How can I achieve this? I get the following enter image description here

I want to get the following but full screen clickable in width enter image description here

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/icon"
            tools:text="Test"/>

    </RadioGroup>
</LinearLayout>

You can apply end (right) padding to the RadioButton that will shift the drawable to the left. The question is how much padding should be applied? Since the amount of padding will change depending upon several factors, the calculation will have to be done in code. (If you have a well-defined environment and know that text length won't change, you could just set the padding in the XML.)

enter image description here

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RadioButton mRadioButton;
    private RadioButton mRadioButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadioButton = findViewById(R.id.radioButton);
        mRadioButton2 = findViewById(R.id.radioButton2);

        RadioGroup rg = findViewById(R.id.radioGroup);
        rg.post(new Runnable() {

            @Override
            public void run() {
                moveDrawable(mRadioButton);
                moveDrawable(mRadioButton2);
            }

            private void moveDrawable(RadioButton radioButton) {
                // Get the drawables for this radio button. If the right drawable is not null
                // then we need to move it to the end of the text.
                Drawable[] drawables = radioButton.getCompoundDrawables();
                if (drawables[2] != null) {
                    // Compute how much end padding to apply to get the drawable beside the text.
                    int textLen = (int) radioButton.getPaint().measureText((String) radioButton.getText());
                    // 4dp between the text and the drawable just to separate them a little.
                    int drawableLeftPadding =
                        (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                                        4, getResources().getDisplayMetrics());
                    int paddingEnd = radioButton.getWidth() -
                        (radioButton.getCompoundPaddingLeft() + +textLen + drawables[2].getIntrinsicWidth())
                        - drawableLeftPadding;
                    radioButton.setPadding(0, 0, paddingEnd, 0);
                }
            }
        });
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    android:padding="16dp">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:drawableEnd="@drawable/ic_insert_emoticon_yellow_24dp"
            android:text="Test Test Test Test Test " />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:drawableEnd="@drawable/ic_insert_emoticon_yellow_24dp"
            android:text="Test" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:drawableEnd="@drawable/ic_insert_emoticon_yellow_24dp"
            android:text="Test" />

    </RadioGroup>

</LinearLayout>
  • 1
Reply Report
      • 1
    • @Nikitc No XML-only solution that I am aware of unless you want to restrict yourself to things that can be represented textually such as emojis. Test😀 will give you text with a grinning face at the end.

Seems like your parent container for RadioButton is having width "match_parent"

Make your parent container width "wrap_content" or replace your snippet with given one as below.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="wrap_content"    //the only change is here
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_arrow_back_black_24dp"
            tools:text="Test"/>

    </RadioGroup>
</LinearLayout>
  • 0
Reply Report