Hey guys I am trying to set title in my custom toolbar. It's not working until I explicit through xml. I don't want to put a Textview in xml in toolbar. I tried this post and trying to set by code, it is not working.
consultation.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:focusable="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:backgroundTint="#FFC04A"
android:fitsSystemWindows="true"
android:gravity="bottom">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<androidx.appcompat.widget.SearchView
android:id="#+id/consultationSearchView"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="#drawable/consultations_search_edit_text_rounded_corner"
android:fitsSystemWindows="true"
android:theme="#style/ConsultationsSearchViewTheme"
app:layout_collapseMode="pin" />
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin"
app:titleTextColor="#android:color/white" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I tried through code
ToolbarActivity.kt
class ToolbarActivity : AppCompatActivity() {
private lateinit var binding: ToolBarLayoutBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ToolBarLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
actionBar()
setupSearchView()
}
private fun actionBar() {
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowTitleEnabled(true)
supportActionBar?.title = "Toolbar"
}
private fun setupSearchView() {
var originalMargin = 0
binding.consultationSearchView.apply {
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?) = false
override fun onQueryTextChange(newText: String?): Boolean {
if (newText != null) {
}
return true
}
})
val params =
binding.consultationSearchView.layoutParams as CollapsingToolbarLayout.LayoutParams
originalMargin = params.marginStart
setOnQueryTextFocusChangeListener { view, hasFocus ->
binding.appBar.setExpanded(!hasFocus)
isSelected = hasFocus
if (hasFocus) {
params.marginStart = originalMargin + 150 // arbitrary constant
} else {
params.marginStart = originalMargin
}
view.layoutParams = params
}
}
}
}
It look like this
Note I don't want to use xml to set text view inside toolbar. I want to do it programmatically. Inside my github project I used xml text view.
Expected output
when screen opens i want like this arrow + title
when collapse it look like this
Related
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)
}
Hi I am working on showing a view (publisher ad) below recycler view. Recycler view has some empty views to show publisher ad when it comes to related index. Currently I can click on any recycler view item and I am able to see my ad, but I cannot click on ad. How to achieve this? Here is my layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/parallax_ad_unit_id"/>
<widgets.AppSwipeRefreshLayout
android:id="#+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<widgets.AppRecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
</widgets.AppSwipeRefreshLayout>
</FrameLayout>
As you are using FrameLayout the PublisherAdView will be overlapped by AppRecyclerView or AppSwipeRefreshLayout, so when you click on item, that click event will be listened by `AppRecyclerView1 every time.
Try after changing the position of your RecyclerView and AdView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<widgets.AppSwipeRefreshLayout
android:id="#+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<widgets.AppRecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
</widgets.AppSwipeRefreshLayout>
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/parallax_ad_unit_id"/>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<widgets.AppSwipeRefreshLayout
android:id="#+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<widgets.AppRecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:nestedScrollingEnabled="false"
android:scrollbars="vertical"
ads:layout_constraintBottom_toBottomOf="parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintTop_toBottomOf="#id/ad_view" />
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="#+id/ad_view"
android:layout_width="match_parent"
android:layout_height="0dp"
ads:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</widgets.AppSwipeRefreshLayout>
Use a RelativeLayout for activity_main.xml which allows to place views
on top of others (in Z axis).
ex: SwipeRefreshLayout is the top most view here.
<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:focusable="true"
tools:context=".MainActivity">
<View
android:id="#+id/ad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_posts"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_posts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
Create two item layouts for two types of recycler view types
ex:
item_normal.xml, item_transparent.xml (where layout background is transparent that allow to see the layout below the surface area)
Setting android:clickable=false in item_transparent does not stop triggering the click event on transparent item, so use communication flow using interfaces, to bring the other view(ad) to the front when clicked on transparent item.
MainActivity.kt
class MainActivity : AppCompatActivity(), RvAdpater.OnItemClick {
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
private lateinit var adView: View
private lateinit var rvPosts: RecyclerView
override fun onClick() {
bringAdFront()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupRecyclerView()
setupSwipeRefreshLayout()
}
private fun setupRecyclerView() {
rvPosts = findViewById(R.id.rv_posts)
rvPosts.layoutManager = LinearLayoutManager(this)
val rvAdpater = RvAdpater()
rvAdpater.setListener(this)
rvPosts.adapter = rvAdpater
}
private fun setupSwipeRefreshLayout() {
swipeRefreshLayout = findViewById(R.id.swipe_refresh_posts)
swipeRefreshLayout.setOnRefreshListener {
Log.d(TAG, "Refreshing...")
}
}
private fun bringAdFront() {
adView = findViewById<View>(R.id.ad)
adView.bringToFront()
}
// to go back to the normal recycler view(which is inside swipe refresh layout)
// when back button is pressed
override fun onBackPressed() {
val parent = swipeRefreshLayout.parent as ViewGroup
parent.removeAllViews()
parent.addView(adView, 0)
// at last add swipe refresh layout which is the container of the recyclerview
parent.addView(swipeRefreshLayout, 1)
}
}
RvAdapter.kt
const val TAG = "RvAdpater"
class RvAdpater : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private lateinit var listener:OnItemClick
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val viewNormal = LayoutInflater.from(parent.context).inflate(R.layout.item_normal, parent, false)
val viewTransparent = LayoutInflater.from(parent.context).inflate(R.layout.item_transparent, parent, false)
return when(viewType){
0 -> NormalViewHolder(viewNormal)
2 -> TransparentViewHolder(viewTransparent)
else -> NormalViewHolder(viewNormal)
}
}
override fun getItemCount(): Int = 10
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(getItemViewType(position)){
0 -> {
val normalHolder = holder as NormalViewHolder
normalHolder.tv.text = "Post"
normalHolder.itemView.setOnClickListener {
Log.d(TAG, "Clicked on Normal item")
}
}
2 -> {
val transparentHolder = holder as TransparentViewHolder
transparentHolder.itemView.setOnClickListener {
listener.onClick()
}
}
}
}
fun setListener(onItem:OnItemClick){
listener = onItem
}
interface OnItemClick{
fun onClick()
}
override fun getItemViewType(position: Int): Int = position % 2 * 2
class NormalViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val tv:TextView = itemView.findViewById(R.id.tv_post)
}
class TransparentViewHolder(itemView: View): RecyclerView.ViewHolder(itemView)
}
Checkout branch: with-swipe-layout
I am trying to use a BottomAppBar, but when I try to show the NavigationView it does not work. I have read about showing NavigationView in BottomAppBar and there are 3 main steps:
1- setSupportActionBar() - In the Activity/Oncreate method
2- set up your menu in onCreateOptionsMenu
3- catch the listener and show the menu in onOptionsItemSelected(item: MenuItem?)
Here is my Activity Code:
class HomeActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(bottom_app_bar)
}
override fun onClick(view: View?) {
when(view!!.id){
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.navigation_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item!!.itemId) {
android.R.id.home -> {
val bottomNavDrawerFragment = BottomNavigationDrawerFragment()
bottomNavDrawerFragment.show(supportFragmentManager, bottomNavDrawerFragment.tag)
}
}
return true
}
}
And Fragment menu code:
class BottomNavigationDrawerFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bottom_navigation_drawer, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
navigation_view.setNavigationItemSelectedListener { menuItem ->
// Bottom Navigation Drawer menu item clicks
when (menuItem.itemId) {
// R.id.nav1 -> context!!.toast(getString(R.string.nav1_clicked))
}
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
true
}
close_imageview.setOnClickListener {
this.dismiss()
}
disableNavigationViewScrollbars(navigation_view)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet!!)
bottomSheetBehavior.setBottomSheetCallback(object: BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
if (slideOffset > 0.5) {
close_imageview.visibility = View.VISIBLE
} else {
close_imageview.visibility = View.GONE
}
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
}
}
})
}
return dialog
}
private fun disableNavigationViewScrollbars(navigationView: NavigationView?) {
val navigationMenuView = navigationView?.getChildAt(0) as NavigationMenuView
navigationMenuView.isVerticalScrollBarEnabled = false
}
}
Anybody have any idea of what's wrong?
I just change the order in the xml and it works.
activity_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/content_main" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/colorPrimary"
app:fabAlignmentMode="center"
app:navigationIcon="#drawable/ic_menu"
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_anadir"
app:tint="#null"
app:layout_anchor="#id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml:
<RelativeLayout
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="match_parent"
android:background="#android:color/background_light"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:background="#drawable/shape_coordinator"
>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/nestedScrollView"
tools:ignore="PrivateResource"
app:layout_behavior="">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/coordinatorLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
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>
I have a BaseActivity which gets inherited from all the other child activities and in that BaseActivity I set the toolbar:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getLayoutId())
if (toolbar != null) {
setSupportActionBar(toolbar)
supportActionBar?.apply {
setDisplayShowTitleEnabled(getActivityTitle() != null)
setDisplayHomeAsUpEnabled(getActivityTitle() != null)
getActivityTitle()?.let {
this.title = getActivityTitle()
}
}
}
}
open fun getActivityTitle(): String? = null
And on the ChildActitivy I have the following code:
override fun getActivityTitle(): String? {
return getString(R.string.gen_everyone_is_watching)
}
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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">
<ImageView
android:id="#+id/toolbar_logo"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
The problem is that I can not display the title when I want to do that in a child activity and the only way I have found to display the title is to not set the toolbar on the BaseActivity. Have you encountered anything similar?