Tuesday, 25 August 2015

Working with Button(Anonymous Inner Type)

activity_main.xml
-----------------
<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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin" 

    tools:context=".MainActivity">



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button"

        android:id="@+id/button"

        android:layout_marginTop="145dp"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>



MainActivity,java
---------------------
package com.anil.android.buttonusingnewonclicklistner;



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 {

    Button btn;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



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

        btn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

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

            }

        });

    }



}


Output is:
------------





No comments:

Post a Comment