Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create DrawerLayout programmatically

I would like to translate this xml to java in my main activity, the output is navigation drawer. I need help only in this part, I finished the remaining parts.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<FrameLayout
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView
    android:id="@+id/drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFF"
    android:choiceMode="singleChoice"/>

I would like to write above xml in java code, How ?

Thanks in advance

Update

final DrawerLayout drawer = new android.support.v4.widget.DrawerLayout(this);
     final FrameLayout fl = new FrameLayout(this);
     fl.setId(CONTENT_VIEW_ID);
     final ListView navList = new ListView (this);

     drawer.addView(fl, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
     drawer.addView(navList);

     setContentView(drawer);

I need little help to customise listview's layout width and layout gravity in code.

like image 364
user289175 Avatar asked Feb 10 '14 22:02

user289175


People also ask

How to create navigation Drawer activity?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

How to use DrawerLayout in Android?

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

How do I open navigation drawer programmatically?

btnMenu. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub drawer. openDrawer(Gravity. LEFT); } });


1 Answers

(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

I solved, here is the complete code

final DrawerLayout drawer = new android.support.v4.widget.DrawerLayout(this);
     final FrameLayout fl = new FrameLayout(this);
     fl.setId(CONTENT_VIEW_ID);
     final ListView navList = new ListView (this);

     DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(
            100 , LinearLayout.LayoutParams.MATCH_PARENT);

     lp.gravity=Gravity.START;


     navList.setLayoutParams(lp); 

     drawer.addView(fl, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
     drawer.addView(navList);

     setContentView(drawer);
like image 92
2 revs Avatar answered Oct 24 '22 10:10

2 revs