dynamic radio button layout not work - android

I'm trying to make a dynamic radio button to linear layout. I have two radio buttons which are called from List and added to Linear Layout. Everything works fine until I get bad looks at my radio button and get stuck with this layout. My question is why does my radio button layout not work at all?
Here is my code :
TextView label;
RadioButton[] rb;
RadioGroup radiogroup;
LinearLayout ll;
String title;
public MyBtn(Context context,String labelText,String options) {
super(context);
ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
label = new TextView(context);
label.setText(labelText);
label.setTextColor(getResources().getColor(R.color.label_color));
radiogroup = new RadioGroup(context);
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
radiogroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam.weight = 1f;
rbParam.gravity = Gravity.CENTER | Gravity.LEFT;
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rbParam2.gravity = Gravity.CENTER | Gravity.RIGHT;
rbParam2.weight = 1f;
rbLayout[0] = new RadioGroup.LayoutParams(rbParam);
rbLayout[1] = new RadioGroup.LayoutParams(rbParam2);
List<String> list = Arrays.asList(options.split("\\|"));
String[] opts = new String[list.size()];
list.toArray(opts);
rb = new RadioButton[list.size()];
for (int i = 0; i < list.size(); i++) {
rb[i] = new RadioButton(context);
rb[i].setText(opts[i]);
rb[i].setId(i);
radiogroup.addView(rb[i],rbLayout[i]);
}
title = label.getText().toString();
radiogroup.check(0);
ll.addView(label);
ll.addView(radiogroup);
//int resID = getResources().getIdentifier(buttonID, "id", context.getPackageName());
this.addView(ll);
}
Thank you.
My radio button :
image link
It should :
image link

change this:
LinearLayout.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
LinearLayout.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
to this:
RadioGroup.LayoutParams [] rbLayout = new RadioGroup.LayoutParams[2];
RadioGroup.LayoutParams rbParam2 = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
there are many same mistake in your code. find out them and correct
let me know about result
LinearLayout.LayoutParams rbParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1f);
that must work.
let me know about it

Related

Submit json request with inputs of a form created dynamically

I'm creating of form with inputs received by an API request, I'm using switch to create each form with it's 'type' , for example if type=='text' var = editText , type=='title' var = 'textview' and if type='submit' var = button.
I'm using volley with the APIs, I need to submit the form after user submits it, but I didn't know how to get the list of variables and it's data (because they are dynamically created I don't know).
switch (type) {
case "text":
LinearLayout lyt = new LinearLayout(context);
lyt.setOrientation(LinearLayout.HORIZONTAL);
ViewGroup.LayoutParams prms = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lyt.setLayoutParams(prms);
TextView textViewLabel = new TextView(context);
textViewLabel.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textViewLabel.setTypeface(null, Typeface.BOLD);
textViewLabel.setText(rowObj.getString("label"));
textViewLabel.setMaxWidth(150);
lyt.addView(textViewLabel);
EditText editText = new EditText(context);
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsL.setMargins(15, 0,0,0);
if(rowObj.has("maxLength")) {
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(Integer.parseInt(rowObj.getString("maxLength")));
editText.setFilters(fArray);
}
editText.setLayoutParams(paramsL);
editText.setSingleLine(true);
editText.setWidth(500);
editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
lyt.addView(editText);
parent.addView(lyt);
break;
case "textarea":
LinearLayout lyt2 = new LinearLayout(context);
lyt2.setOrientation(LinearLayout.HORIZONTAL);
ViewGroup.LayoutParams prms2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lyt2.setLayoutParams(prms2);
TextView textViewLabel2 = new TextView(context);
textViewLabel2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textViewLabel2.setTypeface(null, Typeface.BOLD);
textViewLabel2.setMaxWidth(150);
textViewLabel2.setText(rowObj.getString("label"));
lyt2.addView(textViewLabel2);
EditText editText2 = new EditText(context);
LinearLayout.LayoutParams paramsL1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsL1.setMargins(15, 0,0,0);
if(rowObj.has("maxLength")) {
InputFilter[] fArray1 = new InputFilter[1];
fArray1[0] = new InputFilter.LengthFilter(Integer.parseInt(rowObj.getString("maxLength")));
editText2.setFilters(fArray1);
}
editText2.setLayoutParams(paramsL1);
editText2.setSingleLine(true);
editText2.setWidth(500);
editText2.setHeight(300);
editText2.setEms(10);
editText2.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5.0f, getResources().getDisplayMetrics()), 1.0f);
editText2.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE|InputType.TYPE_CLASS_TEXT);
editText2.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
lyt2.addView(editText2);
parent.addView(lyt2);
break;
case "image":
LinearLayout lLayout1 = new LinearLayout(context);
ViewGroup.LayoutParams params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lLayout1.setLayoutParams(params1);
ImageView imageView = new ImageView(context);
imageView.setPadding(5,5,5,5);
Picasso.with(context).load(rowObj.getString("src")).into(imageView);
lLayout1.addView(imageView);
parent.addView(lLayout1);
break;
case "title":
TextView textView = new TextView(context);
ViewGroup.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, Float.parseFloat(rowObj.getString("colSpan")));
textView.setLayoutParams(params2);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setTypeface(null, Typeface.BOLD);
textView.setText(rowObj.getString("label"));
parent.addView(textView);
break;
case "subtitle":
TextView textView1 = new TextView(context);
ViewGroup.LayoutParams params3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
textView1.setLayoutParams(params3);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView1.setText(rowObj.getString("label"));
parent.addView(textView1);
break;
case "file":
break;
case "radio":
LinearLayout lyt3 = new LinearLayout(context);
lyt3.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams prms3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
lyt3.setLayoutParams(prms3);
TextView textViewLabel3 = new TextView(context);
textViewLabel3.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textViewLabel3.setTypeface(null, Typeface.BOLD);
textViewLabel3.setText(rowObj.getString("label"));
lyt3.addView(textViewLabel3);
// Initialize a new RadioGroup
RadioGroup rg = new RadioGroup(context);
rg.setOrientation(RadioGroup.VERTICAL);
if(rowObj.has("options")){
JSONArray options = rowObj.getJSONArray("options");
if(options.length()>0){
for (int i=0; i<options.length(); i++){
JSONObject radioObj = options.getJSONObject(i);
// Create another Radio Button for RadioGroup
RadioButton rb = new RadioButton(context);
rb.setText(radioObj.getString("label"));
rb.setTextColor(Color.BLACK);
rg.addView(rb);
}
}
}
lyt3.addView(rg);
parent.addView(lyt3);
break;
case "submit":
Button button = new Button(context);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, Float.parseFloat(rowObj.getString("colSpan")));
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
button.setSingleLine(true);
button.setText(rowObj.getString("label"));
button.setTextColor(Color.WHITE);
parent.addView(button);
break;
case "hidden":
break;
}
}else {
TextView view = new TextView(context);
ViewGroup.LayoutParams params3 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, Float.parseFloat(rowObj.getString("colSpan")));
view.setLayoutParams(params3);
view.setBackgroundColor(Color.WHITE);
view.setText("");
parent.addView(view);
}
}
I want to put the variables in a JSON array to send a POST request :
[
{
"variable1": "value1",
"variable2": "value2",
...
}
]
public class Test extends Activity{
private String variableOne;
private String variableTwo;
switch (whatever) {
case "text":
varbiableOne = "firstvaluetext";
variableTwo="secondvaluetext"
break;
case "textarea":
varbiableOne = "firstvaluetextarea";
variableTwo="secondvaluetextarea"
break;
default:
//whatever
break;
}
There you have your variables and you can build your json out of it.
Really hard to get what you want.

Change gravity header in table layout Android

I have this
public void header(int recursocabecera)
{
TableRow.LayoutParams layoutCelda;
TableRow fila = new TableRow(actividad);
TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
fila.setLayoutParams(layoutFila);
String[] arraycabecera = rs.getStringArray(recursocabecera);
COLUMNAS = arraycabecera.length;
for(int i = 0; i < arraycabecera.length; i++)
{
TextView texto = new TextView(actividad);
// layoutCelda = new TableRow.LayoutParams(obtenerAncho(arraycabecera[i]), LayoutParams.FILL_PARENT);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(arraycabecera[i]), LayoutParams.FILL_PARENT);
// layoutCelda= new LayoutParams(100, ViewGroup.LayoutParams.FILL_PARENT);
texto.setSingleLine(true);
texto.setText(arraycabecera[i]);
texto.setGravity(Gravity.CENTER_HORIZONTAL);
texto.setRotation(-90);
texto.setTextAppearance(actividad, R.style.estilo_celda2);
texto.setBackgroundResource(R.drawable.tabla_celda_cabecera2);
texto.setLayoutParams(layoutCelda);
texto.setPadding(20,0,0,100);
layoutCelda.setMargins(0,0,60,0);
layoutCelda.setMarginEnd(90);
fila.addView(texto);
}
tabla.addView(fila);
filas.add(fila);
FILAS++;
}
the result is somenthing like that
but I need it to be something like this
setGravity it works half, but the titles are shown cropped
My result:

How to get ID of multiple EditText dynamically added?

I've added multiple EditText and TextViews dynamically like:
final String[] meses = new String[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
for(int i=0; i<meses.length; i++){
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textview = new TextView(getContext());
ViewGroup.LayoutParams lparams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
textview.setLayoutParams(lparams);
textview.setText(meses[i]);
layout.addView(textview);
EditText ed = new EditText(getContext());
ed.setLayoutParams(lparams);
ed.setId(i);
layout.addView(ed);
llMes.addView(layout);
}
But now I need to retrieve the values of each EditText, how can I retrieve it, I have this:
for(int i=0; i < meses.length; i++){
EditText et = null;
et.getId();
pago = quotas - Integer.parseInt(et.getText().toString());
}
And also tried with findViewById(i) with for but also failed.
Thanks for helping me!
Add EditTexts to an ArrayList then retrieve it by index.
//Arraylist that will contain EditTexts
ArrayList<EditText> list = new ArrayList<>();
// for each EditText add it to the ArrayList
for(int i=0; i < 10; i++){
EditText et = new EditText();
list.add(et);
}
//To retrieve EditText
EditText et = list.get(0);

How to center a TableRow programmatically

I am creating a number of buttons in a table row programmatically, but they always end up left-aligned. How can I center the entire row? I am trying this to create each row:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);
You can simply center the row using a Gravity. Just set it to your row with
row.setGravity(Gravity.CENTER_HORIZONTAL);
Try this:
TableRow row = new TableRow(getActivity());
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(params);
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
for (int j = 0; j != numButtonCols; j++) {
Button b = new Button(getActivity());
b.setLayoutParams(buttonParams);
b.setOnClickListener(actionListener);
row.addView(b);
actionButtons[j][i] = b;
}
layout.addView(row);

how can I properly configure the HorizontalScrollView and the Scrollview?

I have this TableRow of EditText inserted in a tablelayout, this tablelayout is inserted in one Scrollview, this Scrollview is inserted in a HorizontalScrollView. HorizontalScrollView contains, Also, an image of the background. The problem is that I can't scroll properly and display all the EditText. Can you help me?
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
HSC.setBackgroundResource(R.drawable.lavagna);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
values = new EditText[r][c];
for (int i = 0; i < r; i++){
tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < c; j++){
values[i][j] = new EditText(this);
values[i][j].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
values[i][j].setHint("r: " + (i) + " " + "c: " + (j));
values[i][j].setPadding(10, 10, 10, 10);
tableRow.addView(values[i][j]);
}
Remove Gravity.CENTER parameters, or the scroller will scroll from the center of its content that is big enough. It's there, but you can't get it.
Correct various values
Code:
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
HSC.setBackgroundResource(R.drawable.lavagna);
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// you may or may not change here and below
// values = new EditText[15][15];
for (int i = 0; i < 15; i++){
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
for (int j = 0; j < 15; j++){
EditText value = new EditText(this);
value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
value.setHint("r: " + (i) + " " + "c: " + (j));
value.setPadding(10, 10, 10, 10);
tableRow.addView(value);
}
tableLayout.addView(tableRow);
}
VSC.addView(tableLayout);
HSC.addView(VSC);
frameLayout.addView(HSC);
// ... add frameLayout
// ...
}

Categories

Resources