Header Ads

ad728
  • Breaking News

    Working with Multiple Button Android Example

    Multiple Button Example
    activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.desktop.buttonexample.MainActivity">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 1"
            android:id="@+id/button1"
            android:layout_marginTop="53dp"
            android:layout_alignParentTop="true"
            android:layout_alignLeft="@+id/button2"
            android:layout_alignStart="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 2"
            android:onClick="clickme"
            android:id="@+id/button2"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 3"
            android:id="@+id/button3"
            android:layout_below="@+id/button2"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 4"
            android:id="@+id/button4"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button 5"
            android:id="@+id/button5"
            android:layout_below="@+id/button4"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

    MainActivity.java
    package com.example.desktop.buttonexample;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        Button btn1,btn2,btn3,btn4,btn5;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btn1 = (Button) findViewById(R.id.button1);
            btn2 = (Button) findViewById(R.id.button2);
            btn3 = (Button) findViewById(R.id.button3);
            btn4 = (Button) findViewById(R.id.button4);
            btn5 = (Button) findViewById(R.id.button5);
    
            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
            btn3.setOnClickListener(this);
            btn4.setOnClickListener(this);
            btn5.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.button1:
                    Toast.makeText(this,"Button 1 is working",Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.button2:
                    Toast.makeText(this,"Button 2 is working",Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.button3:
                    Toast.makeText(this,"Button 3 is working",Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.button4:
                    Toast.makeText(this,"Button 4 is working",Toast.LENGTH_SHORT)
                            .show();
                    break;
                case R.id.button5:
                    Toast.makeText(this,"Button 5 is working",Toast.LENGTH_SHORT)
                            .show();
                    break;
            }
        }
    }

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728