Patrick J. Patrick J. - 4 months ago 45
Android Question

Clickable Material Design Icons

I have finished placing three material design icons in my toolbar and was wondering how to be able to click them and have intents.
Here is the activity class code:
package com.example.patri.index01;

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

public class Lines extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lines);

Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle(null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


The layout.xml:

android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Lines">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#000000">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LINES"
android:textColor="#ffffff"
android:textSize="25sp"
android:layout_gravity="center"
android:id="@+id/toolbar_title" />


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



The menu.xml/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"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Lines">

<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search_black_24dp"
android:title=""
app:showAsAction="always" />
<item
android:id="@+id/action_add"
android:icon="@drawable/ic_add_black_24dp"
android:title=""
app:showAsAction="always" />
<item
android:id="@+id/action_wheel"
android:icon="@drawable/ic_settings_black_24dp"
android:title=""
app:showAsAction="always" />



</menu>


Thanks in advance.
EDIT

public class Lines extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lines);

Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle(null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
if (i == R.id.action_search) {
Intent s = new Intent(this, SearchActivity.class);
startActivity(s);
return true;
}

if(i == R.id.action_add)

return super.onOptionsItemSelected(item);
}
}

Answer
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int i = item.getItemId();
        switch (i) {
            case R.id.action_search:
                //do your stuff here
                return true;



            case R.id.action_add:
                //do your stuff here
                return true;


            case R.id.action_wheel:
                //do your stuff here
                return true;


        }
        return super.onOptionsItemSelected(item);
    }

Override this function and perform your actions based on "i".