2

I am doing an inventory management Android app where I am programmatically and dynamically creating rows to put in my xml-defined tablelayout. For every item, I create a tablerow containing 2 views: a textview containing the name of the inventory item, and a radiogroup containing 3 radiobuttons: present, missing, and repairs needed. There is also a linearlayout below each row in the tablelayout that contains a textview and an edittext, but that part isn't having any issues.

This is my screenshot showing what it currently looks like: https://preview.ibb.co/k8a7Yw/Screenshot_20171011_170618.png

Right now, the radio buttons display fine on my phone screen (after setting shrinkcolumn=0 in the xml for the tablelayout so the text of the 3rd radiobutton doesn't get cut off), but the item name gets squished. What I would like to happen is to move the last radio button (repairs needed) down a line, but keep it in the radiogroup and row so I don't have to go through creating custom classes inheriting from radiogroup to get it to toggle right.

Current code:

public class PMR {
    String result;
    String caption;
    String repairString;
    EditText repairEditText;
    RadioButton pRadioButton;
    RadioButton mRadioButton;
    RadioButton rRadioButton;

    public PMR(){
    }
    public PMR(final TableLayout tableLayout, final TableRow tableRow, 
    final Context context, String caption){
        this.caption=caption;
        result="incomplete";
        repairString=null;
        final RadioGroup radioGroup=new RadioGroup(context);
        radioGroup.setTag("incomplete");
        radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        tableRow.addView(radioGroup);

        pRadioButton = new RadioButton(context);
        pRadioButton.setText("Present");
        pRadioButton.setId(generateViewId());
        final int pId=pRadioButton.getId();

        mRadioButton = new RadioButton(context);
        mRadioButton.setText("Missing");
        mRadioButton.setId(generateViewId());
        final int mId=mRadioButton.getId();

        rRadioButton = new RadioButton(context);
        rRadioButton.setText("Repairs Needed");
        rRadioButton.setId(generateViewId());
        final int rId=rRadioButton.getId();

        radioGroup.addView(pRadioButton);
        radioGroup.addView(mRadioButton);
        radioGroup.addView(rRadioButton);

        final LinearLayout textLayout=new LinearLayout(context);
        textLayout.setOrientation(LinearLayout.HORIZONTAL);
        tableLayout.addView(textLayout);
        textLayout.setTag("Text Row");

        final TextView labelText = new TextView(context);
        labelText.setText("");
        textLayout.addView(labelText);

        repairEditText = new EditText(context);
        repairEditText.setEnabled(false);
        repairEditText.setText("");
        textLayout.addView(repairEditText);

        final String prefix="Notes: ";

        radioGroup.setOnCheckedChangeListener(new 
        RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged (RadioGroup group, 
            int checkedId){

                if (checkedId!=rId && 
                repairEditText.getText().toString()!=null){
                   repairString=repairEditText.getText().toString();
                }
                if (checkedId == pId) {
                    result="Present";
                    repairEditText.setText("");
                    repairEditText.setEnabled(false);
                    labelText.setText("");
                    radioGroup.setTag("pTag");
                } else if (checkedId == mId) {
                    result="Missing";
                    repairEditText.setText("");
                    repairEditText.setEnabled(false);
                    labelText.setText("");
                    radioGroup.setTag("mTag");
                } else if (checkedId == rId) {
                    result="Repairs Needed";
                    if (repairString!=null){
                        repairEditText.setText(repairString);
                    }
                    repairEditText.setEnabled(true);
                    repairEditText.requestFocus();
                    labelText.setText(prefix);
                    radioGroup.setTag("rTag");
                }
                else {
                    result = "incomplete";
                    radioGroup.setTag("incomplete");
                }
            }
        });
    }
}

With the tablelayout xml:

<TableLayout
                android:id="@+id/tableLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:divider="?android:attr/dividerHorizontal"
                android:showDividers="middle"
                android:shrinkColumns="0"
                >
</TableLayout>

This is basically what I am trying to get my tablerows to look like: https://image.ibb.co/i5qrtw/goodtablerowpmr.png

devander
  • 21
  • 2
  • https://stackoverflow.com/questions/11891105/have-horizontal-radiobuttons-wrap-if-too-long-for-screen – HomeIsWhereThePcIs Oct 11 '17 at 21:50
  • Neither of the two responses for that post answered the posed problem; the first doesn't work for radiogroups, and the second only moves the additional buttons down if it runs out of room. I want my rows to be uniform and all have the 2 buttons over the 1 button look. – devander Oct 11 '17 at 22:22
  • The first answer uses the referenced MultiLineRadioGroup class which extends RadioGroup. – HomeIsWhereThePcIs Oct 11 '17 at 22:41
  • They switched order. The multilineradiogroup class only wraps if it runs out of room. I want it set where the buttons are 2 over 1, regardless of how long the text on the left is. – devander Oct 12 '17 at 14:47
  • I guess you could achieve the same thing by changing the layout logic in the MultiLineRadioGroup class. if(nextBottom > maxBottom){ // if current button will exceed border for this column ... you could change this with something like: if (x>2 && x%2==1) – HomeIsWhereThePcIs Oct 12 '17 at 16:57

2 Answers2

0

The MultiLineRadioGroup class suggested just didn't work in multiple ways, so I went back to the drawing board. The solution I found was just skip out on radio groups altogether; instead of a radiogroup in the table row, I placed a RelativeLayout containing the three radiobuttons, and used LayoutParams to place the buttons in the manner I wanted them and set their width/height/weight. I then created separate onClickListener's for each radiobutton instead of the onCheckedChangeListener. So while I did end up having to write my own class and function, it did turn out to be worth it. My custom class:

public class FancyRadioGroup{
    public FancyRadioGroup(Context context, TableRow tableRow, TextView 
textView, RadioButton pButton, RadioButton mButton, RadioButton rButton){
        TableRow.LayoutParams rowParams= new 
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
TableRow.LayoutParams.MATCH_PARENT, 1.0f);
        textView.setLayoutParams(rowParams);

        RelativeLayout relativeLayout = new RelativeLayout(context);
        tableRow.addView(relativeLayout);
        relativeLayout.setLayoutParams(rowParams);

        relativeLayout.addView(pButton);
        relativeLayout.addView(mButton);

        RelativeLayout.LayoutParams topLParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
        topLParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        pButton.setLayoutParams(topLParams);

        RelativeLayout.LayoutParams topRParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
        topRParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        topRParams.addRule(RelativeLayout.RIGHT_OF,pButton.getId());
        mButton.setLayoutParams(topRParams);

        RelativeLayout.LayoutParams botParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
        botParams.addRule(RelativeLayout.BELOW,mButton.getId());
        relativeLayout.addView(rButton);
        rButton.setLayoutParams(botParams);
    }
}

And then the edited existing class:

public PMR(final TableLayout tableLayout, final TableRow tableRow, final 
TextView textView, final Context context, String caption){
    this.caption=caption;
    textView.setTag("incomplete");
    repairString=null;

    pRadioButton = new RadioButton(context);
    pRadioButton.setText("Present");
    pRadioButton.setId(generateViewId());
    final int pId=pRadioButton.getId();

    mRadioButton = new RadioButton(context);
    mRadioButton.setText("Missing");
    mRadioButton.setId(generateViewId());
    final int mId=mRadioButton.getId();

    rRadioButton = new RadioButton(context);
    rRadioButton.setText("Repairs Needed");
    rRadioButton.setId(generateViewId());
    final int rId=rRadioButton.getId();

    final FancyRadioGroup fancyRadioGroup = new FancyRadioGroup(context, 
tableRow, textView, pRadioButton, mRadioButton, rRadioButton);

    final LinearLayout textLayout=new LinearLayout(context);
    textLayout.setOrientation(LinearLayout.HORIZONTAL);
    tableLayout.addView(textLayout);
    textLayout.setTag("Text Row");

    final TextView labelText = new TextView(context);
    labelText.setText("");
    textLayout.addView(labelText);

    repairEditText = new EditText(context);
    repairEditText.setEnabled(false);
    repairEditText.setText("");
    textLayout.addView(repairEditText);

    final String prefix="Notes: ";
    pRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRadioButton.setChecked(false);
            if (repairEditText.isEnabled()){
                repairEditText.setEnabled(false);
                labelText.setText("");
            }
            textView.setTag("Present");
            rRadioButton.setChecked(false);
        }
    });
    mRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pRadioButton.setChecked(false);
            if (repairEditText.isEnabled()){
                repairEditText.setEnabled(false);
                labelText.setText("");
            }
            textView.setTag("Missing");
            rRadioButton.setChecked(false);
        }
    });
    rRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRadioButton.setChecked(false);
            pRadioButton.setChecked(false);
            repairEditText.setEnabled(true);
            repairEditText.requestFocus();
            labelText.setText(prefix);
            textView.setTag("Repairs Needed");
        }
    });
}
devander
  • 21
  • 2
0

Inside of the RadioGroup, create a LinearLayout and place the first line radio buttons. If you need only 1 radio button on next line, just place that RadioButton as direct child of the RadioGroup. Like this,

<RadioGroup>
    <LinearLayout>
        ... 
    </LinearLayout>

    <RadioButton>
    </RadioButton>
</RadioGroup>

Updated: The above solution didn't work. The solution here works fine