Header Ads

ad728
  • Breaking News

    Android Button Example


    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 {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button btn =(Button)findViewById(R.id.button);
            btn.setOnClickListener(new View.OnClickListener() {
               
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,"Button clicked",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    activity_maain.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"
            android:id="@+id/button"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="126dp" />
    </RelativeLayout>
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728