1

I am trying to show toolbar along with the menu items in an activity. This works fine until I add databinding to the xml. I can not use setSupportActionBar(toolbar) at all after that. Here is my xml file activity_single_product.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <variable name="product" 
       type="com.sales.models.ProductModel"/>
    <variable name="glide" type="com.bumptech.glide.Glide" />
</data>
<RelativeLayout 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:id="@+id/activity_view_single_product"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sales.SingleProductActivity"
    android:background="@color/colorPrimary">
    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/tool"
            layout="@layout/toolbar" />

        <include
            android:id="@+id/search_layout"
            layout="@layout/search_layout" />
        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:background="@drawable/drop_shadow_toolbar" />
    </LinearLayout> <!-- Toolbar & Search bar-->
</RelativeLayout>
</layout>

The Activity :

private void initViews() {
    binding = DataBindingUtil.setContentView(this, R.layout.activity_single_product); // ActivitySingleProductBinding
    setSupportActionBar(binding.tool.toolbar);
    binding.setProduct(product);
}

I have followed stackoverflow answer. This didn't help. Can anyone suggest?

Community
  • 1
  • 1
Aveek
  • 849
  • 1
  • 12
  • 28

1 Answers1

1

I have found and fixed the problem. The problem was in toolbar layout. I included

<include
        android:id="@+id/tool"
        layout="@layout/toolbar" />

and the toolbar layout was

<android.support.v7.widget.Toolbar 
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:layout_marginTop="25dp"
    android:background="@null"
    android:titleTextColor="@color/lightGrey2"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:contentInsetLeft="10dp"
    android:contentInsetRight="10dp"
    android:contentInsetStart="10dp"
    android:padding="0dp"
    app:contentInsetLeft="10dp"
    app:contentInsetRight="10dp"
    app:contentInsetStart="10dp"/>

The problem was , I did not include proper tag to wrap the toolbar. Later I changed it and the new toolbar layout is

<layout 
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools">
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:layout_marginTop="25dp"
    android:background="@null"
    android:titleTextColor="@color/lightGrey2"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:contentInsetLeft="10dp"
    android:contentInsetRight="10dp"
    android:contentInsetStart="10dp"
    android:padding="0dp"
    app:contentInsetLeft="10dp"
    app:contentInsetRight="10dp"
    app:contentInsetStart="10dp"/></layout>

This solved the issue.

Aveek
  • 849
  • 1
  • 12
  • 28