I have a code that generates an array of radio buttons and I want to know how to assign them in one group so the other radio buttons won't be enabled if another is enabled.
This here is my code for the generation of the said radio buttons.
for (int i = 0; i < 20; i++)
{
RadioButton myRadio = new RadioButton(this);//use array
myRadio.setId(i);
final int id_ = myRadio.getId();
myRadio.setText("button " + id_);
LinearLayout li = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.addView(myRadio, lp);
btn = (RadioButton) findViewById(id_);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Button clicked index = " + id_, Toast.LENGTH_SHORT).show();
}
});
}
btn is a button declared outside the function. It is also a radio button
Try this code sample
final RadioButton[] rb = new RadioButton[20];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);
for(int i=0; i<20; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(" " + ContactsActivity.phonetype.get(i)
+ " " + ContactsActivity.phone.get(i));
rb[i].setId(i + 100);
}
ll.addView(rg);//you add the whole RadioGroup to the layout
Update - Answer for the error you mentioned in your comments
To resolve your error, find out, using the line-number in the error, which line produces the error. Look at what you are adding there
Check all your addView calls. (hint: there is a line-number in that error somewhere. use it)
To try to answer your question in the comment, you must follow these rules;
Never add any view more then once.
When a View is already used (e.g., you got it with findViewById, don't use addView on it.
When you want to add a view, use addView with a NEW view.
You can add several of these new views to one view, but you cannot add that one view multiple times.
Related
What I am trying to do is programmatically creating a row of buttons with a constraint view.
I am creating two buttons and I want to have them next to each other without doing something to the .xml file, since the number of buttons can vary depending on the user.
I want to use something like (this code is part of an Activity):
ConstraintLayout layout = findViewById(R.id.layout);
Button btn1 = new Button(this);
Button btn2 = new Button(this);
ConstraintLayout.LayoutParams params1 = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
ConstraintLayout.LayoutParams params2 = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
layout.addView(btn1, 1, params1);
params2.topToTop = btn1.getId();
params2.leftToRight = btn1.getId();
layout.addView(btn2, 2, params2);
Setting the two params2 values does not work because apparently I cannot really access the ID of button1.
What would be a working solution to this?
Things I have read and tried:
Using tags instead of ids
Accessing the buttons using an ArrayList of all the created buttons as a private member for the Activity
Giving some random id (that I have chosen) to the Views using setId()
Using something like this works, because I have predefined that btn3 in the xml file:
params2.topToTop = layout.findViewById(R.id.btn3).getId();
params2.leftToRight = layout.findViewById(R.id.btn3).getId();
But in all the other cases my btn2 just lands on top of btn1 (or rather on the top left edge of the layout)
Thank you in advance!
You can use the following method to generate view id programmatically:
private static final AtomicInteger nextGeneratedId = new AtomicInteger(10000);
public static int generateViewId() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
for (;;) {
final int result = nextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 10000; // Roll over to 10000, not 0.
if (nextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
} else {
return View.generateViewId();
}
}
call .setId(generatedId) for the buttons you create.
I have this function which is supposed to create an array of TextViews with unique ids.
Each TextView is supposed to perform a unique action, however when any one of them is clicked, they perform the function of the last TextView .
(ie, anyone of them appends a 9 to the last TextView the way this i set up) Do you know why it does this, and how I can fix it?
Any help would be greatly appreciated.
//Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_what_can_imake);
int textViewCount = 10;
TextView[] textViewArray = new TextView[textViewCount];
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);
for(int i = 0; i < textViewCount; i++) {
textViewArray[i] = new TextView(this);
textViewArray[i].setText("Title"+Integer.toString(i));
textViewArray[i].setPadding(8,8+50*i,8,0);
textViewArray[i].setId(i);
LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
textViewArray[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int myId = v.getId();
((TextView) v).append(Integer.toString(myId));
}
});
myLayout.addView(textViewArray[i],myTitleDimensions);
}
}
You are using different paddingTop to layout your TextViews vertically:
textViewArray[i].setPadding(8,8+50*i,8,0);
this makes the TextViews visually separate to each other, but in fact they are all overlapped, the 2nd one overlapped the 1st, the 3rd one overlapped the 2nd, etc. At last, the 9th one overlapped all, so no matter which text you clicked, you actually clicked the 9th one.
To fix this, you should change the way you layout the TextViews.
For example, use RelativeLayout.addRule(int verb, int anchor):
RelativeLayout.LayoutParams myTitleDimensions = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
myTitleDimensions.addRule(RelativeLayout.BELOW, i - 1);
}
By the way, 0 is not a valid id, so your 1st TextView will be still overlapped by the 2nd one, just change the way to generate ids a little.
The string answers_log is the result of a database query. So, when i loop trough the results, it generates 4 radio groups. When it try to set all radio groups enable, the radioGroup.getChildAt(j).setEnabled(false); is applied only to the last radio group. What can i do, to set all radio groups enable?
Here is my code:
radioGroup = new RadioGroup(this);
for (Answer an : answers) {
String answers_log = " " + an.getAnswer();
answer = new RadioButton(this);
answer.setText(answers_log);
radioGroup.addView(answer+log);
}
linearLayout.addView(radioGroup);
finishButton = new Button(this);
linearLayout.addView(finishButton);
finishButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < 4; i++) {
radioGroup.getChildAt(i).setEnabled(false);
}
}
});
Thanks!
Using a RadioGroup do not allow you to select multiple RadioButtons at same time. You should add 4 radio buttons without RadioGroup.
I have a textView set up on my main activity, and a button. When I click the button, I'd like the textView to start updating it's value based on the code below. However, this doesn't work and the problem is the loop. Can someone explain why? I am new to Java and Android Development
button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new OnClickListener() {
TextView textView = (TextView)findViewById(R.id.refView);
public void onClick(View arg0) {
for(i=1;i<1;i++){
i = i + 1;
textView.setText(String.valueOf(i)+"hello");
}
}
});
Thank You
Try this:
TextView textView = (TextView)findViewById(R.id.refView);
button2.setOnClickListener(new View.OnClickListener() {
int i = 0;
public void onClick(View arg0) {
i = i + 1;
textView.setText(String.valueOf(i)+"hello");
}
});
Your for loop conditions were wrong.
for(i=1;i<1;i++) won't even start, because 1<1 is already met.
Initiate count variable i before onClick and then update it before click and set new text with updated i.
Not sure what exactly you want to happen. But, you can get rid of this line
i = i + 1;
because the i++ already increments i by 1 with each iteration of the for loop.
Second, since i starts off at 1 and you want the loop to run while i<1, it will never enter the loop. It is never less than 1.
Third, if the conditions were different, say
for (int i=0; i<10; i++)
it will run through the loop so fast that you won't even recognize a change.
i'm having listview with some items. i placed a Checkbox to display the checkbox with that listitem. I've write onclick method to checkall the checkbox. But, it'll select the item which was in top of the list. What's the problem? Anyone clear me? Thanks in Advance. Sample Code : -
CheckBox c = (CheckBox)findViewById(R.id.checkBox1);
if (c.isChecked())
{
c.setChecked(false);
}
else
{
c.setChecked(true);
}
private OnClickListener checkAllCheckboxes = new OnClickListener(){
public void onClick(View v) {
ListView lv = getListView();
int size = getListAdapter().getCount();
boolean check = lv.isItemChecked(0);
for(int i = 0; i <= size; i++)
lv.setItemChecked(i, !check);
}
}
OR
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(true);
android-unable-to-check-all-the-checkboxes-in-a-custom-listview-because-of-recy
Example2
with that snippet its difficult to point out the issue.
one, the listview reuses the view, maybe that while scrolling the same view is appearing else where.
here is a tutorial http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/
check it out. or post additional code. particularly, getView in the adapter code.
It happened because at calling of Adapter view it execute at the time of your list view.
So after the you clicked on check box (true) at same number of below screen will be true. for that you have to maintain the Vector.
if(tempVector.get(position)){
holder.box.setChecked(true);
}
else{
holder.box.setChecked(false);
}
try this after your clicking event.