Created
July 28, 2021 21:23
-
-
Save codinginflow/2330259bd0aa53e6adacf9a24920f44f to your computer and use it in GitHub Desktop.
Navigation Drawer Tutorial Part 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fitsSystemWindows="true" | |
tools:context="com.codinginflow.navigationdrawerexample.MainActivity" | |
tools:openDrawer="start"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
android:background="@color/colorPrimary" | |
android:elevation="4dp" | |
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" | |
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> | |
<FrameLayout | |
android:id="@+id/fragment_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> | |
<android.support.design.widget.NavigationView | |
android:id="@+id/nav_view" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
app:headerLayout="@layout/nav_header" | |
app:menu="@menu/drawer_menu" /> | |
</android.support.v4.widget.DrawerLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.codinginflow.navigationdrawerexample"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity | |
android:name=".MainActivity" | |
android:theme="@style/AppTheme.NoActionBar"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.navigationdrawerexample; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class ChatFragment extends Fragment { | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_chat, container, false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/holo_blue_light"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Chat Fragment" | |
android:textSize="30sp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/holo_red_light"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Message Fragment" | |
android:textSize="30sp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@android:color/holo_green_light"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Profile Fragment" | |
android:textSize="30sp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.navigationdrawerexample; | |
import android.support.annotation.NonNull; | |
import android.support.design.widget.NavigationView; | |
import android.support.v4.view.GravityCompat; | |
import android.support.v4.widget.DrawerLayout; | |
import android.support.v7.app.ActionBarDrawerToggle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.Toolbar; | |
import android.view.MenuItem; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { | |
private DrawerLayout drawer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
drawer = findViewById(R.id.drawer_layout); | |
NavigationView navigationView = findViewById(R.id.nav_view); | |
navigationView.setNavigationItemSelectedListener(this); | |
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, | |
R.string.navigation_drawer_open, R.string.navigation_drawer_close); | |
drawer.addDrawerListener(toggle); | |
toggle.syncState(); | |
if (savedInstanceState == null) { | |
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, | |
new MessageFragment()).commit(); | |
navigationView.setCheckedItem(R.id.nav_message); | |
} | |
} | |
@Override | |
public boolean onNavigationItemSelected(@NonNull MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.nav_message: | |
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, | |
new MessageFragment()).commit(); | |
break; | |
case R.id.nav_chat: | |
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, | |
new ChatFragment()).commit(); | |
break; | |
case R.id.nav_profile: | |
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, | |
new ProfileFragment()).commit(); | |
break; | |
case R.id.nav_share: | |
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show(); | |
break; | |
case R.id.nav_send: | |
Toast.makeText(this, "Send", Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
drawer.closeDrawer(GravityCompat.START); | |
return true; | |
} | |
@Override | |
public void onBackPressed() { | |
if (drawer.isDrawerOpen(GravityCompat.START)) { | |
drawer.closeDrawer(GravityCompat.START); | |
} else { | |
super.onBackPressed(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.navigationdrawerexample; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class MessageFragment extends Fragment { | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_message, container, false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinginflow.navigationdrawerexample; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class ProfileFragment extends Fragment { | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
return inflater.inflate(R.layout.fragment_profile, container, false); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">Navigation Drawer Example</string> | |
<string name="navigation_drawer_open">Open navigation drawer</string> | |
<string name="navigation_drawer_close">Close navigation drawer</string> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | |
<!-- Customize your theme here. --> | |
<item name="colorPrimary">@color/colorPrimary</item> | |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |
<item name="colorAccent">@color/colorAccent</item> | |
</style> | |
<style name="AppTheme.NoActionBar"> | |
<item name="windowActionBar">false</item> | |
<item name="windowNoTitle">true</item> | |
</style> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<style name="AppTheme.NoActionBar"> | |
<item name="windowActionBar">false</item> | |
<item name="windowNoTitle">true</item> | |
<item name="android:statusBarColor">@android:color/transparent</item> | |
</style> | |
</resources> |
is it the same procedure while using androidx ?
yes
how define background color for the selected menu??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is it the same procedure while using androidx ?