I have created the dynamic view. That view contains two edittext and one radio group. when I click the add button the view is added to the layout. Now I got a confusion, how to get values from these type of dynamic views. I tried but it doesn't work. when I add the two or more views, the loop does not find the next views values. I want to add that values to ArrayList. This is code:
private void addDynamicViews() {
EditText name = new EditText(this);
EditText mobile = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 5, 5);
name.setLayoutParams(p);
name.setBackgroundResource(R.drawable.edittext_box);
name.setHint("Enter Name");
studentslayout.addView(name);
mobile.setLayoutParams(p);
mobile.setBackgroundResource(R.drawable.edittext_box);
mobile.setHint("Enter Mobile No");
studentslayout.addView(mobile);
/* radioGroup - Radio Group
maleButton,femaleButton - Radio Buttons
studentslayout - Linear Layout */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
maleButton = new RadioButton(this);
maleButton.setText("Male");
radioGroup.addView(maleButton);
femaleButton = new RadioButton(this);
radioGroup.addView(femaleButton);
femaleButton.setText("Female");
studentslayout.addView(radioGroup);
}
How to take all dynamic edittext and radio group values ?
I tried this code But unfortunately it stopped.
#Override
public void onClick(View v) {
String[] array = new String[studentslayout.getChildCount()];
int count = studentslayout.getChildCount();
for (int i=0; i < studentslayout.getChildCount(); i++){
editText = (EditText)studentslayout.getChildAt(i);
array[i] = editText.getText().toString();
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
array[i] = radValues.getText().toString();
}
}
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
You have added radioGroup and are expecting radiobutton. Also since you are looping, you should check the type of the view.
You can try something like this:
int childCount = studentslayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = studentslayout.getChildAt(i);
if (childView instanceof EditText) {
EditText editText = (EditText) childView;
String text = editText.getText().toString();
//use text
} else if (childView instanceof RadioGroup) {
RadioGroup radioGroup = (RadioGroup) childView;
int radioCount = radioGroup.getChildCount();
for (int j = 0; j < radioCount; j++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
//use radioButton.
}
}
}
Related
I am working with dynamic RadioGroup and I want to call RadioButton on every click.
I am using OnCheckedChangedListener in the for loop where I am initialising all the RadioGroup. Can anybody tell me what is the right approch for calling RadioButtons in dynamic RadioGroup. Thanks
for (int i = 0; i < radioGroupSizeList.size(); i++) {
final RadioGroup radioGroup = radioGroupSizeList.get(i);
final int radioGroupId = radioGroupSizeList.get(i).getId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group.getId() == radioGroupId) {
int rb = radioGroup.getCheckedRadioButtonId();
List<RadioButton> radioButtonsList = radioMap.get(radioGroupId);
for (int k = 0; k < radioButtonsList.size(); k++) {
RadioButton radioButton = radioButtonsList.get(k);
String getTextRadio = radioButton.getText().toString();
int radioButtonId = radioButtonsList.get(k).getId();
if (rb == radioButtonId) {
sizeList = allSizesVariations.get(radioGroupId);
for (int i = 0; i < sizeList.size(); i++) {
String size = sizeList.get(i).getName();
TextView textView = (TextView) group.getChildAt(i);
textView.setTextColor(getResources().getColor(R.color.Black));
if (getTextRadio.equalsIgnoreCase(size)) {
size_attribute_id = sizeList.get(i).getId();
TextView textView1 = (TextView) group.getChildAt(i);
textView1.setTextColor(getResources().getColor(R.color.white));
GeneralFunctions.showToast(DetailActivity.this, String.valueOf(size_attribute_id));
}
}
}
}
}
}
});
}
Is this meant as a Preference? if so, I would recommend using the Preferences-API with onSharedPreferenceChangeListener.. In this way, you can check if the key is equal to the key set for the specific Radio view in question, and even what it's value is, and carry out your code as needed.. In fact even without it being an actual Preference, you could still utilize this to achieve a solution.
In my app i have created list view of checkbox in linear layout and i have created dynamic radio group of three radio button.when user click on any checkbox than radio group will be created at the end. but i want to create this radio group just below the clicked checked box. i am trying to set cursor position but not able to do.i don't have any idea what to do for this.
this is part of main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);
for (int i = 0; i < 25; i++) {
final CheckBox checkBox = new CheckBox(this);
checkBox.setText(Oils_and_Condiments[i]);
checkboxContainer.addView(checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
for (int i = 0; i < Oils_and_Condiments.length; i++) {
String a= (String) checkBox.getText();
if(a==Oils_and_Condiments[i])
{
RadioGroup rg = create_rb(i);
checkboxContainer.get
checkboxContainer.addView(rg);
rg.setVisibility(View.VISIBLE);
//rg.getChildAt(i).setVisibility(View.VISIBLE);
}
RadioGroup create_rb(int a ){
final RadioGroup radiogp = new RadioGroup(this);
radiogp.setOrientation(RadioGroup.HORIZONTAL);
RadioButton rb0 = new RadioButton(this);
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
rb0.setText("1KG");
rb0.setTextSize(10);
rb1.setText("2KG");
rb1.setTextSize(10);
rb2.setText("3KG");
rb2.setTextSize(10);
radiogp.addView(rb0);
radiogp.addView(rb1);
radiogp.addView(rb2);
//checkboxContainer.addView(radiogp);
return radiogp;
}
I am getting arrayList [5,8,13,18,19] from the server and I would like to create a RadioGroup in the xml file. After selecting the diserable items, I will put the selected items in arrayList and transmit the query to the server after clicking the OK button. How can I create such as programmatically RadioGroup?
I have tried this but I dont know how to loog through to set the RadioButton with the ArrayList value. How can I get that tp work?
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
final ArrayList<RadioButton> rb = new ArrayList<RadioButton>();
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
items.get(i) = new RadioButton(this);
}
}
Try this
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
RadioButton rb = new RadioButton(this);
rb.setText(items.get(i)+"");
rg.addView(rb);
}
With the addView you are adding the dynamically created RadioButton to the RadioGroup.
I have created EditText views Dynamically now I want to get their texts into one list or array, How can I do that?
You have to get the reference for your edittexts while you are creating them dynalically like this..
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
//count is number of edittext fields
ed = new EditText(MyActivity.this);
allEds.add(ed);
linear.addView(ed);
}
now allEds will have the references to your edittexts.. and you can get the text like this..
String [] items=new String[allEds.size()]
for(int i=0; i < allEds.size(); i++){
items[i]=allEds.get(i).getText().toString();
}
You can create two List's one for storing dynamically created EditText's and one for storing their text's.
Then iterate through the EditText list using a for-each loop and get the text of each EditText and add it to List of text's.
List<EditText> myList = new ArrayList<EditText>();
List<String> etText = new ArrayList<String>();
EditText myEt1 = new EditText(this);
EditText myEt2 = new EditText(this);
EditText myEt3 = new EditText(this);
//so on...
myList.add(myEt1);
myList.add(myEt2);
myList.add(myEt3);
for(EditText et : myList){
String settext = et.getText().toString();
etText.add(settext);
}
if your problem is accessibility to these views:
// assume ll is your container LinearLayout and your EditTexts inserted in it
LinearLayout ll;
int nViews = ll.getChildCount();
for (int i = 0; i < nViews; i++) {
View child = ll.getChildAt(i);
if (child instanceof EditText ) {
EditText edt= (EditText) child;
//...
}
}
I want to create RadioGroup, with RadioButton list inside it, in the onCreate function. I want to do it as exercise w/o using xml-layout. Is it possible? Thanks.
Something like this:
....
RadioGroup group = new RadioGroup(this);
group.setOrientation(RadioGroup.HORIZONTAL);
RadioButton btn1 = new RadioButton(this);
btn1.setText("BTN1");
group.addView(btn1);
RadioButton btn2 = new RadioButton(this);
group.addView(btn2);
btn2.setText("BTN2");
....
RadioButton btnN = new RadioButton(this);
group.addView(btnN);
btnN.setText("BTNN");
yourLayout.addView(group);
....
This will do the job:
int buttons = 5;
RadioGroup rgp = new RadioGroup(getApplicationContext());
for (int i = 1; i <= buttons; i++) {
RadioButton rbn = new RadioButton(this);
rbn.setId(1 + 1000);
rbn.setText("RadioButton" + i);
//Attach button to RadioGroup.
rgp.addView(rbn);
}
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.addView(rgp);
This is a complete example:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Defining buttons quantity!
int buttons = 5;
//Create a new instance of RadioGroup.
RadioGroup rgp = new RadioGroup(getApplicationContext());
//Create buttons!
for (int i = 1; i <= buttons; i++) {
RadioButton rbn = new RadioButton(this);
rbn.setId(1 + 1000);
rbn.setText("RadioButton" + i);
//Attach button to RadioGroup.
rgp.addView(rbn);
}
//Get the root view.
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.addView(rgp);
}
}
And this is the result:
If you need to use a RadioGroup defined into the xml layout and add dinamically buttons see this answer.