Tuesday, 29 May 2012

How To Get current layout object on Android

In android using getLayoutInflater method in LayoutInflater class we can get current layout object.

Example Code:

 UI Layout (mainlayout.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/darker_gray"
    tools:context=".MainActivity" >
  <Button
                android:id="@+id/button"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                  />

  </RelativeLayout>

Here if we want to get current layout object use below code.
LayoutInflater inflater = getLayoutInflater();
RelativeLayout
root= (RelativeLayout) inflater.inflate(R.layout.mainlayout,null);

And we can access Button  using root object
Button button= (Button) root.findViewById(R.id.record);

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...