Friday, October 28, 2011

Inflate SlidingDrawer from XML

Inflate SlidingDrawer from XML

To inflate SlidingDrawer from XML, create our SlidingDrawer, /res/layout/horizontalslidingdrawer.xml.
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:handle="@+id/handle"
android:content="@+id/content">
<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/icon"/>
<LinearLayout
android:id="@id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#55000000">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" - Button - "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" - Button - "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" - Button - "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" - Button - "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" - Button - "/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" - Button - "/>
</LinearLayout>
</SlidingDrawer>


In Java:
package com.exercise.SlidingDrawer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;

public class AndroidSlidingDrawerActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout mainLayout = new LinearLayout(this);
LayoutParams mainLayoutParams
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
mainLayout.setLayoutParams(mainLayoutParams);
mainLayout.setOrientation(LinearLayout.VERTICAL);

LayoutInflater inflater
= (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
SlidingDrawer mySlidingDrawer
= (SlidingDrawer)inflater.inflate(
R.layout.horizontalslidingdrawer,
mainLayout, false);
mainLayout.addView(mySlidingDrawer);

setContentView(mainLayout);

}
}



Related:
- SlidingDrawer in vertical
- SlidingDrawer in horizontal

No comments:

Post a Comment