Create Custom app bar in android studio java

 Create Custom app bar in android studio java

Demo


custom_action_bar.xml in res/layout

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    android:layout_width = "match_parent"
    android:layout_height = "56dp"
    android:gravity = "center_vertical"
    android:elevation="8dp"
    android:weightSum = "15">
    <LinearLayout
        android:id="@+id/app_bar_location"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"

        android:layout_weight = "7"
        android:gravity = "start">
        <ImageView
            android:layout_width = "wrap_content"
            android:layout_height = "match_parent"
            android:paddingEnd="10dp"
            android:src = "@drawable/ic_edit_location_black"
            app:tint="@color/black" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:text = "Location"
                android:layout_gravity="center"
                android:textSize = "16sp"
                android:textColor = "#000"
                />
            <TextView
                android:id = "@+id/app_bar_location_name"
                android:layout_width = "match_parent"
                android:layout_height = "wrap_content"
                android:maxLength="16"
                android:text = "Varanasi India"
                android:layout_gravity="center"
                android:maxLines="1"
                android:textSize = "16sp"
                android:textColor = "#2C2C2C"
                android:textStyle = "bold"
                />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_weight = "8"
        android:gravity="end"
        android:paddingEnd="10dp"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal">
        <ImageView
            android:id="@+id/menu_icon"
            android:layout_gravity="end"
            android:layout_width = "match_parent"
            android:layout_height = "match_parent"
            android:src = "@drawable/ic__menu_black"
            app:tint="@color/black" />
    </LinearLayout>
    </LinearLayout>
</LinearLayout>


activity_main.xml

<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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


MainActivity.java


import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView locationName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.custom_action_bar);
        //getSupportActionBar().setElevation(0);
        View view = getSupportActionBar().getCustomView();
        locationName = findViewById(R.id.app_bar_location_name);
        LinearLayout linearLayout = view.findViewById(R.id.app_bar_location);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You have Location", Toast.LENGTH_LONG).show();
                locationName.setText("Chandauli");
            }
        });
        ImageView menuIcon = view.findViewById(R.id.menu_icon);
        menuIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You have clicked menu icon", Toast.LENGTH_LONG).show();
            }
        });

    }
}


Post a Comment

0 Comments