How To Add Back Button With Option Menu In Title Bar android studio - Androidpro.in

 Step – 1 Add this Java code down to the main activity

ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
//
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

 

Step – 2 paste code up to last closing brasher in main activity

 
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_settings:
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                // To count with Play market backstack, After pressing back button,
                // to taken back to our application, we need to add following flags to intent.
                goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                        Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
                        Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException e) {
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://play.google.com/store/apps/details?id=in.app.androidpro.htmlcourse" + getPackageName())));
                }
 
//                Toast.makeText(getApplicationContext(), "Item 1 Selected", Toast.LENGTH_LONG).show();
                return true;
            case R.id.action_settings1:
                ApplicationInfo api = getApplicationContext().getApplicationInfo();
                String apkpath = api.sourceDir;
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("application/vnd.android.package-archive");
                intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(apkpath)));
                startActivity(Intent.createChooser(intent,"ShareVia"));
 
//                Toast.makeText(getApplicationContext(), "Item 2 Selected", Toast.LENGTH_LONG).show();
                return true;
            case R.id.action_settings2:
                Intent intent4 = new Intent(Intent.ACTION_VIEW);
                intent4.setData(Uri.parse("https://play.google.com/store/apps/developer?id=Androidpro"));
                startActivity(intent4);
//                Toast.makeText(getApplicationContext(), "Item 3 Selected", Toast.LENGTH_LONG).show();
                return true;
            case android.R.id.home:
                finish();
                return true;
 
            default:
                return super.onOptionsItemSelected(item);
        }}
 
 

 

Step 3 (Menu file name) menu_main_activity2.xml

 
<menu 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"
    tools:context="in.app.androidpro.htmlcourse.MainActivity2">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="Rate"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_settings1"
        android:orderInCategory="100"
        android:title="Share "
        app:showAsAction="never" />
    <item
        android:id="@+id/action_settings2"
        android:orderInCategory="100"
        android:title="More App"
        app:showAsAction="never" />
</menu>
 

 

Now your project is ready