NavigationDrawer not appearing with AndroidX - android

I have an app that I am using with androidx components and I cannot get it to show the hamburger icon indicating a DrawerActivity. Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer_layout"
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:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
style="#style/Widget.MyApp.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/sliding_tabs"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<com.webnation.begonerobotexters.widgets.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintBottom_toTopOf="#+id/viewpager"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/sliding_tabs"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- The navigation drawer -->
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="#android:color/white"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>
Here is how I am setting up navigation drawer in the activity:
private fun setUpNavigationDrawer() {
setSupportActionBar(toolbar)
val actionBar = supportActionBar
try {
assert(actionBar != null)
actionBar?.setDisplayHomeAsUpEnabled(true)
actionBar?.setHomeButtonEnabled(true)
//actionBar?.setSubtitle(getString(R.string.subtitle))
actionBar?.setDisplayShowTitleEnabled(true)
} catch (ignored: Exception) {
Timber.e(ignored)
ignored.printStackTrace()
}
toolbar?.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))
toolbar?.setTitleTextColor(ContextCompat.getColor(this, R.color.tab_text_color))
navigation_view.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true
when (menuItem.itemId) {
R.id.navigation_item_1 -> {
val intent = Intent(this, EulaActivity::class.java)
intent.putExtra(keyFileName, "privacy")
startActivity(intent)
}
R.id.navigation_item_2 -> {
val intent = Intent(this, EulaActivity::class.java)
intent.putExtra(keyFileName, "eula")
startActivity(intent)
}
R.id.navigation_item_3 -> {
}
}
if (navigation_view != null) {
drawer_layout?.closeDrawer(navigation_view)
}
true
}
mDrawerToggle = object: ActionBarDrawerToggle(this, drawer_layout, toolbar,R.string.drawer_open, R.string.drawer_close) {
override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)
invalidateOptionsMenu()
}
override fun onDrawerClosed(view: View) {
super.onDrawerClosed(view)
//getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu()
}
}
mDrawerToggle.isDrawerIndicatorEnabled = true
drawer_layout?.addDrawerListener(mDrawerToggle)
mDrawerToggle.syncState()
}
Here is my DrawActivity:
class DrawerActivity : AppCompatActivity() {
lateinit var draw_layout: DrawerLayout
lateinit var mDrawerToggle: ActionBarDrawerToggle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onStart() {
super.onStart()
draw_layout = findViewById<DrawerLayout>(R.id.drawer_layout)
mDrawerToggle = ActionBarDrawerToggle(this, draw_layout, 0, 0)
draw_layout.addDrawerListener(mDrawerToggle)
val actionBar = supportActionBar
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setDisplayShowHomeEnabled(true)
actionBar.setDisplayShowTitleEnabled(true)
actionBar.setDisplayUseLogoEnabled(false)
actionBar.setHomeButtonEnabled(true)
}
}
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
mDrawerToggle.syncState()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item)
}
}
What am I doing wrong?

ActionBarDrawerToggle is deprecated for quite a while; one can use the framework's own resId for that button (to be inflated into the ActionBar's custom view - so that it will appear where it should):
<androidx.appcompat.widget.AppCompatImageButton
android:id="#android:id/home"
android:src="#drawable/ic_menu_black_36dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#null" />
The one can get the click event along with top-right menu events; for example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
/* hide/show NavigationView */
break;
...
}
}

I found out my viewpager/sliding bar tab was covering the action bar. So I switched out my ConstraintLayout for a LinearLayout, and poof it appeared.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
style="#style/Widget.MyApp.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
/>
<com.webnation.begonerobotexters.widgets.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
<!-- The navigation drawer -->
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="#android:color/white"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>

Related

Add menu icons from fragement into host actvity toolbar - Navigation component

Iam using single activity navigation component structure.
In host activity iam using navigation drawer and bottom navigation.
So There are 2 frgaments inside bottom navigation Fragment A and Fragment B.
I need to add menu icons (search icon) for Fragment A and (chat icon) for Fragment B.
So how i can add these icons on Host Activity toolbar from fragment ?
HostActivity
private fun setupViews()
{
drawerLayout = binding.drawerLayout
// Finding the Navigation Controller
navController = findNavController(R.id.hostFragmentLanding)
// Setting Navigation Controller with the BottomNavigationView
binding.bottomNavView.setupWithNavController(navController)
appBarConfiguration = AppBarConfiguration(topLevelDestinationIds = TOP_LEVEL_DESTINATIONS, drawerLayout)
// Set up ActionBar
setSupportActionBar(binding.toolBar)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navigationView.setupWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
host_activity_xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title=""
app:theme="#style/ToolbarTheme"
>
<ImageView
android:id="#+id/logo"
android:layout_width="97dp"
android:layout_height="#dimen/dp_20"
android:src="#drawable/logo_sportsal"
android:layout_gravity="center"
/>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/hostFragmentLanding"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="#navigation/navigation_landing" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
style="#style/Widget.MaterialComponents.BottomNavigationView"
app:menu="#menu/bottom_navigation_menu"
/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
style="#style/Widget.MaterialComponents.NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start" />
</androidx.drawerlayout.widget.DrawerLayout>
I want to add menu (search)icon from Fragment A to the Hostactivity Toolbar
I have added menu icons directlty from the fragment class
Fragment A
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_home_fragment, menu)
menu.findItem(R.id.search).isVisible = false
menu.findItem(R.id.qrScan).isVisible = false
menu.findItem(R.id.notification).isVisible = true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.search) {
}
return super.onOptionsItemSelected(item)
}
So the menu items has been added to the Host Activities Toolbar automatically
I was having the same problem and this anwser solved my problem
I have overrided my onCreateOptionsMenu and onOptionsItemSelected functions in my fragment and the icon wasn't being shown in the toolbar. I only needed to add these lines to my onCreate method in my activity
setSupportActionBar(binding.activityMainToolbar);
getSupportActionBar()?.setDisplayShowTitleEnabled(false);

toolbar.setNavigationOnClickListener not working with AppBarLayout

Back button is not clickable. Tried this answer but it doesn't work.
override fun onCreate(savedInstanceState: Bundle?) {
val toolbar = findViewById<Toolbar>(R.id.toolbar)
toolbar.setNavigationIcon(R.drawable.ic_navbar_cancel)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowHomeEnabled(true)
toolbar.setNavigationOnClickListener { Log.d("Test", "Clicked!") }
}
title_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/app_bar_layout"
style="?attr/appBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#id/toolbar"
style="?attr/toolBarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
android:id="#+id/toolbar_title"
style="?attr/toolBarTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:fontFamily="#font/intro_bold_caps"
android:paddingStart="0dp"
android:paddingEnd="6dp" />
</androidx.appcompat.widget.Toolbar>
<View
android:id="#+id/toolbar_shadow_pre_lollipop"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#id/toolbar"
android:background="#drawable/toolbar_shadow_pre_lollipop" />
</com.google.android.material.appbar.AppBarLayout>
ic_navbar_cancel.xml
<path
android:fillColor="#003b5a"
android:pathData="M18,18 L30,30 M18,30 L30,18"
android:strokeColor="#003b5a"
android:strokeWidth="3"/>
Since you are using the setSupportActionBar() method use the ActionBar API:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
Log.d("Test", "Clicked!")
}
return super.onOptionsItemSelected(item)
}

Side Navigation Drawer Item click is not working although Navigation Drawer is visible and opens

I am trying to Implement Navigation drawer but when I run my application and click on items in the drawer it just closes the drawer without doing showing the toast. nothing is happening with my drawer when I click an Item to navigate i.e The content inside Navigation Drawer is not clickable i.e the menu.
Below are my codes for Navigation drawer:
Activity Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:openDrawer="start">
<include
layout="#layout/content_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu"/>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstMain"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:background="#color/back"
android:fontFamily="#font/berkshire_swash"
android:gravity="center"
android:text="#string/app_name"
android:textAlignment="center"
android:textColor="#color/txt"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:layout_margin="30sp"
android:background="#color/back"
android:fontFamily="#font/berkshire_swash"
android:gravity="center_horizontal"
android:padding="10sp"
android:text="PLAY"
android:textColor="#color/txt"
android:textSize="35sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_rule"
android:icon="#drawable/rules"
android:title="Rules" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/about"
android:title="About"/>
</group>
</menu>
Content_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/puzzle">
</LinearLayout>
</LinearLayout>
Main.kt
class FirstMain : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
lateinit var btn : Button
lateinit var toolbar: Toolbar
lateinit var drawerLayout: DrawerLayout
lateinit var navView: NavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first_main)
btn = findViewById(R.id.button)
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(
this, drawerLayout, toolbar, 0, 0
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
btn.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
navView.setNavigationItemSelectedListener(this)
}
override fun onNavigationItemSelected(p0: MenuItem): Boolean {
p0.isChecked = true
when (p0.itemId) {
R.id.nav_rule -> {
Toast.makeText(this, "Profile clicked", Toast.LENGTH_LONG).show()
}
R.id.nav_about -> {
Toast.makeText(this, "Messages clicked", Toast.LENGTH_LONG).show()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
}
Your code looks all good, weird it doesn't work but you can try and add
navView.bringToFront();
navView.setNavigationItemSelectedListener(this)

How to make the drawer go over the action bar?

I added a NavigationView to my DrawerLayout. The problem is that it goes under the action and status bar. I would like it to be above preferably both bars, but I would be happy to get it at least over the action bar.
Even if the background color of the action bar is transparent it gets darker as the drawer is opened.
this is how my main activity looks like in xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/background"
android:fitsSystemWindows="true"
android:id="#+id/drawerLayout"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/sideMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#drawable/navigation_menu_background"
app:menu="#menu/navigation_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>
and here is the Kotlin code:
class MainActivity : AppCompatActivity() {
private var playlistFragment = PlaylistFragment.newInstance()
private var streamFragment = StreamFragment.newInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
replaceFragment(playlistFragment)
sideMenu.setNavigationItemSelectedListener { item ->
handleNavigationItemTap(item)
true
}
}
private fun replaceFragment(fragment:Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentContainer, fragment)
fragmentTransaction.commit()
}
private fun handleNavigationItemTap(item: MenuItem){
when(item.itemId){
R.id.playlistFragmentItem -> replaceFragment(playlistFragment)
R.id.singleStreamItem -> replaceFragment(streamFragment)
}
drawerLayout.closeDrawer(GravityCompat.START)
}
}

How can I use a back arrow in my actionbar of my tablayout?

I want to create a back-arrow in my ActionBar in Main3activity.
In my drawermenu I use fragments, but I can't fix it to make a fragment
with TabLayout so i choose to use an activity and that works for me. But
when I'm on the Main3activity and I want to go back to MainActivity I
want to use an arrow in my ActionBar of something else. Can someone
help me please?
First I want to try it with fragments, but it was not work for me to at
TabLayout at a fragment. So I choose activity.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.drauwertab20190621">
<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=".Main3Activity"
android:label="#string/title_activity_main3"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
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>
MainActivity.kt
package com.example.drauwertab20190621
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.NavigationView
import android.support.design.widget.Snackbar
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.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
class MainActivity : AppCompatActivity(),
NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG).setAction("Action", null).show()
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is
present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_home -> {
// Handle the camera action
}
R.id.nav_gallery -> {
}
R.id.nav_slideshow -> {
val i = Intent(this#MainActivity, Main3Activity::class.java)
startActivity(i)
}
R.id.nav_tools -> {
}
R.id.nav_share -> {
}
R.id.nav_send -> {
}
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
}
Main3Activity.kt
package com.example.drauwertab20190621
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.design.widget.TabLayout
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import com.example.drauwertab20190621.ui.main.SectionsPagerAdapter
class Main3Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)
val sectionsPagerAdapter = SectionsPagerAdapter(this,
supportFragmentManager)
val viewPager: ViewPager = findViewById(R.id.view_pager)
viewPager.adapter = sectionsPagerAdapter
val tabs: TabLayout = findViewById(R.id.tabs)
tabs.setupWithViewPager(viewPager)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG).setAction("Action", null).show()
}
}
}
activity_main.xml
<?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:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main3Activity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.Widget
.AppCompat.Toolbar.Title"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email"/>
content.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
fragment_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#+id/constraintLayout"/>
</android.support.constraint.ConstraintLayout>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#mipmap/ic_launcher_round"
android:contentDescription="#string/nav_header_desc"
android:id="#+id/imageView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="#string/nav_header_title"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nav_header_subtitle"
android:id="#+id/textView"/>
</LinearLayout>
First of all its not a good practice to named activity as "Main3activity".
- Check here
For setting arrow back button to toolbar your should add:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
You can also use that in Fragments that are attached to ActionBarActivities you can use it like this:
((YourActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((YourActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
package com.example.drauwertab20190621
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.design.widget.TabLayout
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import com.example.drauwertab20190621.ui.main.SectionsPagerAdapter
class Main3Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)
val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
val viewPager: ViewPager = findViewById(R.id.view_pager)
viewPager.adapter = sectionsPagerAdapter
val tabs: TabLayout = findViewById(R.id.tabs)
tabs.setupWithViewPager(viewPager)
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
//backbutton in actionbar
val actionbar = supportActionBar
actionbar!!.title="1st Division"
actionbar.setDisplayHomeAsUpEnabled(true)
actionbar.setDisplayShowHomeEnabled(true)
}
}

Categories

Resources