0

I have a layout toolbar which I use in every pages using in xml. There are some buttons in this layout and I want to create a class which takes care of the button clicks of the buttons in toolbar.xml . Is this possible? If yes how?

This is my code of the xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="56dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:layout_above="@+id/relativeLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#ffa500"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        android:visibility="visible" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_alignBottom="@+id/toolbar"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:background="@drawable/round_edges"
        android:ems="10"
        android:hint="Search"
        android:imeOptions="actionSearch"
        android:inputType="textPersonName"
        android:paddingLeft="10dp"
        android:visibility="invisible"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>

</RelativeLayout>

<ImageButton
    android:id="@+id/button2"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_toLeftOf="@+id/button1"
    android:layout_toStartOf="@+id/button1"
    android:background="@android:color/transparent"
    android:src="@mipmap/cart" />

<ImageButton
    android:id="@+id/button1"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent"
    android:src="@mipmap/contact" />

<ImageButton
    android:id="@+id/button3"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_toLeftOf="@+id/button2"
    android:layout_toStartOf="@+id/button2"
    android:background="@android:color/transparent"
    android:src="@mipmap/search" />

<ImageButton
    android:id="@+id/button4"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="9dp"
    android:layout_marginRight="9dp"
    android:layout_toLeftOf="@+id/button3"
    android:layout_toStartOf="@+id/button3"
    android:background="@android:color/transparent"
    android:src="@mipmap/phone" />

<ImageButton
    android:id="@+id/button0"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent"
    android:src="@mipmap/ic_launcher" />
</RelativeLayout>
Ciddarth Raaj
  • 55
  • 1
  • 6

1 Answers1

0

Of course you can, but for every action you must pass the context from the activity/fragment which are calling that action.

Example of class for handling editText and button1 from your layout(other elements you can add yourself)

public class TestClass {


    public void getEditText(Context context, View view){

        EditText et = (EditText) view.findViewById(R.id.editText);

        Toast.makeText(context,"Here I handle editText!",Toast.LENGTH_SHORT).show();

    }


    public void setImageButton(final Context context, View view){

        ImageButton imageButton = (ImageButton) view.findViewById(R.id.button1);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // do something
                Toast.makeText(context,"button1 clicked!",Toast.LENGTH_SHORT).show();

            }
        });

    }

}

You call this methods from your activity like this:

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

    TestClass tc = new TestClass();
    tc.getEditText(this,findViewById(android.R.id.content));
    tc.setImageButton(this,findViewById(android.R.id.content));

}
tompadre
  • 797
  • 7
  • 22
  • How to use intent in this class?? @tompadre – Ciddarth Raaj May 24 '17 at 11:33
  • Why would you use intent? I thought whole idea of separate class was to use getters and setters? I TestCLass you can make method for everything that you want https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work – tompadre May 24 '17 at 12:04
  • when I click a button I want it to go to another page ,I have to use intent for this,Is there a way to do this or should I have to copy and paste the code for every individual page. – Ciddarth Raaj May 24 '17 at 12:09
  • Then like I wrote in the first sentence, pass the context and make intent like i did in the methods above in the answer and start activity like you would normally do `context.startActivity(new Intent(context,MainActivity.class));` – tompadre May 24 '17 at 12:24
  • Just put `context.startActivity(new Intent(context,MainActivity.class));` under the toast and instead MainActivity write your activity and it'll work. I have tested it. – tompadre May 25 '17 at 12:31