Android Developer Trainee Android Developer Trainee - 3 months ago 11
Android Question

Android : Toolbar visible in layout but cant see in emulator?

Here is the image of what i need to create.I want to make a transparent toolbar in my Android application. I have placed a toolbar in my XML code and it is seeing in the design window. When i run the program the toolbar is not visible in the emulator.

Here is my XML code...

<?xml version="1.0" encoding="utf-8"?>
<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:padding="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jithin.image_custom.MainActivity">

<android.support.v7.widget.Toolbar android:id="@+id/my_toolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/transparent"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="fitXY"
android:src="@drawable/album1"
android:id="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="435dp"
android:layout_below="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

Answer

You can use CollapsingToolbarLayout

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginEnd="64dp"
    app:expandedTitleMarginStart="48dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>
            <ImageView
                android:src="@drawable/cheese_1"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"/>

</android.support.design.widget.CollapsingToolbarLayout>

Checkout this link for further customisations & doubts

Comments