Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of CSS class selectors for Android views?

Do Android views have something equivalent to CSS class selectors? Something like R.id but usable for multiple views? I would like to hide some group of views independent of their position in the layout tree.

like image 799
Adam Avatar asked Jul 19 '13 13:07

Adam


People also ask

What are the 3 different kinds of selectors in CSS?

Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)

Can I use CSS in Android Studio?

It's a libary that you can use css-like selectors in android layout.

How can I use CSS in Android?

First, create an Android Application Project in Eclipse. Then open the Layout XML file. Drag the first Linear Layout then WebView Element to the Relative Layout of the Activity. Then make a HTML and CSS file.


2 Answers

I think that you will need to iterate through all of the views in your layout, looking for the android:id you want. You can then use View setVisibility() to change the visibility. You could also use the View setTag() / getTag() instead of android:id to mark the views that you want to handle. E.g., the following code uses a general purpose method to traverse the layout:

// Get the top view in the layout.
final View root = getWindow().getDecorView().findViewById(android.R.id.content);

// Create a "view handler" that will hide a given view.
final ViewHandler setViewGone = new ViewHandler() {
    public void process(View v) {
        // Log.d("ViewHandler.process", v.getClass().toString());
        v.setVisibility(View.GONE);
    }
};

// Hide any view in the layout whose Id equals R.id.textView1.
findViewsById(root, R.id.textView1, setViewGone);


/**
 * Simple "view handler" interface that we can pass into a Java method.
 */
public interface ViewHandler {
    public void process(View v);
}

/**
 * Recursively descends the layout hierarchy starting at the specified view. The viewHandler's
 * process() method is invoked on any view that matches the specified Id.
 */
public static void findViewsById(View v, int id, ViewHandler viewHandler) {
    if (v.getId() == id) {
        viewHandler.process(v);
    }
    if (v instanceof ViewGroup) {
        final ViewGroup vg = (ViewGroup) v;
        for (int i = 0; i < vg.getChildCount(); i++) {
            findViewsById(vg.getChildAt(i), id, viewHandler);
        }
    }
}
like image 87
Yojimbo Avatar answered Sep 21 '22 05:09

Yojimbo


You can set same tag for all such views and then you can get all the views having that tag with a simple function like this:

private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
    ArrayList<View> views = new ArrayList<View>();
    final int childCount = root.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = root.getChildAt(i);
        if (child instanceof ViewGroup) {
            views.addAll(getViewsByTag((ViewGroup) child, tag));
        }

        final Object tagObj = child.getTag();
        if (tagObj != null && tagObj.equals(tag)) {
            views.add(child);
        }

    }
    return views;
}

As explained in Shlomi Schwartz answer. Obviously this is not as useful as css classes are. But this might be a little useful as compared to writing code to iterate your views again and again.

like image 31
Adil Malik Avatar answered Sep 20 '22 05:09

Adil Malik