I'm attempting to create a few radio buttons and add them a RadioGroup dynamically. When I use the LayoutInflater method of pulling in the xml and adding it to the current view, everything works fine. The correct radio buttons show up.
However when I try to cast the View that LayoutInflater.inflate returned to a RadioButton (so I can setText), I get a force close with a java.lang.ClassCastException.
for (int i = 0; i < options.length(); i++) {
JSONObject option = options.getJSONObject(i);
View option_view = vi.inflate(R.layout.poll_option, radio_group, true);
option_view.setId(i);
RadioButton rb = (RadioButton) option_view.findViewById(i);
rb.setText(option.getString("response"));
}
poll_option.xml:
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:text="RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The problem is you're not getting the views you think you're getting. LayoutInflater.inflate() called with a supplied root view means the view returned to you is THAT root view (not the inflated view). The method in which you are calling it inflates a new RadioButton and attaches it to the Group, but the return value (option_view) is the group itself, not the individual item. Since you need to play with the view before attaching it to the group, I'd recommend code like this (which works):
//I added these for posterity, I'm sure however you get these references is fine
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RadioGroup radio_group = new RadioGroup(this);
//Get the button, rename it, then add it to the group.
for(int i = 0; i < options.length(); i++) {
JSONObject option = options.getJSONObject(i);
RadioButton option_view = (RadioButton)vi.inflate(R.layout.poll_option, null);
option_view.setText(option.getString("response"));
radio_group.addView(button);
}
Editorial Note:
Just my $0.02, for such a simple layout, running this inflation process over and over in a loop may be a bit too much (inflation is expensive). You could easily create the same RadioButton in code, and add it with your LayoutParams, like:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (int i = 0; i < options.length(); i++) {
RadioButton option_view = new RadioButton(this);
option_view.setText(option.getString("response"));
radio_group.addView(option_view, params);
}
This code I didn't test, but it should be pretty close :p
Hope that Helps!
You probably want to use findViewById and locate the radio button in the inflated view. Something like:
RadioButton rb = (RadioButton)option_view.findViewById(R.id.yourButtonId);
See http://developer.android.com/reference/android/view/View.html#findViewById(int)
you want to radiobutton.setId(INT)
and then later get it by findViewById() to get the button.
The setID(Int) should be used when you dynamically create the button. You can now access it later with findViewById.
Related
Suppose I have a Table layout and i have a button called 'add people' which dynamically adds a row with two EdiTexts in it each time I click it. now there is another button which is supposed to save the first editText values in database and Add(sum) all the values of second editText .in HTML is very straight forward I can simply add class attribute to both inputs and loop through each class name. but I have no idea how i am supposed to do it in android.
What about saving the references in two lists? Then you can simply iterate over the lists to either store the values in a db or to sum them.
original answer check
public void init(){
TableLayout tablelayout = (TableLayout) findViewById(R.id.displayLinear);
for (int i = 0; i <2; i++) {
//Dynamic Button
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}
thanks in advance.
I have multiple TableRow objects in an Android app, each of which contains exactly two EditTexts. I want to have the contents and quantity of editTexts saved and restored when I open/close the app and so I need to have a way to set the text of the EditTexts, but this is where the problem is.
Android Studio is saying "Cannot Resolve Symbol 'setText'":
//will loop through each of the TableRows in a tableRowHolder(no problem yet):
for (int i = 0; i < tableRowHolder.getChildCount() && tableRowHolder.getChildAt(i) instanceof android.widget.TableRow; ++i) {
//set tableRow to be the i-th child in tableRowHolder (no problem yet)
TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);
//where the problem is("setText" is red), I don't think Java recognises that "tableRow.getChildAt(1)" is an EditText, even though it always will be.
tableRow.getChildAt(1).setText();
//this however, is perfectly fine:
EditText et = new EditText(
et.setText("");
}
To recap, I have:
A tableRow object always containing exactly two EditTexts
and my problem is that:
Java seems to not recognise that I am asking for .setText() on a EditText
Thanks so much in advance.
Just like you're casting your TableRow out of the TableRowHolder, you need to cast the View child to an EditText before you can call its methods.
TableRow tableRow = (TableRow) tableRowHolder.getChildAt(i);
((EditText) tableRow.getChildAt(1)).setText("Some Text");
You could optionally wrap your calls inside an instanceof if-block to avoid any ClassCastExceptions if there's any chance that the View may not be an EditText always.
View child = tableRow.getChildAt(1);
if (child instanceof EditText) {
EditText et = (EditText) child;
et.setText("Some Text");
}
I am developing an app where the user has to match the image and corresponding name of it correctly.
My problem is when the user selects the image first and selects the wrong name it will display wrong answer and if he selects the answer it will be displayed correct answer.
The user should not have to re-select the image again
I have made the onClickListerner's null but it wont work some of my code is as follows,
txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
txt_tag[0].setOnClickListener(this);
txt_tag[0].setTypeface(tf);
txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
txt_tag[1].setOnClickListener(this);
txt_tag[1].setTypeface(tf);
txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
txt_tag[2].setOnClickListener(this);
txt_tag[2].setTypeface(tf);
txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
txt_tag[3].setOnClickListener(this);
txt_tag[3].setTypeface(tf);
img[0] = (ImageButton) findViewById(R.id.img1);
img[0].setOnClickListener(this);
img[1] = (ImageButton) findViewById(R.id.img2);
img[1].setOnClickListener(this);
img[2] = (ImageButton) findViewById(R.id.img3);
img[2].setOnClickListener(this);
img[3] = (ImageButton) findViewById(R.id.img4);
img[3].setOnClickListener(this);
btn_nxt = (Button) findViewById(R.id.btn_next);
btn_nxt.setOnClickListener(this);
and I have called an method inside that method where I have made all onClickListerner's null
txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);
Can anyone tell me where I am going wrong or any modifications I can do to it.
Thanks in advance
Try using
txt_tag[0].setClickable(false);
txt_tag[1].setClickable(false);
..
img[0].setClickable(false);
img[1].setClickable(false);
..
Your question is not that clear.. but if you want your image and text tag not clickable.. make them android:clickable="false" in xml or setClickable(false);
If I were you I would be checking that logic in a listener. So if the quiestion (if it's a quiz) is in the state "ANSWERED", don't react to event.
Your question is unclear, but I understand it as follows:
You have a bunch of ImageViews and a bunch of TextViews and a Mapping between them.
You want to be able to first select an ImageView, then a TextView. If they match, "correct answer" will be displayed somewhere, if not, "wrong answer" will be displayed
If you click on a TextView before an ImageView is selected, nothing happens
If you click on a Textview and another TextView is already selected, nothing happens
If that is correct, you can do this like this: You keep two variables
int selectedImage = -1;
int selectedText = -1;
In your OnClickListener you update their values like this:
if (source instanceof ImageViews) {
selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
selectedText = -1; // reset textSelection
} else {
if (selectedText < 0) {
selectedText = getArrayIndex(source);
}
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.
Instead, you could just iterate through the TextView array and call
setClickable(false);
on every element as soon as one is clicked. If a new image is selected, you will have to set them to clickable again.
EDIT: And I agree with Rob, you should not remove your Listeners to achieve this behaviour.
Here's my suggestion, if you want you code up specific behaviour you can use the onClickListener callback to achieve what you want.
In the listener, check the state of the image; if it is already selected and you want to ignore the event then you just exit from your callback.
I think setting the onClickListener to null is the wrong thing to do.
I am doing an pageindex, and the current page should be highlighted with a arrow image (imageview).
The index is a RelativeLayout with 25 textviews added to it:
for (int i = 0; i < 25; i++) {
TextView tv = new TextView(this);
tv.setText(Integer.toString(i+1));
tv.setGravity(Gravity.CENTER);
int id = 2000+i;
tv.setId(id);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
if(i==0)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
else
params.addRule(RelativeLayout.RIGHT_OF, prevViewId);
prevViewId = id;
rl.addView(tv,params);
}
And when the page changes I do something like this:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, 2000+i);
params.addRule(RelativeLayout.ALIGN_RIGHT, 2000+i);
params.addRule(RelativeLayout.ALIGN_LEFT, 2000+i);
arrowImageView.setLayoutParams(params);
rl.requestLayout();
rl.invalidate();
Everything looks correct, I can place the arrow at arbitrary page at "start up", but the arrow wont move when page is changed. I have debuged and verified that the code is run and everything looks correct, but still the arrow is stuck in the first position. If I force delete and add a new imageview instead of updating the LayoutParams the arrow will disappear totally.
I have the same problem.. I also want to move my views around at run-time inside a reference layout. So if anyone ca help that would be awesome.
What i believe is happening above is that the arrow IS having its location changed, however it doesn't get updated to the screen. Correct me if I'm wrong, please, this is just my guess as I am too having the same problem.
-edit-
After some messing around I've found what works for me is to remove first then add.
rl.removeView(tv);
rl.addView(tv,params);
-edit-
also, u can save the params for the moving view into a unique variable so that way all ude have to do is change the margins...
for example: instead of params, have it be its own name "arrowParams"
then to move it ude just need: arrowParams.leftMargin = 2000+i; and so on...
I started my own android app a few days ago since I needed a mobile application to store a bunch of data I collect in the hospital.
I'm pretty new to Java and android environment, although it seems easy to understand and very similar to C++.
Anyway, my application has a bunch of "EditText" and radio buttons and my question is:
How can I iterate through all those widgets (EditTexts and radio buttons)?
In .NET you could do a "for each element in container " loop but I can't seem to find a way to do this in Java/android environment.
Note: I don't know how many "widgets" exist in the activity, since some are created dinamicaly, others are hardcoded and some others show if some user preferences are set
Any help or hint would be appreciated.
for (int i = 0; i < rootView.getChildCount(); i++)
rootView.getChildAt(i)
Note that this will return View-s, you will have to check at runtime exactly what type of View you are currently looking at
It works.
Regarding the View type (ie Spinner, radioButton, EditText, etc) we can tag each type we want to parse in the Layout XML file and then add a conditional, ie:
if (Widget_Tag != null){
View Current_Widget = (View) rootView.getChildAt(i);
String Widget_Tag = (String) Current_Widget.getTag();
if (Widget_Tag.equals("MyEdiText")) {
//do something
}
}
the if (Widget_Tag != null){ is to prevent NullPointReferences. You can also doi it with a Try / Catch.
You can try this code:
LayoutInflater inflater = getLayoutInflater();
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.main, null);
ViewGroup Current_Widget = (ViewGroup)l.getRootView();
for (int i = 0; i < Current_Widget.getChildCount(); i++)
Current_Widget.getChildAt(i);