mardi 21 juin 2016

Navigation drawer not appearing while running the app in android AVD

I have made a custom toolbar watching tutorials online with a searchview and a separate activity to display the results. For now just a toast is displayed when performing searches. It works fine. And then I decided to add a navigationdrawer to the existing app with a few options which do nothing for now. But it doesn't work. In the tutorials the dude creates a new project and with an empty activity and then designs a navigationdrawer but I tried to add the navigation drawer in the existing app by adding an empty activity and following the guy in the tutorial and it doesn't work. It doesn't give any error. The navigationdrawer simply isn't there.

These are my files

This might give you guys a sense of what my code for the toolbar and searchview looks like. Since posting all the code would be too long.

But I will post the code for the navigation drawer.

activity_nav_drawer.xml

<android.support.v4.widget.DrawerLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zoggfrombetelgeuse.clef.NavDrawer"
    android:id="@+id/drawerLayout">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:gravity="center">

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/app_name"/>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/nav_menu"
        android:layout_gravity="start">

    </android.support.design.widget.NavigationView>

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

NavDrawer.java

public class NavDrawer extends AppCompatActivity {

    ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_drawer);

        DrawerLayout drawerLayout = (DrawerLayout)   findViewById(R.id.drawerLayout);
        toggle= new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (toggle.onOptionsItemSelected(item))
            return true;

        return super.onOptionsItemSelected(item);
    }
}

build.gradle

apply plugin: 'com.android.application'

android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.zoggfrombetelgeuse.clef"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

  buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

What am I doing wrong. If you guys need all the codes, please ask. Thanks

Aucun commentaire:

Enregistrer un commentaire