find Layout Id By Name - Android android.content

Android examples for android.content:Context

Description

find Layout Id By Name

Demo Code

import android.content.Context;

public class Main{

    private static final int DEFAULT_LAYOUT_ID = -1;

    public static int findLayoutIdByName(Context context,
            final String layoutName) {
        int resourceId = DEFAULT_LAYOUT_ID;
        try {//from   w w  w  . jav a  2  s .c  o  m
            resourceId = context.getResources().getIdentifier(layoutName,
                    "style", context.getPackageName());
        } catch (final Exception e) {
            e.printStackTrace();
            resourceId = DEFAULT_LAYOUT_ID;
        }
        return resourceId;
    }

    public static String getPackageName(Context context) {
        return context.getPackageName();
    }

}

Related Tutorials