Showing posts with label multiple button events in android. Show all posts
Showing posts with label multiple button events in android. Show all posts

Wednesday, December 27, 2017

Multiple Button Events in Android

While learning Android programming one of the problem that I encounter is how to assign multiple buttons in an activity and event when I click a particular button. Here is the solution that I wrote I called this application multiple button events in Android. I hope you will find my work useful.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.









Sample Program Output



Program Listing

MainActivity.java

package com.example.jacob.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements        View.OnClickListener {

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


        Button one = (Button) findViewById(R.id.button);
        one.setOnClickListener(this);
        Button two = (Button) findViewById(R.id.button2);
        two.setOnClickListener(this);
        Button three = (Button) findViewById(R.id.button3);
        three.setOnClickListener(this);
        Button four = (Button) findViewById(R.id.button4);
        four.setOnClickListener(this);
        Button five = (Button) findViewById(R.id.button5);
        five.setOnClickListener(this);
    }
        @Override
        public void onClick (View v){

            switch (v.getId()) {

                case R.id.button:
                    Toast.makeText(MainActivity.this, "You have clicked Button 1", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button2:
                    Toast.makeText(MainActivity.this, "You have clicked Button 2", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button3:
                    Toast.makeText(MainActivity.this, "You have clicked Button 3", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button4:
                    Toast.makeText(MainActivity.this, "You have clicked Button 4", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button5:
                    Toast.makeText(MainActivity.this, "You have clicked Button 5", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }



        }

    }


activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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.jacob.myapplication.MainActivity">

    <RelativeLayout        android:layout_width="440dp"        android:layout_height="587dp"        tools:layout_editor_absoluteX="16dp"        tools:layout_editor_absoluteY="0dp">

        <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentStart="true"            android:layout_alignParentTop="true"            android:layout_marginStart="80dp"            android:layout_marginTop="44dp"            android:text="Button 1" />

        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button"            android:layout_below="@+id/button"            android:layout_marginTop="61dp"            android:text="Button 2" />

        <Button            android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button2"            android:layout_below="@+id/button2"            android:layout_marginTop="55dp"            android:text="Button 3" />

        <Button            android:id="@+id/button4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button3"            android:layout_below="@+id/button3"            android:layout_marginTop="57dp"            android:text="Button 4"            tools:layout_editor_absoluteX="67dp"            tools:layout_editor_absoluteY="0dp" />

        <Button            android:id="@+id/button5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignEnd="@+id/button4"            android:layout_below="@+id/button4"            android:layout_marginTop="44dp"            android:text="Button 5" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>


strings.xml

<resources>
    <string name="app_name">Multiple Buttons in Android</string>
</resources>




DOWNLOAD SOURCE CODE HERE