EltMrx EltMrx - 25 days ago 8
Android Question

I have multiple linear layouts, how can I change some or all of the backgrounds at one time, then change them again, and again etc?

In my activity I have about 40 linear layouts... Basically 5 parents and the rest children of said parents.

What I want to be able to do is to change the background of some, then click a button and change the background of others.

The issue I'm having... And unfortunately I don't have my exact code with me right now is that once I change a background, I have to set it to null.

The code that I have in my java class looks something like this...

mlayout1= findViewById(R.id.layout1);

mlayout2= findViewById(R.id.layout2);

mlayout3= findViewById(R.id.layout3);

mlayout4= findViewById(R.id.layout3);

if variable = 1{

mlayout1.setBackgroundResource(R.drawable.background_img);
mlayout2.setBackgroundResource(R.drawable.background_img);
mlayout3.setBackgroundResource(R.drawable.background_img);
mlayout4.setBackgroundResource(R.drawable.background_img);
}

if variable = 2{

mlayout1.setBackgroundResource(R.drawable.background_img);

mlayout2.setBackgroundResource(null);

mlayout3.setBackgroundResource(R.drawable.background_img);

mlayout4.setBackgroundResource(null);
}

if variable = 3{

mlayout1.setBackgroundResource(R.drawable.background_img);

mlayout2.setBackgroundResource(null);

mlayout3.setBackgroundResource(null);

mlayout4.setBackgroundResource(null);
}


and etc...

One problem is that there are about 30 layouts that I need to change the background to and about 500 different combinations that I'm working with...

The biggest problem that I'm having is having to re-set all the backgrounds to null before applying the new combination of backgrounds...

I have a table with all the different combinations in it.

I would like to be able to scroll through them by hitting a next and/or previous button or typing in a combination ID number and have the correct combination of backgrounds light up, then have them all reset before lighting up the next combination......

Maybe I need another activity to reset everything to null before I draw the new combination of backgrounds or something I'm not sure how best to do this efficiently.

I'm new to Android and my head hurts as I've been trying to figure this out for weeks now...

mrb mrb
Answer

The first thing you might want to do is put your layout views into an array or list or something, so you don't have to code in the ID name of every single one:

public void onCreate(Bundle b) {
    setContentView(R.layout.main);

    List<View> myViews = new ArrayList<View>();
    myViews.add(findViewById(R.id.layout1));
    myViews.add(findViewById(R.id.layout2));
    // etc.
}

Then you can access them directly and programmatically in the array. But that's still a lot of code.

If you have access to their parent you could iterate over its children when making your choice:

public void performSelection(int selection) {
    // One of your parents that holds a lot of child views
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1);

    for(int i = 0; i < parent.getChildCount(); i++) {
        View v = parent.getChildAt(i);

        // If selection is 1, this sets the drawable background on every item.
        // If 2, then sets drawable on 0, 2, 4, 6, ... and null on 1, 3, 5, ...
        // If 3, then sets drawable on 0, 3, 6, 9, ... and null on 1, 2, 4, 5, ...
        if (i % selection == 0) {
            v.setBackgroundResource(R.drawable.background_img);
        } else {
            v.setBackgroundResource(null);
        }
    }
}

Careful though, that will look at EVERY child for the requested parent. Make sure it only contains the views that you want to change the backgrounds of.

Another approach: If your layout views are all basically the same in XML but with different IDs, you might consider making a separate XML resource for one and then loading them dynamically at activity creation into your parents or an array. In this case, your main layout XML file would have the parent_1 element but with no child views, as they'd be added at runtime.

layout/one_element.xml:

<LinearLayout ...>
     <TextView android:id="@+id/element_text" ... />
     <!-- etc -->
</LinearLayout>

Then you can do something like this:

public void onCreate(Bundle b) {
    setContentView(R.layout.main);

    // One of your parents that will hold a lot of child views
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1);

    for (int i = 0; i < NUM_CHILDREN; i++) {
        View newView = getLayoutInflater().inflate(R.layout.one_element,
                                                   null);
        // Set anything specific to the new view
        // eg.
        TextView tv = (TextView)newView.findViewById(R.id.element_text);
        tv.setText(String.format("Hi, I'm element %d!", i));

        // Add it to the parent
        parent.addView(newView);
    }
}

Then you can use the performSelection method above to change the backgrounds as needed.