I have some code that generates in the activity some radiobuttons:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
What I want to do is be able to verify which ones (radiobuttons) are checked when I click a button:
public void onAddAnswer(View v){
position++;
delete();
drawRAnswers(position);
}
For now, what this button does is only load the next set of radiobuttons in the view and delete the current ones, but I don't check which ones are selected. Do you know how I could do this in this onAddAnswer method? I would like to put the text of each selected radiobutton in a list.
Thanks.
I managed to figure this one out myself. Here is what I have done:
public class Activity extends AppCompatActivity {
RadioGroup currentGroup;
...
public void onAddAnswer(View v){
if (currentGroup.getCheckedRadioButtonId()==-1){
Toast.makeText(getApplicationContext(), "Veuillez sélectionner au moins une réponse",
Toast.LENGTH_LONG).show();
} else {
int selectedId = currentGroup.getCheckedRadioButtonId();
RadioButton selectedRadio = (RadioButton)findViewById(selectedId);
Toast.makeText(getApplicationContext(), selectedRadio.getText().toString()+" is selected",
Toast.LENGTH_SHORT).show();
position++;
delete();
updateData(position);
}
}
...
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
currentGroup = l1;
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
}
So basically what I am doing is have a RadioGroup for every time I draw radiobuttons, match it with the currentGroup and check if I have (or not) any radiobuttons selected. If yes, then I find out which one.
Related
Sometimes when i want to select choices in radio buttons there is something wrong with radio buttons which i can select Two radios However sometimes is right as default way it must be worked.
Here two sample in my application :
Here's my Code :
span.replace(0,0,getText(R.string.ForeSingleChoice));
new DynamicViews().makeNormalContent(getApplicationContext(),span,linearLayout,16,16,16,16,16.0f);
span.clear();
radioGroup = new DynamicViews().makeRadioGroup(getApplicationContext(),linearLayout);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop1_2),-1);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop2_2),5);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop3_2),-1);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop4_2),-1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
case 5: goToNextLevel= true ;
break;
default: goToNextLevel = false;
break;
}
}
});
DynamicViews makeRadioButton:
public RadioButton makeRadioButton(Context context, RadioGroup radioGroup, String text, int Id) {
RadioButton radioButton = new RadioButton(context);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
radioButton.setBackgroundResource(R.drawable.backgroundbetweenradios);
radioButton.setLayoutParams(Params);
radioButton.setPadding(dpToPx(8,context) , dpToPx(8,context) , dpToPx(8,context) , dpToPx(8,context));
radioButton.setId(Id);
radioButton.setText(text);
radioButton.setTextColor(Color.BLACK);
radioGroup.addView(radioButton);
return radioButton;
}
DynamicViews makeRadioGroup:
public RadioGroup makeRadioGroup(Context context, LinearLayout linearLayout) {
RadioGroup radioGroup = new RadioGroup(context);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(Params);
radioGroup.setOrientation(LinearLayout.VERTICAL);
CardView cardView = new CardView(context);
cardView.setCardBackgroundColor(Color.WHITE);
cardView.setCardElevation(8.0f);
Params.setMargins(dpToPx(16,context) , dpToPx(16,context) , dpToPx(16,context) , dpToPx(16,context));
cardView.setLayoutParams(Params);
cardView.addView(radioGroup);
linearLayout.addView(cardView);
return radioGroup;
}
My application is consists of a lot of radiobuttons ,This is the approach i've made these radio buttons , what's the problem ? is this a bug ?
Have you tried to give each radio button unique id?
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop1_2),1);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop2_2),5);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop3_2),2);
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,getString(R.string.Choice_Q2Mathematicsop4_2),3);
add this on your onCreate()
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
rbtn1.setChecked(yesrbtn == buttonView);
rbtn2.setChecked(norbtn == buttonView);
....
}
}
};
rbtn1.setOnCheckedChangeListener(listener);
rbtn2.setOnCheckedChangeListener(listener);
what this does is making sure that radiobuttons are not allowed to have 2 or more selection
I am looking for something similar in android where I can add radiobuttons to each cells in Table and get their position. Does anyone has worked on it before, if you can share the sample code would be helpful. Thanks
The following pseudocode should get you started:
//create a radio group
RadioGroup rg = new RadioGroup(context);
RadioGroup.LayoutParams rgParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
rg.setLayoutParams(rgParams);
//add the radio group to the row.
rowView.addView(rg);
//add radio buttons
for (i = 0; i < length; i++) {
RadioButton btn = new RadioButton(context);
btn.setText(description[i]);
//or save an object which will hold all information you need
btn.setTag(rowNumber);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, v.getTag());
}
});
if (<some condition>) {
btn.setChecked(true);
}
rg.addView(btn);
}
i have added some button in a layout:
LinearLayout row = (LinearLayout)findViewById(R.id.KeysList);
keys=db.getKeys(console);
my_button=new Button[keys.size()];
for (bt=0;bt<keys.size();bt++){
my_button[bt]=new Button(this);
my_button[bt].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
my_button[bt].setText(keys.get(bt));
my_button[bt].setId(bt);
row.addView(my_button[bt]);
my_button[bt].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (my_button[bt].getId() == ((Button) v).getId()){
Toast.makeText(getBaseContext(), keys.get(bt), 0).show();
}
}
});
}
I want to know which button is clicked and how to get text of the clicked button?And I think using bt here dose not seem to work!
This code is running. I hope it help you :)
final ArrayList<String> Keys = new ArrayList<String>();
for(int i = 0; i < 10; i ++){
Keys.add("Keys is : " + String.valueOf(i));
}
LinearLayout Row = (LinearLayout)findViewById(R.id.KeysList);
final Button[] my_button = new Button[Keys.size()];
for (int bt = 0; bt < Keys.size(); bt ++){
final int Index = bt;
my_button[Index] = new Button(this);
my_button[Index].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
my_button[Index].setText(Keys.get(Index));
my_button[Index].setId(Index);
my_button[bt].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (my_button[Index].getId() == ((Button) v).getId()){
Toast.makeText(getBaseContext(), Keys.get(Index), 0).show();
}
}
});
Row.addView(my_button[Index]);
}
ExampleProject id : Your project
You should probably use View#setTag to set some arbitrary data you'd like associate with the Button. Then you can just instantiate only one OnClickListener that then uses getTag and acts on that data in whatever way you need.
Another way is to have your Activity listen to all button clicks and then you just filter respective to the ID. You should not get the text of the button and use that at all. You should use your own type of identifier, ideally the idea should be enough. Or perhaps you use setTag as #qberticus described.
Consider This example :
public class MainActivity extends Activity implements View.OnClickListener
{
LinearLayout linearLayout;
Button [] button;
View.OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.parent_lay);
String[] array={"U123","U124","U125"};
int length=array.length;
System.out.println("11111111111111111111111111");
button=new Button[length];
for(int i=0;i<length;i++)
{
button[i]=new Button(getApplicationContext());
button[i].setId(i);
button[i].setText("User" + i);
button[i].setOnClickListener(this);
linearLayout.addView(button[i]);
}
}
#Override
public void onClick(View view)
{
view.getId();
Button button=(Button)findViewById(view.getId());
button.setText("Changed");
}
}
This works fine :)
I am new to android development. I want to create RadioButton group programmetically in java class and set the text to the individual radio buttons.
Please Help on this
Thanks in Advance.
This program block shows how to create RadioButton group programmatically.
Hope it will help you.
public class RadioGroupActivity extends Activity {
protected static final String TAG = "RadioGroupActivity";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiogroup);
RadioGroup radGrp = (RadioGroup)findViewById(R.id.radGrp);
int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
switch(id) {
case -1:
Log.v(TAG, "Choices cleared!");
break;
case R.id.chRBtn:
Log.v(TAG, "Chose Chicken");
break;
case R.id.fishRBtn:
Log.v(TAG, "Chose Fish");
break;
case R.id.stkRBtn:
Log.v(TAG, "Chose Steak");
break;
default:
Log.v(TAG, "Huh?");
break;
}
}});
}
}
You have good example here:
Creating RadioButtons programmatically
or this example also good:
Programmatically-added RadioButtons refuse to obey LayoutParams weighting
Here you have it...
ln = (LinearLayout)findViewById(R.id.Linear_layout); // Assuming linearLayout is your parent layout
RadioGroup rg = new RadioGroup(context); // create the RadioGroup
rg.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
cg_Value = new String{“item 1”, “item 2”, “item 3”};
rb = new RadioButton[cg_Value.length];
rg.setOrientation(RadioGroup.HORIZONTAL);// horizontal or vertical depends on requirement
for (int l = 0; l < cg_Value.length; l++) {
rb[l] = new RadioButton(context);
rg.addView(rb[l]); // the RadioButtons are added to
// the radioGroup instead of the
// layout
rb[l].setTextColor(Color.BLACK);
rb[l].setText(cg_Value[l]);
rb[l].setTextSize(14);
}
//rb[1].setChecked(true);
ln.addView(rg);
Hope this helps
in my android app i am tried to create a dynamic radio button, according to an arraylist size, inside a radio group using the following code
for (int aa = 0; aa < itemname.length(); aa++)
{
String iname = itemname.getJSONObject(aa).getString("item");
final RadioButton[] radioButton = new RadioButton[itemname.length()];
radioButton[aa] = new RadioButton(this);
radioGroup.addView(radioButton[aa]);
radioButton[aa].setTextColor(R.color.black);
radioButton[aa].setText(iname);
radioButton[aa].setButtonDrawable(R.drawable.about_us_tab);
radioButton[aa].setId(h_count);
radioGroup.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
int selectedBtn = radioGroup.getCheckedRadioButtonId();
}
});
h_count++;
}
}
I am getting all the data in right order, and the onclick action of radio group is also working correctly.
But i want the the first radio button to be in selected mode. For this i tried using an if condition as follows
if(aa == 0)
{
radioButton[aa].setChecked(true);
radioButton[aa].setId(h_count);
}
But here the problem is, the first one is getting selected and in onclick action of radio group the first one is not getting deseleced, all others are working fine.
how to make the first one also deselected in onclick action of radio group
try in this way and its working
for (int aa = 0; aa < itemname.length(); aa++)
{
String iname = itemname.getJSONObject(aa).getString("item");
final RadioButton radioButton = new RadioButton(this);
radioGroup.addView(radioButton;
radioButton.setTextColor(R.color.black);
radioButton.setText(iname);
radioButton.setButtonDrawable(R.drawable.about_us_tab);
if(aa == 0)
{
radioButton.setChecked(true);
}
radioButton.setId(h_count);
radioGroup.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
int selectedBtn = radioGroup.getCheckedRadioButtonId();
}
});
h_count++;
}