info@thistimebd.com

Sunday 24th of November 08:08:46am

Custom Button android app source code

activity_main.xml

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


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;


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);




    }


    public void showMessage(View v){

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

            Toast toast=Toast.makeText(MainActivity.this,"Login Button is clicked",Toast.LENGTH_SHORT);

            toast.setGravity(Gravity.CENTER,0,0);

            toast.show();




        }



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

            Toast toast=Toast.makeText(MainActivity.this,"Logout Button is clicked",Toast.LENGTH_SHORT);

            toast.setGravity(Gravity.CENTER,0,0);

            toast.show();





        }



    }


}

====================

string.xml

---------

<resources>

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

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

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

</resources>

==========================

activity_main.xml

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


<?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.bdstall.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:onClick="showMessage"

            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:onClick="showMessage"

            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>