1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android Navigation Drawer with activities

Discussion in 'Android Development' started by devx, Jan 20, 2016.

  1. devx

    devx Newbie
    Thread Starter

    I downloaded android navigationdrawer project from HERE

    And i wanna change ImportFragment to activity and set it in Drawer?

    Please if can anyone help me
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Change

    Change Lurker

    instead of
    Code (Text):
    1.  fm.beginTransaction().replace(R.id.content_frame, new ImportFragment()).commit();
    You would use:
    Code (Text):
    1.  Intent i = new Intent(this, ImportActivity.class)
    2. startActivity(i);
    Correct me if I am wrong as I am new to coding

    -Change
     
  3. devx

    devx Newbie
    Thread Starter

    I try this but its not working. i cannot run app it closing my second activity is

    public class ImportActivity extends Activity {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_import,container,false);
    }
    }

    Is there something wrong?
     
  4. devx

    devx Newbie
    Thread Starter

    Its work.. Thx man..
    I was forget add my activity to manifest
     
  5. devx

    devx Newbie
    Thread Starter

    Do you know why my app not working if i add toolbar in my second activity??
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    I like to have the same look in all activities, do anyone know how create it?

    Thx for help
     
  6. devx

    devx Newbie
    Thread Starter

    anyone please??
     
  7. Deleted User

    Deleted User Guest

    What do you mean 'my app not working'? Does it crash?
    You've given virtually no code. It's impossible to answer your question, with the lack of information you've given.
     
  8. devx

    devx Newbie
    Thread Starter

    first.java

    Code (Text):
    1.  
    2. public class first extends AppCompatActivity {
    3.     protected void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setContentView(R.layout.first);
    6.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    7.  
    8.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    9.         fab.setOnClickListener(new View.OnClickListener() {
    10.             [USER=1021285]@override[/USER]
    11.             public void onClick(View view) {
    12.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    13.                         .setAction("Action", null).show();
    14.             }
    15.         });
    16.  
    17.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    18.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    19.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    20.         drawer.setDrawerListener(toggle);
    21.         toggle.syncState();
    22.  
    23.         //NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    24.  
    25.     }
    26. }
    27.  
    first.xml:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <RelativeLayout
    4.     xmlns:android="http://schemas.android.com/apk/res/android"
    5.     xmlns:app="http://schemas.android.com/apk/res-auto"
    6.     xmlns:tools="http://schemas.android.com/tools"
    7.     android:layout_width="match_parent"
    8.     android:layout_height="match_parent"
    9.     app:layout_behavior="[USER=696546]@String[/USER]/appbar_scrolling_view_behavior"
    10.     tools:showIn="@layout/app_bar_main">
    11.  
    12. </RelativeLayout>
    13.  
    app_bar_main.xml:
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent"
    8.     android:fitsSystemWindows="true"
    9.     tools:context="com.example.sasor.tst.MainActivity">
    10.  
    11.     <android.support.design.widget.AppBarLayout
    12.         android:layout_width="match_parent"
    13.         android:layout_height="wrap_content"
    14.         android:theme="[USER=19691]@Style[/USER]/AppTheme.AppBarOverlay">
    15.  
    16.         <android.support.v7.widget.Toolbar
    17.             android:id="@+id/toolbar"
    18.             android:layout_width="match_parent"
    19.             android:layout_height="?attr/actionBarSize"
    20.             android:background="?attr/colorPrimary"
    21.             app:popupTheme="[USER=19691]@Style[/USER]/AppTheme.PopupOverlay" />
    22.  
    23.     </android.support.design.widget.AppBarLayout>
    24.  
    25.     <include layout="@layout/content_main" />
    26.     <include layout="@layout/first" />
    27.  
    28.     <android.support.design.widget.FloatingActionButton
    29.         android:id="@+id/fab"
    30.         android:layout_width="wrap_content"
    31.         android:layout_height="wrap_content"
    32.         android:layout_gravity="bottom|end"
    33.         android:layout_margin="@dimen/fab_margin"
    34.         android:src="@android:drawable/ic_dialog_email" />
    35.  
    36. </android.support.design.widget.CoordinatorLayout>
    37.  
    When i try to open my activity, program crash.
     
    #8 devx, Jan 22, 2016
    Last edited: Jan 22, 2016
  9. Deleted User

    Deleted User Guest

    Can you put your code in [code] tags, and indent it properly please?

    Also, if the program crashed, it will have generated a stack trace in the Logcat output. Please show this information.
     
  10. devx

    devx Newbie
    Thread Starter

    Code (Text):
    1. 01-22 10:30:12.718 358-678/? W/ActivityManager: in case of changing top-activity!![com.example.sasor.tst]
    2. 01-22 10:30:12.718 358-678/? I/ActivityManager: START {cmp=com.example.sasor.tst/.first u=0} from pid 24217
    3. 01-22 10:30:12.808 24217-24217/? D/AndroidRuntime: Shutting down VM
    4. 01-22 10:30:12.808 24217-24217/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x410da2a0)
    5. 01-22 10:30:12.808 24217-24217/? E/AndroidRuntime: FATAL EXCEPTION: main
    6.                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sasor.tst/com.example.sasor.tst.first}: java.lang.NullPointerException
    7.                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
    8.                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
    9.                                                        at android.app.ActivityThread.access$600(ActivityThread.java:138)
    10.                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
    11.                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
    12.                                                        at android.os.Looper.loop(Looper.java:137)
    13.                                                        at android.app.ActivityThread.main(ActivityThread.java:4872)
    14.                                                        at java.lang.reflect.Method.invokeNative(Native Method)
    15.                                                        at java.lang.reflect.Method.invoke(Method.java:511)
    16.                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    17.                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    18.                                                        at dalvik.system.NativeStart.main(Native Method)
    19.                                                     Caused by: java.lang.NullPointerException
    20.                                                        at com.example.sasor.tst.first.onCreate(first.java:23)
    21.                                                        at android.app.Activity.performCreate(Activity.java:5191)
    22.                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
    23.                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
    24.                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
    25.                                                        at android.app.ActivityThread.access$600(ActivityThread.java:138)
    26.                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
    27.                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
    28.                                                        at android.os.Looper.loop(Looper.java:137)
    29.                                                        at android.app.ActivityThread.main(ActivityThread.java:4872)
    30.                                                        at java.lang.reflect.Method.invokeNative(Native Method)
    31.                                                        at java.lang.reflect.Method.invoke(Method.java:511)
    32.                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    33.                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    34.                                                        at dalvik.system.NativeStart.main(Native Method)
    35. 01-22 10:30:12.818 358-16492/? W/ActivityManager:   Force finishing activity com.example.sasor.tst/.first
    36. 01-22 10:30:12.818 358-16492/? W/ActivityManager:   Force finishing activity com.example.sasor.tst/.MainActivity
     
  11. Deleted User

    Deleted User Guest

    You have a null pointer exception at line 23 in first.java. Looking at your code, I would say that this statement evaluates to null

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
     
  12. devx

    devx Newbie
    Thread Starter

    If i do this without button
    Code (Text):
    1.  
    2. protected void onCreate(Bundle savedInstanceState) {
    3.     super.onCreate(savedInstanceState);
    4.     setContentView(R.layout.first);
    5.     View myView = findViewById(R.id.toolbar);
    6.  
    7.     Toolbar mtoolbar = (Toolbar) myView.findViewById(R.id.toolbar);
    8.     setSupportActionBar(mtoolbar);
    9.  
    10. }
    11.  
    i get the same error.
    null pointer exception is there:
    Toolbar mtoolbar = (Toolbar) myView.findViewById(R.id.toolbar);

    but i dont know what to do
     
  13. Deleted User

    Deleted User Guest

    I think the problem is you have set the content view to R.layout.first, but this layout does not contain a component called R.id.toolbar. So myView gets a value null.
    What is the layout XML for activity first?
     
  14. devx

    devx Newbie
    Thread Starter

    look up where i paste codes of my project. I m edit post..
     
  15. devx

    devx Newbie
    Thread Starter

    I think its easyer if you tell me how start:)
    i create new navigationdrawer project in android studio

    MainActivity.java
    Code (Text):
    1.  
    2. package com.example.sasor.myapplication;
    3.  
    4. import android.os.Bundle;
    5. import android.support.design.widget.FloatingActionButton;
    6. import android.support.design.widget.Snackbar;
    7. import android.view.View;
    8. import android.support.design.widget.NavigationView;
    9. import android.support.v4.view.GravityCompat;
    10. import android.support.v4.widget.DrawerLayout;
    11. import android.support.v7.app.ActionBarDrawerToggle;
    12. import android.support.v7.app.AppCompatActivity;
    13. import android.support.v7.widget.Toolbar;
    14. import android.view.Menu;
    15. import android.view.MenuItem;
    16.  
    17. public class MainActivity extends AppCompatActivity
    18.         implements NavigationView.OnNavigationItemSelectedListener {
    19.  
    20.     @Override
    21.     protected void onCreate(Bundle savedInstanceState) {
    22.         super.onCreate(savedInstanceState);
    23.         setContentView(R.layout.activity_main);
    24.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    25.         setSupportActionBar(toolbar);
    26.  
    27.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    28.         fab.setOnClickListener(new View.OnClickListener() {
    29.             @Override
    30.             public void onClick(View view) {
    31.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    32.                         .setAction("Action", null).show();
    33.             }
    34.         });
    35.  
    36.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    37.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    38.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    39.         drawer.setDrawerListener(toggle);
    40.         toggle.syncState();
    41.  
    42.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    43.         navigationView.setNavigationItemSelectedListener(this);
    44.     }
    45.  
    46.     @Override
    47.     public void onBackPressed() {
    48.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    49.         if (drawer.isDrawerOpen(GravityCompat.START)) {
    50.             drawer.closeDrawer(GravityCompat.START);
    51.         } else {
    52.             super.onBackPressed();
    53.         }
    54.     }
    55.  
    56.     @Override
    57.     public boolean onCreateOptionsMenu(Menu menu) {
    58.         // Inflate the menu; this adds items to the action bar if it is present.
    59.         getMenuInflater().inflate(R.menu.main, menu);
    60.         return true;
    61.     }
    62.  
    63.     @Override
    64.     public boolean onOptionsItemSelected(MenuItem item) {
    65.         // Handle action bar item clicks here. The action bar will
    66.         // automatically handle clicks on the Home/Up button, so long
    67.         // as you specify a parent activity in AndroidManifest.xml.
    68.         int id = item.getItemId();
    69.  
    70.         //noinspection SimplifiableIfStatement
    71.         if (id == R.id.action_settings) {
    72.             return true;
    73.         }
    74.  
    75.         return super.onOptionsItemSelected(item);
    76.     }
    77.  
    78.     @SuppressWarnings("StatementWithEmptyBody")
    79.     @Override
    80.     public boolean onNavigationItemSelected(MenuItem item) {
    81.         // Handle navigation view item clicks here.
    82.         int id = item.getItemId();
    83.  
    84.         if (id == R.id.nav_camera) {
    85.             // Handle the camera action
    86.         } else if (id == R.id.nav_gallery) {
    87.  
    88.         } else if (id == R.id.nav_slideshow) {
    89.  
    90.         } else if (id == R.id.nav_manage) {
    91.  
    92.         } else if (id == R.id.nav_share) {
    93.  
    94.         } else if (id == R.id.nav_send) {
    95.  
    96.         }
    97.  
    98.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    99.         drawer.closeDrawer(GravityCompat.START);
    100.         return true;
    101.     }
    102. }
    103.  
    activity_main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:id="@+id/drawer_layout"
    7.     android:layout_width="match_parent"
    8.     android:layout_height="match_parent"
    9.     android:fitsSystemWindows="true"
    10.     tools:openDrawer="start">
    11.  
    12.     <include
    13.         layout="@layout/app_bar_main"
    14.         android:layout_width="match_parent"
    15.         android:layout_height="match_parent" />
    16.  
    17.     <android.support.design.widget.NavigationView
    18.         android:id="@+id/nav_view"
    19.         android:layout_width="wrap_content"
    20.         android:layout_height="match_parent"
    21.         android:layout_gravity="start"
    22.         android:fitsSystemWindows="true"
    23.         app:headerLayout="@layout/nav_header_main"
    24.         app:menu="@menu/activity_main_drawer" />
    25.  
    26. </android.support.v4.widget.DrawerLayout>
    27.  
    app_bar_main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent"
    8.     android:fitsSystemWindows="true"
    9.     tools:context="com.example.sasor.myapplication.MainActivity">
    10.  
    11.     <android.support.design.widget.AppBarLayout
    12.         android:layout_width="match_parent"
    13.         android:layout_height="wrap_content"
    14.         android:theme="@style/AppTheme.AppBarOverlay">
    15.  
    16.         <android.support.v7.widget.Toolbar
    17.             android:id="@+id/toolbar"
    18.             android:layout_width="match_parent"
    19.             android:layout_height="?attr/actionBarSize"
    20.             android:background="?attr/colorPrimary"
    21.             app:popupTheme="@style/AppTheme.PopupOverlay" />
    22.  
    23.     </android.support.design.widget.AppBarLayout>
    24.  
    25.     <include layout="@layout/content_main" />
    26.  
    27.     <android.support.design.widget.FloatingActionButton
    28.         android:id="@+id/fab"
    29.         android:layout_width="wrap_content"
    30.         android:layout_height="wrap_content"
    31.         android:layout_gravity="bottom|end"
    32.         android:layout_margin="@dimen/fab_margin"
    33.         android:src="@android:drawable/ic_dialog_email" />
    34.  
    35. </android.support.design.widget.CoordinatorLayout>
    36.  
    content_main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent"
    8.     android:paddingBottom="@dimen/activity_vertical_margin"
    9.     android:paddingLeft="@dimen/activity_horizontal_margin"
    10.     android:paddingRight="@dimen/activity_horizontal_margin"
    11.     android:paddingTop="@dimen/activity_vertical_margin"
    12.     app:layout_behavior="@string/appbar_scrolling_view_behavior"
    13.     tools:context="com.example.sasor.myapplication.MainActivity"
    14.     tools:showIn="@layout/app_bar_main">
    15.  
    16.     <TextView
    17.         android:layout_width="wrap_content"
    18.         android:layout_height="wrap_content"
    19.         android:text="Hello World!" />
    20. </RelativeLayout>
    21.  
    nav_header_main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="@dimen/nav_header_height"
    6.     android:background="@drawable/side_nav_bar"
    7.     android:gravity="bottom"
    8.     android:orientation="vertical"
    9.     android:paddingBottom="@dimen/activity_vertical_margin"
    10.     android:paddingLeft="@dimen/activity_horizontal_margin"
    11.     android:paddingRight="@dimen/activity_horizontal_margin"
    12.     android:paddingTop="@dimen/activity_vertical_margin"
    13.     android:theme="@style/ThemeOverlay.AppCompat.Dark">
    14.  
    15.     <ImageView
    16.         android:id="@+id/imageView"
    17.         android:layout_width="wrap_content"
    18.         android:layout_height="wrap_content"
    19.         android:paddingTop="@dimen/nav_header_vertical_spacing"
    20.         android:src="@android:drawable/sym_def_app_icon" />
    21.  
    22.     <TextView
    23.         android:layout_width="match_parent"
    24.         android:layout_height="wrap_content"
    25.         android:paddingTop="@dimen/nav_header_vertical_spacing"
    26.         android:text="Android Studio"
    27.         android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    28.  
    29.     <TextView
    30.         android:id="@+id/textView"
    31.         android:layout_width="wrap_content"
    32.         android:layout_height="wrap_content"
    33.         android:text="android.studio@android.com" />
    34.  
    35. </LinearLayout>
    36.  
    Now i like to know how can i add new activity class which open when i click on drawer item, and it have the same toolbar, and navigation drawer like MainActivity?

    And really thx you for your time
     
Loading...
Similar Threads - Android Navigation Drawer
  1. Scadica
    Replies:
    0
    Views:
    122
  2. SrLimon
    Replies:
    0
    Views:
    135
  3. Olive
    Replies:
    2
    Views:
    145
  4. charlington
    Replies:
    5
    Views:
    223
  5. whois1230
    Replies:
    4
    Views:
    237
  6. Jason Edwardes
    Replies:
    0
    Views:
    138
  7. EvasiveEmanual
    Replies:
    4
    Views:
    436
  8. Derek John Castle
    Replies:
    0
    Views:
    441
  9. thedavegray
    Replies:
    5
    Views:
    825
  10. BenyBen
    Replies:
    2
    Views:
    818

Share This Page

Loading...