1

I want to create 3 fragment like below images:

Fragment with toolbar&statusbar

Transparent toolbar&statusbar

Transparent toolbar not statusbar

The layout of fragment is below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorYellow"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="4dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"/>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:id="@+id/fragment_container_layout">
    </FrameLayout>

</RelativeLayout>

My themes are below:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="MyTranparentTheme" parent="AppTheme">
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style> </resources>

I set theme of application in AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

Fragment constructor signature is here:

new BlankFragment(isTranslucentHeaderbar, isTranslucentStatusbar)

How can I create these fragments?

I create non-translucent toolbar&statusbar is here:

enter image description here

But I couldn't create fragment with translucent toolbar. My onCreateView code is below:

View view = inflater.inflate(R.layout.fragment_sensor_light, container, false);

Toolbar toolbar = view.findViewById(R.id.toolbar);

toolbar.setTitle("Fragment toolbar");
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.translucentColor)));
return view;

If I set theme as MyTransparentTheme in manifest file, ss is below:

enter image description here

us2956
  • 476
  • 12
  • 27
  • Are you returning the view in onCreateView() ? – AIK Sep 10 '18 at 11:28
  • yes, of course :) I just edited my onCreateView code @AIK – us2956 Sep 10 '18 at 11:29
  • Apply the theme in `AndroidManifest.xml` like this `android:theme="@style/MyTranparentTheme"` And see what happens. Also, you are using `Theme.AppCompat.Light.DarkActionBar` and you have `Toolbar` in your layout. Maybe checking this answer: https://stackoverflow.com/a/41659927/4409113 – ʍѳђઽ૯ท Sep 10 '18 at 11:32
  • Possible duplicate of [How to add Toolbar to a fragment in android?](https://stackoverflow.com/questions/41091438/how-to-add-toolbar-to-a-fragment-in-android) – Adam Ostrožlík Sep 10 '18 at 11:33
  • 1
    @ʍѳђઽ૯ท I just added a new image for MyTranparentTheme. – us2956 Sep 10 '18 at 11:35
  • 1
    @AdamOstrožlík This question is different. It is not related to my problem. – us2956 Sep 10 '18 at 11:37
  • Yes! Translucent works now. Remove `android:background="@color/colorPrimary"` or change `Theme.AppCompat.Light.DarkActionBar` to `Theme.AppCompat.Light.NoActionBar` or remove whole `Toolbar` then use only `Theme.AppCompat.Light.DarkActionBar`.Check this answer: https://stackoverflow.com/a/41659927/4409113 – ʍѳђઽ૯ท Sep 10 '18 at 11:37
  • I changed theme parent as ` – us2956 Sep 10 '18 at 11:43

0 Answers0