Android Button

Android Development

Android Button :-


⇛ layout > .xml file :

<?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"
android:padding="5dp"
tools:context=".First">

<!--Default Button-->

<Button
android:id="@+id/Idbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="Default Button"
android:textAlignment="center"
android:textColor="#0d0d0d"
android:textSize="15sp"
android:textStyle="bold" />

<!--Default Button-->

<Button
android:id="@+id/Idbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/custom_button"
android:padding="10dp"
android:text="Custom Button"
android:textAlignment="center"
android:textColor="#0d0d0d"
android:textSize="15sp"
android:textStyle="bold" />

<!--Drawable Icon Button-->

<Button
android:id="@+id/Idbutton3"
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/image_button"
android:padding="10dp"
android:textAlignment="center"
android:textColor="#e6074e"
android:textSize="15sp"
android:textStyle="bold" />

<!--Its a best way to made your custom button. ok-->

<Button
android:id="@+id/Idbutton4"
android:layout_width="270dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:background="@drawable/custom_button_2"
android:shadowColor="#A6A7A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Your choice"
android:textColor="#FFFFFF"
android:textSize="30sp" />

<Button

android:id="@+id/Idbutton5"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:background="@drawable/custom_button_3"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="D"
android:textColor="#000000"
android:textSize="30sp" />

</LinearLayout>

⇛ drawable > custom_button.xml file :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke

android:width="18dp"
android:color="#30ca09"/>

<corners android:radius="90dp"/>
<padding

android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp"/>

<solid android:color="#30ca09"/>
</shape>

⇛ drawable > custom_button_2.xml file :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle" >
<corners

android:radius="14dp"/>
<gradient

android:angle="45"
android:centerX="35%"
android:centerColor="#e70ad8"
android:startColor="#f9f506"
android:endColor="#6508f2"
android:type="linear"/>

<padding

android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>

<size

android:width="270dp"
android:height="60dp"/>

<stroke

android:width="3dp"
android:color="#f50817"/>

</shape>

⇛ drawable > custom_button_3.xml file :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle" >
<corners android:radius="360dp"/>
<gradient

android:angle="90"
android:centerX="35%"
android:centerColor="#47A891"
android:startColor="#E8E8E8"
android:endColor="#FFFFFF"
android:type="linear"/>

<padding

android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>

<size

android:width="90dp"
android:height="90dp"/>

<stroke

android:width="21dp"
android:color="#EFFF14"/>

</shape>

⇛ drawable > image_button.png file :


⇛ java file :

package com.example.dell.button;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class First extends AppCompatActivity {

private Button btn1, btn2, btn3, btn4, btn5;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

btn1 = (Button) findViewById(R.id.Idbutton1);
btn2 = (Button) findViewById(R.id.Idbutton2);

btn3 = (Button) findViewById(R.id.Idbutton3);
btn4 = (Button) findViewById(R.id.Idbutton4);

btn5 = (Button) findViewById(R.id.Idbutton5);
/*When click on any Button*/

btn1.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Default Button",Toast.LENGTH_SHORT).show();
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Custom Button",Toast.LENGTH_SHORT).show();
}
});

btn3.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Icon Button",Toast.LENGTH_SHORT).show();
}
});

btn4.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Your Choice",Toast.LENGTH_SHORT).show();
}
});

btn5.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Round Button Your Choice",Toast.LENGTH_SHORT).show();
}
});
}
}


No comments

Powered by Blogger.