clear all child of a view (RadioGroup) [duplicate] - android

This question already has answers here:
Uncheck all RadioButton in a RadioButtonGroup
(3 answers)
Closed 7 years ago.
I have a drawer menu with a dynamic option inside it and I got to fill it.
I want to clear all the child of a RadioGroup inside adapter but I didn't find anything.
p.s: I fill all RadioGroup with addView and fill it with CheckBox
ps2: The question is not about the check and uncheck. I want to find out how can remove all child of a view (like RadioGroup).
viewHolder.mRadioGroup.setVisibility(View.VISIBLE);
/////clear viewHolder.mRadioGroup child hear, befor add new child to it
for (int i; i<10; i++) {
RadioButton radioButton = new RadioButton(mContext);
radioButton.setText(selectableValue.getValue());
viewHolder.mRadioGroup.addView(radioButton);
}

Try This :
int count = radioGroup.getChildCount();
if(count>0) {
for (int i=count-1;i>=0;i--) {
View o = radioGroup.getChildAt(i);
if (o instanceof RadioButton) {
radioGroup.removeViewAt(i);
}
}
}

This taken from link here for complex nested layout
private static void disable(ViewGroup layout) {
layout.setEnabled(false);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
disable((ViewGroup) child);
} else {
child.setEnabled(false);
}
}
}

Related

Android Studio GridLayout toggle all button

I'm using a Gridlayout with the MyView.java class. I got the code from GridLayout.
I implemented another button under the GridLayout. I want to toggle all (GrigLayout-)Buttons with this button. Does anyone have an Idea how I could do this?
Thank you!!!
You have to revise through all child views, and check if they are toggle buttons. Here is an example, when you want to toggle, call toggleButtons(grid);
private void toggleButtons(ViewGroup v) {
View a;
for(int i = 0; i < v.getChildCount(); i++)
{
a = v.getChildAt(i);
if(a instanceof ViewGroup) {
toggleButtons((ViewGroup) a);
}
else if(a instanceof Button){
// Toggle here
}
}
}

How to handle this type of events [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to make a button like this. In which you can add more forms.
These type of form will generate.
how to create and handle this type of event in android. This image is taken from ios but i want to implement it in android.
Thanks in advance. Sorry for bad English.
Follow this steps:
1) take hidden Layout.
2) Make one layout that include your Button , Editext.. etc.
3) When Click on Plus Button inflate layout & add it to hidden layout.
use this code :
LayoutInflater inflater = LayoutInflater.from(context);
View mView = (View) inflater.inflate(R.layout.custom_layout, null, false);
// Access button & Edittext like:
Button mButton= (Button)mView.findViewById(R.id.mButton);
EditText mEdiText= (EditText)mView.findViewById(R.id.mEditText);
LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
hiddenLinear.addView(mView);
EDIT
for adding multiple EditText use FOR Loop
LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
for (int i = 0, l <= 3; i++)
{
LayoutInflater inflater = LayoutInflater.from(context);
View mView = (View) inflater.inflate(R.layout.custom_layout, null, false);
// Access button & Edittext like:
Button mButton= (Button)mView.findViewById(R.id.mButton);
EditText mEdiText= (EditText)mView.findViewById(R.id.mEditText);
hiddenLinear.addView(mView);
}
& for getting all EditText Value from All View use this Method:
LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
findAllEdittexts(hiddenLinear);
ArrayList<EditText> array = new ArrayList<>();
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof EditText) {
EditText edittext = (EditText) view;
array.add(edittext);
}
}
}

Clearing multiple EditText fields with Clear Button

I have looked at other questions regarding this problem but the solutions don't work for me. I have a number of edittext fields on a page and want to be able to reset them with one button press. I implemented the solution from this thread but I have nested linear layouts and it only clears the edittext that is a direct child of the main container. How can I get it to find all of the edittext fields or will I have to reference each one?
My xml file structure is as follows:
<ScrollView>
<LinearLayout android:id="#+id/MainParent">
<LinearLayout>
TextView
EditText
</LinearLayout>
<LinearLayout>
TextView
EditText
</LinearLayout>
<LinearLayout>
TextView
EditText
</LinearLayout>
EditText
<LinearLayout>
Button1
Button2
</LinearLayout>
</LinearLayout>
</ScrollView>
Snippet from Java File:
ViewGroup group = (ViewGroup)findViewById(R.id.MainParent);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
If you are not interested in referencing each EditText specifically, I'd make a recursive function to handle the hierarchy.
public void clearAll(ViewGroup root) {
for (int i = 0, j = root.getChildCount(); i < j, i++) {
View view = root.getChildAt(i);
if (view instanceof ViewGroup) {
clearAll((ViewGroup) view);
continue;
}
if (view instanceof EditText) {
((EditText) view).setText("");
continue;
}
}
}
Then you just call:
clearAll((ViewGroup) findViewById(R.id.MainParent));
After onClick of any action do below step :
EditText firstEditText = (EditText) findViewById(R.id.yoursXmlId);
firstEditText.setText("");
LinearLayout group = (LinearLayout)findViewById(R.id.MainParent);
for (int i = 0, count = group.getChildCount(); i < count; ++i)
{
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}

Method to get all EditTexts in a View

can anyone help me with coding a method to get all EditTexts in a view? I would like to implement the solution htafoya posted here:
How to hide soft keyboard on android after clicking outside EditText?
Unfortunately the getFields() method is missing and htafoya did not answer our request to share his getFields() method.
EDIT
MByD pointed me to an error, thus making my answer almost identical to that of blackbelt. I have edited mine to the correct approach.
You could do a for-each loop and then check if each view is of the type EditText:
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
You could also, instead of having a list of EditTexts, have a list of ID's and then just add the id of the child to the list: myIdList.add( child.getId() );
To access your layout you need to get a reference for it. This means you need to provide an ID for your layout in your XML:
<LinearLayout android:id="#+id/myLinearLayout" >
//Here is where your EditTexts would be declared
</LinearLayout>
Then when you inflate the layout in your activity you just make sure to save a reference to it:
LinearLayout myLinearLayout;
public void onCreate( Bundle savedInstanceState ) {
super( savedInstanceState );
setContentView( R.layout.myLayoutWithEditTexts );
...
myLinearLayout = (LinearLayout) findViewById( R.id.myLinearLayout );
}
You then have a reference to your the holder of your EditTexts within the activity.
Here's a method I wrote to recursively check all EditText children of a ViewGroup, handy for a long sign-up form I had to do and probably more maintainable.
private EditText traverseEditTexts(ViewGroup v)
{
EditText invalid = null;
for (int i = 0; i < v.getChildCount(); i++)
{
Object child = v.getChildAt(i);
if (child instanceof EditText)
{
EditText e = (EditText)child;
if(e.getText().length() == 0) // Whatever logic here to determine if valid.
{
return e; // Stops at first invalid one. But you could add this to a list.
}
}
else if(child instanceof ViewGroup)
{
invalid = traverseEditTexts((ViewGroup)child); // Recursive call.
if(invalid != null)
{
break;
}
}
}
return invalid;
}
private boolean validateFields()
{
EditText emptyText = traverseEditTexts(mainLayout);
if(emptyText != null)
{
Toast.makeText(this, "This field cannot be empty.", Toast.LENGTH_SHORT).show();
emptyText.requestFocus(); // Scrolls view to this field.
}
return emptyText == null;
}
You can do it by calling View#getFocusables, which will return an arraylist of all focusable views in a View.
Then you can either check if they are EditTexts, with (instanceof) or act on all of them.
This Methods walks recursively through all ViewGroups and collects their TextViews. I use this to assign a new Color to all TextViews (even those embedded in predefined Widgets like Switch etc that make use of TextViews)
private HashSet<TextView> getTextViews(ViewGroup root){
HashSet<TextView> views=new HashSet<>();
for(int i=0;i<root.getChildCount();i++){
View v=root.getChildAt(i);
if(v instanceof TextView){
views.add((TextView)v);
}else if(v instanceof ViewGroup){
views.addAll(getTextViews((ViewGroup)v));
}
}
return views;
}
Get all Edit Text in any type of layout.
public List<EditText> getAllEditTexts(ViewGroup layout){
List<EditText> views = new ArrayList<>();
for(int i =0; i< layout.getChildCount(); i++){
View v =layout.getChildAt(i);
if(v instanceof EditText){
views.add((EditText)v);
}
}
return views;
}

Getting information from RadioButtons and Checkboxes

I have report which consists of 5 questions (there can be only one answer or multiple choice answer). Question are different for each report. So everytime I generate questions and answers as RadioButtons (one answer) or CheckBoxes (multiple choice answer)...But now I really don't know how to save those answers (I'd like to save to it as _question_id, _answer_id).
How can I assign good _answer_id, to _question_id...
Thanks for help in advance
Try something like this:
public void saveAnswers() {
LinearLayout root = (LinearLayout) findViewById(R.id.frame); //or whatever your root control is
loopQuestions(root);
}
private void loopQuestions(ViewGroup parent) {
for(int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if(child instanceof RadioGroup ) {
//Support for RadioGroups
RadioGroup radio = (RadioGroup)child;
storeAnswer(radio.getId(), radio.getCheckedRadioButtonId());
}
else if(child instanceof CheckBox) {
//Support for checkboxes
CheckBox cb = (CheckBox)child;
int answer = cb.isChecked() ? 1 : 0;
storeAnswer(cb.getId(), answer);
}
else {
//Support for other controls
}
if(child instanceof ViewGroup) {
//Nested Q&A
ViewGroup group = (ViewGroup)child;
loopQuestions(group);
}
}
}
private void storeAnswer(int question, int answer) {
//TODO: Store your answer to disk
}
I suppose you are using RadioGroup for a group of mutually exclusive RadioButtons? Then:
radioGroup.getCheckedRadioButtonId() // gets Id of clicked RadioButton within group
for CheckBox you must check for every button:
checkBox.isChecked()

Categories

Resources