Tuesday, 25 August 2015

How to Create Button in Android Using This

activity_main.xml
--------------
<LinearLayout 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:orientation="vertical"     tools:context=".MainActivity">     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Button"         android:id="@+id/button"         android:layout_gravity="center"         android:layout_marginTop="45dp"          /> </LinearLayout>
MainActivity.Java
----------------

package com.anil.android.anilapp;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    Button b1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        b1=(Button)findViewById(R.id.button);

        b1.setOnClickListener(this);

    }





    @Override

    public void onClick(View v) {

        Toast.makeText(getApplicationContext(),"Button is Working",Toast.LENGTH_LONG).show();

        

    }

}



Output 
-----------








No comments:

Post a Comment