Toolbar Example

Toolbar is main for application highlight their application name.

actvity_main.xml

This xml shows toolbar only in our layout

 






 

MainActivity.java

This activity shows toolbar with navigation buttons.

package com.example.codebridgetoolbarexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
        Toolbar toolbar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	   initToolBar();
	}
public void initToolBar() {
       toolbar = (Toolbar) findViewById(R.id.toolbar);
       toolbar.setTitle(R.string.toolbarTitle);
       setSupportActionBar(toolbar);
       toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow);
       toolbar.setNavigationOnClickListener(new View.OnClickListener() {
	  @Override
	  public void onClick(View v) {
	     Toast.makeText(AndroidToolbarExample.this, "clicking the toolbar!", Toast.LENGTH_SHORT).show();
	  }
       });
}
}