skip to Main Content

Look the image:
image: enter image description here

I don’t know what code this is caused by. That’s why I will only share themes.xml codes.

//Themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.TitleBarSettings" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.TitleBarSettings" parent="Base.Theme.TitleBarSettings" />
</resources>

2

Answers


  1.  <style name="Base.Theme.TitleBarSettings" parent="Theme.Material3.DayNight.NoActionBar">
    

    You have set NoActionBar theme. Thats why it is not appearing.

    use this as your parent theme:

    parent="Theme.AppCompat.Light.DarkActionBar"
    

    It will have actionbar with the app name but it will not have 3 dotted menu on the right side.

    For changing the color of the ActionBar add following code in your onCreate of the mainActivity.

    // Define ActionBar object
            ActionBar actionBar;
            actionBar = getSupportActionBar();
    
            // Define ColorDrawable object and parse color
            // using parseColor method
            // with color hash code as its parameter
            ColorDrawable colorDrawable
                    = new ColorDrawable(Color.parseColor("#0F9D58"));
    
            // Set BackgroundDrawable
            actionBar.setBackgroundDrawable(colorDrawable);
    

    You can change the "#0F9D58" to any color code you want.

    Login or Signup to reply.
  2. there is another way to add a default toolbar to your app while setting your parent theme same as:

    parent="Theme.Material3.DayNight.NoActionBar"
    

    activity_main.xml

    by following this approach you don’t need to change background color from the java code but you can easily change background color as a simple attribute of toolbar.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/myToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#1F6C23"
            android:elevation="4dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity.class

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar myToolbar = (Toolbar) findViewById(R.id.myToolbar);
            setSupportActionBar(myToolbar);
        }
    }
    

    see the official documentation here :
    https://developer.android.com/develop/ui/views/components/appbar/setting-up

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search