Toolbar Android

Toolbar contiene icone o pulsanti per eseguire azioni che forniscono un accesso rapido alle funzioni di un applicazione. Il widget si trova generalmente nella parte superiore dello schermo.
In questo esempio andremo ad impostare il metodo setSupportActionBar() per sostituire ActionBar con Toolbar.

themes.xml
    <style name=”Theme.MyApplication” parent=”Theme.MaterialComponents.DayNight.NoActionBar“>

activity_main.xml
<?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.AppBarLayout
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”>

<com.google.android.material.appbar.MaterialToolbar
        android:layout_width=”wrap_content”
        android:layout_height=”?attr/actionBarSize”
android:id=”@+id/app_bar”/>

</com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

example.xml
<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=”match_parent”
    android:orientation=”vertical”
android:background=”#CD30C8″>

<Button

android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
android:layout_marginTop=”10dp”
android:id=”@+id/button”
android:text=”Ext”/>
   

</LinearLayout>

MainActivity.java
package com.example.toolbar;

import android.content.Intent;
import android.view.View;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

Toolbar toolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

toolbar.setNavigationOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
startActivity(new Intent(getApplicationContext(),Example.class));
finish();
}
});
}
}

Example.java
package com.example.toolbar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Example extends Activity {


@Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.example);

Button btn;
btn =findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void  onClick(View view){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}

});
}
}

Manifest.xml
<activity
android:name=”.Example”>

</activity>