info@thistimebd.com

Sunday 24th of November 06:16:51am

Android Button source code

MainActivity.java

---------------------

package com.thistimebd.web.myapplication2;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {



    private Button loginButton, logoutButton;

    private TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        loginButton= (Button) findViewById(R.id.loginButtonId);

        logoutButton= (Button)findViewById(R.id.logoutButtonId);

        textView=(TextView) findViewById(R.id.texViewId);


        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {



                textView.setText("Login button is Clicked");

            }

        });


        logoutButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {


                textView.setText("Logout button is Clicked");


            }

        });

    }

}

-------------

another java code

MainActivity.java


private Button loginButton, logoutButton;

    private TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        loginButton= (Button) findViewById(R.id.loginButtonId);

        logoutButton= (Button)findViewById(R.id.logoutButtonId);

        textView=(TextView) findViewById(R.id.texViewId);


        loginButton.setOnClickListener(this);

        logoutButton.setOnClickListener(this);



    }


    @Override

    public void onClick(View v) {


        if(v.getId()==R.id.loginButtonId){


            textView.setText("Login Button is Clicked");


        }


        if(v.getId()==R.id.logoutButtonId){

            textView.setText("Logout Button is Clicked");



        }




    }

}

-------------------


package com.thistimebd.web.myapplication2;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {



    private Button loginButton, logoutButton;

    private TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        loginButton = (Button) findViewById(R.id.loginButtonId);

        logoutButton = (Button) findViewById(R.id.logoutButtonId);

        textView = (TextView) findViewById(R.id.texViewId);


        Handler handler = new Handler();

        loginButton.setOnClickListener(handler);

        logoutButton.setOnClickListener(handler);



    }


    class Handler implements View.OnClickListener {



        @Override

        public void onClick(View v) {

            if (v.getId() == R.id.loginButtonId) {

                textView.setText("Login button is clicked");



            } else if (v.getId() == R.id.logoutButtonId) {

                textView.setText("Logout button is clicked");



            }

        }



    }


}



------------------

<resources>

    <string name="app_name">My Application2</string>

    <string name="login_button_name">Login</string>

    <string name="logout_button_name">Logout</string>

</resources>

--------------


<?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"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center"

    android:orientation="vertical"

    tools:context="com.thistimebd.web.myapplication2.MainActivity">


    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">


        <Button

            android:id="@+id/loginButtonId"

            android:textAllCaps="false"

            android:layout_margin="5dp"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/login_button_name"

            android:drawableRight="@drawable/login"

            />


        <Button

            android:id="@+id/logoutButtonId"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/logout_button_name"

            android:layout_margin="10dp"

            android:textAllCaps="false"

            android:drawableLeft="@drawable/logout"


            />



    </LinearLayout>




    <TextView

        android:id="@+id/texViewId"

        android:layout_margin="20dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="20sp"

        />


</LinearLayout>

---------------------------------------------------


acivitymain

android:onClick="showMessage"

-----------------------

mainactivity.java


private Button loginButton, logoutButton;

    private TextView textView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        loginButton = (Button) findViewById(R.id.loginButtonId);

        logoutButton = (Button) findViewById(R.id.logoutButtonId);

        textView = (TextView) findViewById(R.id.texViewId);




    }


    public void showMessage(View v){

        if(v.getId()==R.id.loginButtonId){

            textView.setText("Login button is clicked");


        }



        if(v.getId()==R.id.logoutButtonId){

            textView.setText("Logout button is clicked");


        }



    }


}