Home > other >  how to make toolbar stays on top
how to make toolbar stays on top

Time:12-27

i have my toolbar set as transparent background, i want it to stay on top of image. i tried use Framelayout yet still same. how do i set my toolbar always on top? But I can't make my Activity be on top and be visible at the back of my ToolBar...

my Image

my xml

`<LinearLayout 
    <androidx.appcompat.widget.Toolbar
        android:id="@ id/tool_bar"
        android:background="@android:color/transparent"
        android:backgroundTint="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C I N E M A"
            android:textColor="@color/white"
            android:textSize="24sp"
            android:layout_gravity="center"
            android:id="@ id/toolbar_title" />
    </androidx.appcompat.widget.Toolbar>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <com.denzcoskun.imageslider.ImageSlider
            android:id="@ id/image_slider"
            android:layout_width="wrap_content"
            android:layout_height="500dp"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>`

CodePudding user response:

In the activity's onCreate() method, call the activity's setSupportActionBar() method, and pass the activity's toolbar. This method sets the toolbar as the app bar for the activity. For example:

For Kotlin;

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_my)
    // Note that the Toolbar defined in the layout has the id "my_toolbar"
    setSupportActionBar(findViewById(R.id.my_toolbar))
}

For Java;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);
}

and You can use elevation.

your toolbar wrap layout : android:elevation="4dp"

sample ;

<com.google.android.material.appbar.MaterialToolbar
     app:elevation="8dp"
     android:elevation="8dp"
     android:background="@color/white"/>
  • Related