In Android, how can I get the values I set in my XML file ?
my_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="500dp"
    android:orientation="vertical" >
    .....
<RelativeLayout/>
I've tried the following with no success:
View v = ((LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
            R.layout.my_layout, null);
//this gave me -1(LayoutParams.FILL_PARENT), which is not what I want. I need 300
v.getLayoutParams().width
// same for height, it gives me -1
 v.getLayoutParams().width
I do not care about how the View would actually look, I just need the values...
I know I can get the width and height of a view after the measuring is done (onGlobalLayout), but that's NOT what I need. What I need is the values in my XML.
EDIT1: I know v.getWidth() and v.getHeight() works AFTER the View is displayed on the screen, they will not work before measuring happens
If what you want to do is parse your xml layout in order to get some attributes the try some like this:
 Resources r = getResources();
    XmlResourceParser parser = r.getLayout(R.layout.your_layout);
    int state = 0;
    do {
        try {
            state = parser.next();
        } catch (XmlPullParserException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }       
        if (state == XmlPullParser.START_TAG) {
            //here get the attributes you want..., with AttributeSet for example
            }
        }
    } while(state != XmlPullParser.END_DOCUMENT);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With