Skip to content Skip to sidebar Skip to footer

Is There A Way Retrieve Piece Of Information Of A Control (linearlayout) Created In Dynamic Way?

I'm working on an android project which allow the user to fill a form. The picture below descripts my differents layouts for the form. The black layout (LinearMain) is my principa

Solution 1:

Because you don't know exactly with view you are to deal with it is important here to use instance of to avoid exceptions

ViewGroup is an abstract class that extends all ViewGroups, LinearLayout for instance is a ViewGroup.

if(ViewGroup.getChildAt(int) instanceof Checkbox){

//do sommethng here
}elseif(ViewGroup.getChildAt(int) instanceof Button){

//do sommethng here
}elseif(ViewGroup.getChildAt(int) instanceof TextView){

//do sommethng here
}elseif(ViewGroup.getChildAt(int) instanceof EditText){

//do sommethng here
}

Solution 2:

ViewGroup.getChildAt(int) is documented here

you can create a for loop to go through LinearLayout's children, check if corresponding children is instanceof CheckBox and get the value ((CheckBox) getChildAt(i)).isChecked()

Sample code:

LinearLayout ll;

for ( inti=0; i < ll.getChildCount(); i++ ) {
    Viewchild= ll.getChildAt(i);
    if ( child instanceof CheckBox ) {
        booleanchecked= ((CheckBox) child).isChecked();
    }
}

Solution 3:

You could set id to your View, never mind what view it is by: LinearButton.setId(); and then get the text by using findViewById(R.id.chosenId).getText().toString(); for EditText or findViewById(R.id.chosenId).isChecked(); for CheckBox.

another way is to save your view in a arrayList of views and to access them using this array.

Solution 4:

Have u created the layout in xml and set the id of each of the view ? If yes then all u need to just create the object of the EditText using findViewById(R.id.edittext) and then use getText() method of the EditText to get the text from the EditText. And for CheckBox you can use findViewById(R.id.chosenId).isChecked().

Post a Comment for "Is There A Way Retrieve Piece Of Information Of A Control (linearlayout) Created In Dynamic Way?"