I'm trying to do a sliding menu for my app which is activated when pushing a button but I don't have so big idea about how to achieve that. At the moment, I'm following this tutorial.
My problem at the moment is that I'm trying to merge the DrawerLayout
with my Activity
because I wanna launch the slide menu over the Activity
that I already have. Here is the merged XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/pantallaPrincipalGenerarBackgroundView">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView"
android:fillViewport="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Seleccione dificultad"
android:id="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" />
<RadioGroup
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:gravity="top|center"
android:id="@+id/linearLayout"
android:layout_gravity="bottom">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facil"
android:id="@+id/FacilButton"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medio"
android:id="@+id/MedioButton2"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Difícil"
android:id="@+id/DifícilButton"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" />
</RadioGroup>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:id="@+id/linearLayout2"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:weightSum="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/longitudText"
android:hint="Introduce longitud"
android:singleLine="true"
android:layout_weight="0.45" />
<Button
android:layout_marginTop="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generar"
android:id="@+id/Generarbutton"
android:layout_marginLeft="20dp" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Pass"
android:id="@+id/contraseniaActual"
android:layout_below="@+id/linearLayout2"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:textColor="#ff1024ff"
android:textSize="15dp"
android:singleLine="true"
android:shadowColor="#ff69ff00"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Usuario"
android:id="@+id/textView2"
android:layout_below="@+id/passwordText"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="229dp"
android:layout_height="wrap_content"
android:id="@+id/nombreUsuarioTextView"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="Introduzca usuario"
android:layout_margin="7dp"
android:singleLine="true"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Seleccione el servicio"
android:id="@+id/textView3"
android:layout_below="@+id/newUserText"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Servicios"
android:drawableLeft="@drawable/flechaizquierda"
android:id="@+id/abrirListaServiciosButton"
android:layout_above="@+id/guardarButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toEndOf="@+id/abrirListaServiciosButton"
android:gravity="center"
android:weightSum="1"
android:layout_alignTop="@+id/abrirListaServiciosButton"
android:layout_toRightOf="@+id/abrirListaServiciosButton"
android:layout_marginTop="0dp"
android:layout_alignRight="@+id/newUserText"
android:layout_alignEnd="@+id/newUserText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SERVICIOTEST0"
android:id="@+id/textoMostrarServicioSeleccionado"
android:textColor="#ff000000"
android:maxLength="13"
android:maxLines="1" />
<ImageView
android:layout_width="80dp"
android:layout_height="match_parent"
android:id="@+id/imageMostrarServicioSeleccionado"
android:layout_gravity="right" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Guardar"
android:id="@+id/guardarButton"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/guardarButton"
android:id="@+id/linearLayout5"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Salir"
android:id="@+id/salirycerrarPantallaGenerarButton"
android:layout_alignBottom="@+id/guardarButton"
android:layout_alignLeft="@+id/goToSegundabutton"
android:layout_alignStart="@+id/goToSegundabutton"
android:layout_margin="1dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancelar"
android:id="@+id/goToSegundabutton"
android:layout_alignTop="@+id/guardarButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<!-- Slide menu code, supuestamente. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
As you can see at the bottom of the file, I just added out of all my activity but inside the parent RelativeLayout
their DrawerLayout
. Now the problem I'm getting is when addressing it from the java code.
When I do mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
it says that they are inconvertable types, cannot cast from android.view.View
to android.support.v4.widget.DrawerLayout
.
Any idea how could I do that?? I don't care about changing my code, but I want this menu to come over my activity, not over an empty one. I just didn't find any good tutorial or video tutorial over the internet.
Thank you so much :)
I suggest you to take a look at Google IO source code. You have there pretty good example of using DrawerLayout.
And it's easy.
First of all.
It's good to have DrawerLayout on top.
For example. MapActivity - activity_map.xml
And as you can see you have NavDrawer include at the end.
nadrawer.xml
You'll find whole project here: https://github.com/google/iosched