Radio buttons are supposed to be mutually exclusive. But they need to be in a "radio group."
According to the docs, "By grouping them together, the system ensures that only one radio button can be selected at a time."
I made some radio buttons (dynamically loaded) and I made sure to put them in a group. However, they are not mutually exclusive and I can check all of them at once.
How do I fix this?
Here is the code:
public class MainActivity extends AppCompatActivity {
private LinearLayout candidatesLayout;
private String[] candidates = {"Smith","Johnson","Williams","Brown","Jones","Garcia","Miller","Davis","Rodriguez","Martinez"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
candidatesLayout = (LinearLayout) findViewById(R.id.candidatesLayout);
final int N = candidates.length; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this); //rowTextView is persons name
//create horizonal parent for radio buttons
LinearLayout radioButtons = new LinearLayout(this);
LinearLayout numbers = new LinearLayout(this);
radioButtons.setOrientation(LinearLayout.HORIZONTAL);
//create new radio buttons
final RadioGroup radioGroup = new RadioGroup(this);
RadioButton button;
for(int ii = 0; ii < N; ii++) {
button = new RadioButton(this); //I should not be able to click more than one of these!
radioButtons.addView(button);
}
candidatesLayout.addView(rowTextView);
candidatesLayout.addView(radioButtons);
}
}
}
I am guessing that they are not really in the group that I think they are in.
You have created RadioGroup, but didn't add it to the layout.
So, replace radio buttons adding with the following snippet:
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this); //rowTextView is persons name
//create horizonal parent for radio buttons
LinearLayout radioButtons = new LinearLayout(this);
LinearLayout numbers = new LinearLayout(this);
radioButtons.setOrientation(LinearLayout.HORIZONTAL);
//create new radio buttons
final RadioGroup radioGroup = new RadioGroup(this);
RadioButton button;
for(int ii = 0; ii < N; ii++) {
button = new RadioButton(this);
radioGroup.addView(button); // <-- Add to Group
}
candidatesLayout.addView(rowTextView);
candidatesLayout.addView(radioGroup); // Add Group itself
}
Related
I am trying to add items to array dynamically and then add these items in array of textviews but the layout doesn't show me any item. I need to know what is wrong with my code. Thanks in advance.
TextView[] tt;
LinearLayout testy;
List<String> x = new ArrayList<String>(99);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
testy=(LinearLayout) findViewById(R.id.testy);
tt=new TextView[100];
LinearLayout.LayoutParams dim=new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
x.add("hii");
for(int i=1;i<x.size();i++)
{
tt[i]=new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i,"hello");
String data=x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
setContentView(testy);
}
You have only 1 item in your array list (which has an index of 0) but your loop starts at 1 which skips the whole for loop because 1<1 is false
R.layout.activity_test is a layout file that controls are how to display how to display the display interface of setContentView is to set a Activity, this sentence is the sentence and then set the Activity using main layout file under R.layout layout
remove this
setContentView(testy);
Edit
Try this .
TextView[] tt;
LinearLayout testy;
List<String> x = new ArrayList<String>(100);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
testy = (LinearLayout) findViewById(R.id.testy);
tt = new TextView[100];
LinearLayout.LayoutParams dim = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < x.size(); i++) {
tt[i] = new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i, "hello");
String data = x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
}
Replace
for(int i=1;i<x.size();i++)
{
tt[i]=new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i,"hello");
String data=x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
By
for(int i=0;i<tt.length;i++)
{
tt[i]=new TextView(this);
tt[i].setLayoutParams(dim);
x.add(i,"hello");
String data=x.get(i);
tt[i].setText(data);
testy.addView(tt[i]);
}
And
List<String> x = new ArrayList<String>(99);
By
List<String> x = new ArrayList<String>(100);
My question is:
How I can check if the checkboxes are checked and how can I get its id or text in a onclick event?
I have done it whit RadioGroup, but RadioGroup only let me check 1 RadioButton, and in this case I need to check more than one, but if Im wrong and there is a way to do it with RadioGroup it should work for me.
Thanks for the help
Here is the code where I create the checkboxes programmatically:
protected void onCreate(Bundle savedInstanceState) {
LinearLayout lg = (LinearLayout) findViewById(R.id.lg);
Button botonenv = (Button) findViewById(R.id.botonenv);
try{
myJson = new JSONObject(data);
JSONArray Solutions = myJson.getJSONArray("solutions");
for (int i = 0; i < Solutions.length(); i++) {
JSONObject JSonSol = Solutions.getJSONObject(i);
final String idSt = JSonSol.getString("id");
final String name = JSonSol.getString("name");
String resp = name.toUpperCase();
CheckBox cb1 = new CheckBox(this);
cb1.setText(resp);
LinearLayout.LayoutParams llg = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llg.setMargins(0, 30, 0, 0);
cb1.setTextSize(15);
cb1.setTextColor(Color.parseColor("#FF000000"));
lg.addView(cb1);
int id =Integer.parseInt(idSt);
cb1.setId(id);
}
}catch(JSONException e){
e.printStackTrace();
}
botonenv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//HERE I MUST NOW WHAT CHECKBOXES ARE CHECKED AND THEIR IDS
..............
}
});
}
Add their references to list , then you can reuse those reference again in any where outside after creation .
like
List<CheckBox> checkBoxes = new ArrayList<>();
add checkbox to your list wherever your create a new one checkBoxes.add(cb1);
inside your onClick you can check all of them .
I am dynamically creating the buttons. But the problem is they are getting created from top. I want to create it from bottom .
public class MainActivity extends Activity implements View.OnClickListener {
int i, j, butNum, lay1num = 1, lay2num = 100, lay3num = 100, store;
Button[] Button;
EditText numBut;
LinearLayout mainLayout;
LinearLayout[] subLayout;
LinearLayout.LayoutParams params;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
butNum = 5;
Button = new Button[butNum];
subLayout = new LinearLayout[3];
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.HORIZONTAL);
mainLayout.setWeightSum(90);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layoutGen();
for (j = 0; j < butNum; j++) {
int value = j + 1;
Button[j] = new Button(this);
Button[j].setText("" + value);
Button[j].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Button[j].setTextSize(20);
Button[j].setTag(value);
Button[j].setId(j);
subLayout[0].addView(Button[j]);
Button[j].setOnClickListener(this);
}
mainLayout.addView(subLayout[0], params);
setContentView(mainLayout);
}
Is there anyway I can create buttons which are aligned to the bottom.
subLayout.setGravity(Gravity.BOTTOM);
You nay need to set size of layout to match_parent.
Eventually You may set a layout_gravity like here
I'm creating a version of the popular Minesweeper game for Android. I'm trying to programmatically create a button and add it to a RelativeLayout. I've found something very similar here: How do I programmatically add buttons into layout one by one in several lines?
When I try to run it I get a NullPointerException at:
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
Here's the whole block of code:
public void create() {
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
for(int i = 0; i < gridSize; i++) {
if(grid[i] == 0) { //if grid pos. indicates an empty cell
Button empty = new Button(this);
empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
empty.setId(i); //set id to value of i
empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout1.addView(empty); //add the button to the relativeLayout view
//((Button) findViewById(i)).setOnClickListener(emptyListener);
}
Thanks in advance for any responses
have set the layout xml of the Activity by setContentView(R.layout.xxxx)?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
...
this
RelativeLayout layout1 = (RelativeLayout) findViewById(R.layout.game);
should be
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.relative_id);
R.id... used for mapping control and RelativeLayout is a control.
I think you're getting a blank screen because you haven't set the content view.
What i mean is that the codes does what it's supposed to do however, you should remove the "setContentView()" method at the top and place it at the end, then you should set it to the RelativeLayout before you close the onCreate() method! Something like this:
public void create() {
RelativeLayout layout1 = new RelativeLayout(this);
for(int i = 0; i < gridSize; i++) {
if(grid[i] == 0) { //if grid pos. indicates an empty cell
Button empty = new Button(this);
empty.setBackgroundResource(R.drawable.emptybutton); //set background to empty
empty.setId(i); //set id to value of i
empty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout1.addView(empty); //add the button to the relativeLayout view
//((Button) findViewById(i)).setOnClickListener(emptyListener);
}
}
setContentView(layout1);
}
Also notice that I've changed the declaration of the Relativelayout a little bit.
I hope this helps. :) !
You must enter the ID of the RelativeLayout, not the xml fil name.
try with
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.yourRelativeLayoutViewID);
Can anyone please guide me on how to setOnClickListener for the TextViews that are generated dynamically.
I have few buttons in my app.Now when a particular button is clicked than some specific text elements are retrieved from the database based on the the number of elements returned corresponding TextViews are generated in order to display the retrieved text elements.
Now the problem that i'm having is that i don't know how to set onClickListner for the dynamically generated TextViews.Following is the piece of code where i'm creating my TextViews.Please guide me on what should i do.thank you
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
catagory = "General";
IndependentDB genData = IndependentDB.getInstance();
genData.open(this);
ArrayList<TextHolder> genList = new ArrayList<TextHolder>();
genList = genData.getAllTextFromGenT();
genData.close();
x = genList.size();
String xstr = new StringBuilder().append(x).toString();
System.out.println(xstr);
mainText = new TextView[x];
textLayout = new LinearLayout[x];
llseperator = new LinearLayout[x];
textLayoutContainer
.setBackgroundResource(R.color.dark_navy_blue);
if (!genList.isEmpty()) {
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
mainText[i] = new TextView(this);
mainText[i].setId(i);
mainText[i].setText("Text");
mainText[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mainText[i].setTextSize(25);
mainText[i].setGravity(Gravity.CENTER);
mainText[i].setText(text);
mainText[i].setTextColor(0xFFFFFFFF);
mainText[i].setClickable(true);
mainText[i].setOnClickListener(this);
llseperator[i] = new LinearLayout(this);
llseperator[i].setId(i);
llseperator[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
llseperator[i].setGravity(Gravity.CENTER);
llseperator[i]
.setBackgroundResource(R.drawable.tv_seperator);
textLayout[i] = new LinearLayout(this);
textLayout[i].setId(i);
textLayout[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout[i].setGravity(Gravity.CENTER);
textLayout[i].addView(mainText[i]);
textLayoutContainer.addView(textLayout[i]);
textLayoutContainer.addView(llseperator[i]);
}// End of for
}// End of If
break;}
You already have onClick() method in Activity class (implemented View.setOnClickListener) so I suggest you to create a new class which implements View.setOnClickListener interface to handle click of TextView.
Try,
class TextClick implements View.setOnClickListener {
#Override
public void onClick(View view) {
TextView tx=(TextView) view;
...
}
}
and set click listener,
TextClick click=new TextClick();
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
mainText[i] = new TextView(this);
mainText[i].setOnClickListener(click);
...
}
It's the same setting on click listeners regardless of if the view was dynamically generated.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dosomething();
}
});
For example. you have a List tvs to store your TextView Objects created by the db result. What you should do is like this:
for(int i =0;i<tvs.count;i++){
tvs.get(i).setOnClickListener(new OnClickListener(){
...
})
}