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?

1
  • 1
    Meaningful comments for down voting please! Commented May 4, 2017 at 9:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.