How to create button and code below see example:
Activity_main.XML :
Activity_main.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: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="com.kiranapp.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b1"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b2"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignStart="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="b3"
android:id="@+id/button3"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/button2" /> </RelativeLayout>
MainActivity.java :
package com.kiranapp; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button b1,b2,b3;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button) findViewById(R.id.button); b2 = (Button) findViewById(R.id.button2); b3 = (Button) findViewById(R.id.button3); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); }
@Override
public void onClick(View view) { switch (view.getId()){ case R.id.button: Toast.makeText(MainActivity.this, "Button1 is working", Toast.LENGTH_SHORT).show(); break; case R.id.button2: Toast.makeText(MainActivity.this, "Button2 is working", Toast.LENGTH_SHORT).show(); break; case R.id.button3: Toast.makeText(MainActivity.this, "Button3 is working", Toast.LENGTH_SHORT).show(); break; } }
}
Output :
Android Button fig:1 |
Android Button Example fig:2 |
Android Button Example fig:3 |
No comments:
Post a Comment