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);
Related
The orientation of the Radio Group is horizontal. I am creating Radio Group and Radio Buttons Dynamically . It is displaying the radio buttons with respect to the device screen size. How to align the Radio Buttons in multiple rows if they are in large number.
Many Thanks.
if (status.equals("success")) {
JSONArray jarry = jobj
.getJSONArray("questions");
for (int i = 0; i < jarry.length(); i++) {
LinearLayout ll = new LinearLayout(
RajaAndroid.this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.VERTICAL);
JSONObject jo = jarry.getJSONObject(i);
// rg = new RadioGroup(RajaAndroid.this);
RadioGroup rg = new RadioGroup(
RajaAndroid.this); // create the
// RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or
// RadioGroup.VERTICAL
TextView textView = new TextView(
RajaAndroid.this);
textView.setText(jo.getString("q_id")
+ ". " + jo.getString("question"));
textView.setTextColor(0xff0000ff);
textView.setTextSize(20);
layout.addView(textView, p);
layout.addView(rg, p);
JSONArray ja = jo.getJSONArray("options");
for (int j = 0; j < ja.length(); j++) {
JSONObject jo1 = ja.getJSONObject(j);
RadioButton rb = new RadioButton(
RajaAndroid.this);
ImageView iv = new ImageView(
RajaAndroid.this);
iv.setImageResource(R.drawable.excellent);
Drawable db = getDrawable(R.drawable.ic_launcher);
// are added to the radioGroup
// instead of the layout
rb.setText(jo1.getString("option_name"));
rb.setTextColor(0xff000000);
rb.setTextSize(18);
rb.setCompoundDrawablesRelativeWithIntrinsicBounds(
null, db, null, null);
rb.setGravity(Gravity.BOTTOM);
rb.setPadding(0, 0, 0, 5);
rb.setOnClickListener(RajaAndroid.this);
//rg.addView(iv, j);
rg.addView(rb);
}
}
} else {
Alerts.alertWithOk(RajaAndroid.this,
"No Questions found", "Ok");
}
i create two edittext programmatically and i need to get text from him
this is my code for create edittext
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
//First EditText
edText = new EditText(getActivity());
edText.setId(0);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setBackgroundResource(R.drawable.edittext);
edText.setPadding(5, 5, 5, 5);
linear=(LinearLayout)rootView.findViewById(R.id.edittextpanel);
linear.addView(edText ,layoutParams);
nb++;
//Second EditText
edText = new EditText(getActivity());
edText.setId(1);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setPadding(5, 5, 5, 5);
edText.setBackgroundResource(R.drawable.edittext);
linear.addView(edText ,layoutParams);
Help please i'm blocked
edText = new EditText(getActivity());
edText.setId(id);
String stringAnswer = edText.getText().toString();
If you want to create and reference more than one edit text you'll have to create an EditText array
List<EditText> allEDs = new ArrayList<EditText>();
Then create the Edit Texts
edText = new EditText(getActivity());
edText.setId(id);
Push them into the array
allEDs.add(edText);
Create a string array same size as the EditText array
String[] ETResults = new String[allEDs.size()];
Loop through the EditText array and put converted results into String array
for(int i=0; i < allEDs.size(); i++){
ETResults[i] = allEDs.get(i).getText().toString();
}
And then assign the desired string to whatever you want
String stringAnswer = ETResults[i];
try {
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);
for(int i = 0; i <viewGroup.getChildCount(); i++){
if(viewGroup.getChildAt(i) instanceof EditText) {
EditText view = (EditText)viewGroup.getChildAt(i);
Log.i("val " , " val " + ((EditText) viewGroup.getChildAt(i)).getText() + " view "+ view.getText() );
}
}
}catch (Exception ex){
Log.e("Error in Bind layout", ex.toString());
}
Maintain a reference to both EditTexts. Put the following in the main body of your Fragment to make them globally accessible..
EditText edText1;
EditText edText2;
Then in the method you've shown you can use...
// Do some stuff
edText1 = new EditText(getActivity());
// Do some more stuff
edText2 = new EditText(getActivity());
...
Later when you need to get the text you can use...
String text1 = edText1.getText().toString();
String text2 = edText2.getText().toString();
Alternatively you can find them later in your Fragment using their ids...
LinearLayout linear = (LinearLayout) findViewById(R.id.edittextpanel);
EditText et = (EditText) linear.findViewById(0);
String text1 = et.getText().toString();
et = (EditText) linear.findViewById(1);
String text2 = et.getText().toString();
I have developed a form in dynamically using some edit text,check boxes,radio buttons and buttons.I have created form successfully ,but how i can get the values from that and stored in database.Please can any one help me.
Thanking in Advance.
public void textView() {
idText++;
i++;
LinearLayout linearLayoutHorizantal1 = new LinearLayout(
getApplicationContext());
linearLayoutHorizantal1.setOrientation(LinearLayout.HORIZONTAL);
/*
* LinearLayout.LayoutParams params = new
* LinearLayout.LayoutParams(LayoutParams
* .WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
* linearLayoutHorizantal1.setGravity(Gravity.RIGHT);
*/
txtQuetion = new TextView(getApplicationContext());
txtQuetion.setText(i + ". " + strQuestionText);
txtQuetion.setTextColor(Color.BLACK);
txtQuetion.setTextSize(20);
txtQuetion.setId(idText);
linearLayoutHorizantal1.addView(txtQuetion);
linearLayoutDynamicAdd.addView(linearLayoutHorizantal1);
}
public void RadioButtons() {
radiogroup = new RadioGroup(getApplicationContext());
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
SurveyFillActivity.this, null);
params.setMargins(10, 10, 10, 0);
linearLayoutDynamicAdd.addView(radiogroup);
for (int j = 0; j < answersList.length; j++) {
if (answersList[j] != null) {
idRadio++;
rb = new RadioButton(getApplicationContext());
rb.setId(idRadio);
rb.setText(answersList[j]);
rb.setTextColor(Color.BLACK);
rb.setButtonDrawable(R.drawable.radio_custom);
// rb.setChecked(true);
rb.setLayoutParams(params);
radiogroup.addView(rb);
}
}
}
private EditText editText(int _intID) {
idEditBox++;
final LayoutParams lparams = new LayoutParams(350, 50);
final EditText et = new EditText(this);
lparams.leftMargin = 20;
lparams.topMargin = 10;
et.setLayoutParams(lparams);
et.setWidth(32);
et.setEms(50);
et.setBackgroundResource(R.drawable.customborder_backbutton);
/*
* EditText et = new EditText(getApplicationContext());
* et.setId(idEditBox); et.setHeight(60); et.setWidth(50);
*/
//et.setBackgroundColor(Color.WHITE);
String s1 = et.getText().toString();
linearLayoutDynamicAdd.addView(et, lparams);
return et;
}
The above code for creating forms and i need to get the values form it and store in Database.
Create the arrays for texviews and radiobuttons ...what ever you need in your form.
take this as reference
TextView tv[] = new TextView[HowManyYouNeed];
TextView tvsa = new TextView(YourContax);
tv[position]=tvsa;//position like o ..1..2
tv[0].getText();
May be help full..
I am making an android application, which creates a dynamic interface, according to a string read from your preferences:
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String Favs = SP.getString("Favs", "None");
String[] Genveje;
if (!Favs.equals("None"))
{
Genveje = Favs.split(";");
tr = new TableRow[Genveje.length];
for (int i = 0; i < Genveje.length; i++)
{
String BtnName = Genveje[i].split("#")[0];
String BtnPath = Genveje[i].split("#")[1];
Button btn = new Button(this);
btn.setText(BtnName);
btn.setTag(BtnPath);
btn.setOnClickListener(this);
btn.setOnLongClickListener(this);
tr[i].addView(btn);
.........
}
The problem is: when i try to do ANYTHING with tr[i] i get a NullPointerException, and i have no clue why.
Anyone with a suggestion?
tr[i]=new TableRow(this);
tr[i].addView(btn);
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