Saber Solooki
Saber Solooki

Reputation: 1220

how to set attribute on group of child view android

Assume you have linerlayout with bunch of child like text view and checkbox and ... . how can I set attribute like setAlpha or setClickable or ... on all of child in linerlayout. I can do this by find every child with findViewById and then set attribute but I have many child in linerlayout

Upvotes: 1

Views: 876

Answers (2)

Alex Salauyou
Alex Salauyou

Reputation: 14348

Iterate all children recursively, checking if a view is instance of ViewGroup (i. e. contains other children):

public void setAlpha(View view, float alpha){
    if (view instanceof ViewGroup){
        ViewGroup viewgroup = (ViewGroup)view;
        int count = viewgroup.getChildCount();
        for (int i=0; i < count; i++){
            View viewNext = viewgroup.getChildAt(i);
            setAlpha(viewNext, alpha);
        }
    }
    else {
        view.setAlpha(alpha);
    }
}

and apply it to your layout:

setAlpha(findViewById(R.id.your_layout), alpha);

Upvotes: 3

john
john

Reputation: 4158

This is what I did for my list of checkboxes. Can be done the same way for different controls

        //get the checkbox states
        for(int i = 0; i < 13; ++i)//I had 13 checkboxes
        {
            //dynamically get the bool state of the 'initial' checkboxes
            String initialCbID = "initial" + i;//my Id's looked like: initial1, initial2, initial3 etc
            //get the id of control
            int intialResID = getResources().getIdentifier(initialCbID, "id", context.getApplicationContext().getPackageName());
            //then use the id to initialize a reference
            CheckBox cbInitial = ((CheckBox)view.findViewById(intialResID));

        }

or you can do what @Aleks G said

Upvotes: 0

Related Questions