title: Programmatically build Navigation Drawer
body: <p>I have a DrawerLayout, which uses 2 Layouts.</p>
  
  <ol>
  <li>for the main content. (RelativeLayout)</li>
  <li>The drawer pane. (LinearLayout)</li>
  </ol>
  
  <p>I need to create everything DYNAMCALLY, so no XML has to be used.</p>
  
  <p>This is the code to create the drawerPane:</p>
  
  <pre><code>public class Test2 extends AppCompatActivity{
  
      ListView myList;
      DrawerLayout drawerLayout;
  
      LinearLayout drawerPane;
      RelativeLayout content;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          myList=new ListView(this);
  
          drawerLayout=new DrawerLayout(this);
  
          DrawerLayout.LayoutParams drawerLayout_params=new DrawerLayout.LayoutParams(280, DrawerLayout.LayoutParams.MATCH_PARENT);
  
          drawerLayout.setId(0);
  
          drawerPane=new LinearLayout(this);
          drawerPane.setGravity(Gravity.START);
          drawerPane.setClickable(true);
  
          myList=new ListView(this);
  
          myList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  
          LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(280,LinearLayout.LayoutParams.MATCH_PARENT);
          lp.gravity=Gravity.START;
  
          drawerPane.setLayoutParams(lp);
  
          drawerPane.addView(myList,new ListView.LayoutParams(280,ListView.LayoutParams.MATCH_PARENT));
  
          drawerPane.setGravity(Gravity.START);
          drawerPane.setOrientation(LinearLayout.VERTICAL);
  
          setContentView(drawerLayout,drawerLayout_params);
  
          content=new RelativeLayout(this);
  
          drawerLayout.addView(content,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
          drawerLayout.addView(drawerPane,lp);
  
          LinkedList&lt;String&gt; list=new LinkedList&lt;&gt;();
  
          list.add("Item #1");
          list.add("Item #2");
          list.add("Item #3");
  
          myList.setAdapter(new My_Adapter&lt;String&gt;(this,R.layout.drawer_item,list) {
              @Override
              protected void set(String item, View customView) {
  
                  TextView title=(TextView)customView.findViewById(R.id.title);
                  TextView subTitle=(TextView)customView.findViewById(R.id.subTitle);
  
                  title.setText(item);
                  subTitle.setText(item);
              }
          });
      }
  }
  </code></pre>
  
  <p>I use a My_Adapter custom class for the adapter of my ListView:</p>
  
  <pre><code>public abstract class My_Adapter&lt;T&gt; extends BaseAdapter {
  
      private int customLayoutID;
  
      private Context myContext;
  
      private Collection&lt;T&gt; listValues;
      private T[] arrayValues;
  
      private final boolean listMode;
      private final boolean arrayMode;
  
      public My_Adapter(Context context,int layoutID, T[] values) {
  
          arrayMode=true;
          listMode=false;
  
          myContext=context;
          arrayValues=values;
          customLayoutID=layoutID;
      }
  
      public My_Adapter(Context context,int layoutID, Collection&lt;T&gt; values) {
  
          listMode=true;
          arrayMode=false;
  
          myContext=context;
          listValues=values;
          customLayoutID=layoutID;
      }
  
      @Override
      public int getCount() {
  
          if (listMode){
  
              return listValues.size();
          }
          else if (arrayMode){
  
              return arrayValues.length;
          }
          else return -1;
      }
  
      @Override
      public T getItem(int position) {
  
          if (listMode){
  
              return ((List&lt;T&gt;) listValues).get(position);
          }
          else if (arrayMode){
  
              return arrayValues[position];
          }
          else return null;
      }
  
      @Override
      public long getItemId(int position) {
  
          return position;
      }
  
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          LayoutInflater inflater= LayoutInflater.from(myContext);
  
          View myView= inflater.inflate(customLayoutID,parent,false);
  
          T item=getItem(position);
  
          set(item,myView);
  
          return myView;
      }
  
      protected abstract void set(T item, View customView);
  }
  </code></pre>
  
  <p>With this code, this is what the app looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/rhA3G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rhA3G.png" alt="Application preview"></a></p>
  
  <p>This is the drawer_item.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="10dp"
      android:paddingBottom="10dp"&gt;
  
      &lt;TextView
          android:id="@+id/title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="18sp"
          android:gravity="center_vertical"
          android:textColor="@color/abc_primary_text_material_dark"
          android:text="Line 1"
          android:textStyle="bold" /&gt;
  
      &lt;TextView android:id="@+id/subTitle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Line 2"
          android:textColor="#717171"
          android:layout_below="@+id/title"
          android:layout_alignRight="@+id/title"
          android:layout_alignEnd="@+id/title" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>EDIT/UPDATE</strong></p>
  
  <p>I want to get the same result i get with the following xml code. 
  Look the "LAYOUT_GRAVITY" for the LinearLayout.</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawerLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;!-- The main content view --&gt;
      &lt;RelativeLayout
          android:id="@+id/mainContent"
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;!-- The navigation drawer --&gt;
      &lt;LinearLayout
          android:layout_width="280dp"
          android:layout_height="match_parent"
          android:id="@+id/drawerPane"
          android:layout_gravity="start"
          android:clickable="true"
          android:orientation="vertical"&gt;
  
          &lt;!-- Profile Box --&gt;
  
          &lt;RelativeLayout
              android:id="@+id/profileBox"
              android:layout_width="match_parent"
              android:layout_height="100dp"
              android:background="@color/material_blue_grey_900"
              android:padding="8dp" &gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="42dp"
                  android:layout_centerVertical="true"
                  android:layout_marginLeft="15dp"
                  android:orientation="vertical" &gt;
  
                  &lt;TextView
                      android:id="@+id/userName"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="ElektroFR"
                      android:textColor="#fff"
                      android:textSize="16sp"
                      android:textStyle="bold" /&gt;
  
                  &lt;TextView
                      android:id="@+id/desc"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/userName"
                      android:layout_marginTop="4dp"
                      android:text="View Profile"
                      android:textColor="#fff"
                      android:textSize="12sp" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/RelativeLayout&gt;
  
          &lt;!-- List of Actions (pages) --&gt;
          &lt;ListView
              android:id="@+id/navList"
              android:layout_width="280dp"
              android:layout_height="match_parent"
              android:layout_below="@+id/profileBox"
              android:choiceMode="singleChoice"
              android:background="@color/bright_foreground_disabled_material_light" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>