0

I am very new when it comes to android app development and android studio. I have been searching around for some couple days but still havent found the solution.

How can i have different toolbar Items on different activity's is their an way to do that ? Do you have an example so i could take a look at that? Do you know an tutorial that has that in it?

already much thanks for the help.

MainActivity.java

package com.example.stage.absa;

import android.Manifest;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AbsRuntimePermission {

    private ZXingScannerView scannerView;
    private static final int REQUEST_PERMISSION = 10;
    Toolbar toolbar;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        requestAppPermissions(new String[]{
                        Manifest.permission.CAMERA},
                R.string.msg, REQUEST_PERMISSION);
    }

    @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, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        if (id == R.id.action_settings) {
            setContentView(R.layout.activity_settings);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void browser1(View view) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
        startActivity(browserIntent);
    }

    public void scanCode(View view) {
        scannerView = new ZXingScannerView(this);
        scannerView.setResultHandler(new ZxingScannerResultHandler());

        setContentView(scannerView);
        scannerView.startCamera();
    }

    class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
        @Override
        public void handleResult(Result result) {
            String resultCode = result.getText();
            Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();

            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            scannerView.stopCamera();
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode) {
        //Do anything when permisson granted
        Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
    }
}

activity_main.xml

<?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="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/white"
    android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>

<!-- Layout for content is here. This can be a RelativeLayout  -->
    <RelativeLayout 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="com.example.stage.absa.MainActivity">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="browser1"
            android:layout_margin="2dp"
            android:src="@drawable/suppliance" />

        <Button
            android:id="@+id/S"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:onClick="scanCode"
            android:layout_margin="5dp"
            android:backgroundTint="@color/colorPrimary"
            android:text="Scan" />
    </RelativeLayout>

</LinearLayout>

SettingsActivity.java

   package com.example.stage.absa;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;


    public class SettingsActivity extends AppCompatActivity {
        Toolbar toolbar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
        }

        @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_settings, menu);
            return true;
        }
    }

activity_settings.xml

<?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="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        app:titleTextColor="@android:color/white"
        android:layout_height="?attr/actionBarSize">

        <ImageView
            android:id="@+id/tv_header_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginRight="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Name"
            android:textColor="#ffffff"
            android:gravity="center"
            android:id="@+id/brand_name" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="vertical"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="C Name :"
                android:layout_gravity="right"
                android:layout_marginRight="20dp"
                android:textColor="#ffffff"
                android:id="@+id/t1" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="User Name :"
                android:layout_gravity="right"
                android:layout_marginRight="20dp"
                android:textColor="#ffffff"
                android:id="@+id/t2" />

        </LinearLayout>



    </android.support.v7.widget.Toolbar>

    <!-- Layout for content is here. This can be a RelativeLayout  -->
    <RelativeLayout 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="com.example.stage.absa.MainActivity">


        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Settings" />
    </RelativeLayout>

</LinearLayout>

menu_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_back"
        android:title="Back"
        app:showAsAction="ifRoom" />
</menu>

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:title="Settings"
        app:showAsAction="ifRoom" />
</menu>
Merijn
  • 21
  • 8
  • Possible duplicate of [Creating a button in Android Toolbar](http://stackoverflow.com/questions/31231609/creating-a-button-in-android-toolbar) – Lepidopteron May 19 '17 at 12:20

2 Answers2

0
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu1, menu);
    return true;
}

Just pass different menu resources for the different activities ie R.id.menu1 or R.menu.menu2.

Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40
  • i have tried that already before but i doesnt use the menu/menu_settings.xml i want but it uses the layout/activity_settings.xml what could i possibly be doing wrong ? – Merijn May 19 '17 at 12:16
  • what are your Activity's theme? – Veneet Reddy May 19 '17 at 12:58
  • that is my styles.xml file – Merijn May 19 '17 at 13:00
  • that is what you ment or ? – Merijn May 19 '17 at 13:38
0

You can set custom toolbar layout for any activity like.

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                app:titleTextColor="@android:color/white"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <ImageView
                    android:id="@+id/tv_header_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_marginRight="20dp"
                    android:src="@drawable/siyaram_logo"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" Name"
                    android:textColor="#ffffff"
                    android:gravity="center"
                    android:id="@+id/brand_name" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:orientation="vertical"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="C Name :"
                        android:layout_gravity="right"
                        android:layout_marginRight="20dp"
                        android:textColor="#ffffff"
                        android:id="@+id/t1" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="User Name :"
                        android:layout_gravity="right"
                        android:layout_marginRight="20dp"
                        android:textColor="#ffffff"
                        android:id="@+id/t2" />

                </LinearLayout>



            </android.support.v7.widget.Toolbar>
vinod
  • 1,126
  • 13
  • 18