user4683997
user4683997

Reputation:

Change Listview of Navigation Drawer to any Custom View (Say Relative Layout)?

We have ListView to be displayed in our default NavigationDrawer by Android studio. Can we change that ListView to any other Relative Layout?

Can we write anything Custom?

Upvotes: 1

Views: 1103

Answers (1)

V Jayakar Edidiah
V Jayakar Edidiah

Reputation: 976

Yes You can Replace the ListView with any other customView like RelativeLayout Etc.. See my Example

in your activity_main.xml write a custom relativeLayout and give the layout Id in MainActivity's drawerLayout like this,

activity_main.xml

    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#0099cc"
            android:id="@+id/view" />

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/menu_imageView"
            android:src="@drawable/menu"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:layout_alignParentStart="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/zone_name"
            android:text="Kitchen"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_alignBottom="@+id/menu_imageView"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>



    <!-- The navigation drawer -->

    <RelativeLayout
        android:id="@+id/dockedLayout"
        android:layout_width="500dp"
        android:background="@drawable/background_dock"
        android:layout_height="match_parent"
        android:layout_gravity="start">
        <TextView
            android:id="@+id/master_volume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Master Volume"
            android:textColor="#ffffff"
            android:textSize="15dp"
            android:textStyle="bold"/>
        <SeekBar
            android:id="@+id/seekBarMasterVolume"
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:layout_below="@+id/master_volume"
            android:progressDrawable="@drawable/progress"
            android:thumb="@drawable/thumb"/>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    RelativeLayout mDockLayout;
    ImageView menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        menu = (ImageView) findViewById(R.id.menu_imageView);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDockLayout = (RelativeLayout) findViewById(R.id.dockedLayout);

        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(mDockLayout);
            }
        });
    }

Upvotes: 1

Related Questions