Buttons in Android studio

A button can be given by selecting ‘Buttons’ option from the ‘Palette’ of the ‘Design’ menu. Following section has the code for a simple button in the android studio.

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Output:

A colored button can be made in Android studio. Following section shows code for a simple colored button.

<Button
android:id="@+id/button"
android:layout_width="183dp"
android:layout_height="65dp"
android:alpha="0.5"
android:background="#5B1DB5C3"
android:fontFamily="serif"
android:padding="10sp"
android:paddingLeft="10sp"
android:paddingTop="10sp"
android:paddingRight="10sp"
android:paddingBottom="10sp"
android:shadowColor="#75222020"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="5"
android:text="Button"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Output:

The following section shows the code for another colored button. This button has feature of text cursor color.

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:padding="8dp"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
android:text="Button"
android:textColor="@android:color/holo_red_dark"
android:textCursorDrawable="@android:color/black"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Output:

The following section shows code for a button which changes color on pressing. The background in the button has been given by Drawable Resource File created by right clicking on drawable folder. These files are in .xml file format.

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/custom_button_colored"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
custom_button_colored
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:state_pressed="true"
android:drawable="@drawable/custom_button_feature1"/>
<item
android:drawable="@drawable/custom_button_feature2"/>

</selector>
custom_button_feature1
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#A1C1CFC6"/>
<corners android:radius="3dp"/>
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp"/>

<stroke
android:width="2dp"
android:color="#8E7D7D"/>

</shape>
custom_button_feature2
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#741547C6"/>
<corners android:radius="2dp"/>

</shape>

Output:

The following section shows the code for a button which has also a shadow. The background in the button is given by ‘Drawable Resource File‘. ‘Drawable Resource File’ is created by right clicking on the drawable folder and then selecting the Drawable Resource File from new. The file is in .xml file format.

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/background_feature"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
background_feature
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:state_pressed="true"
android:drawable="@android:color/white"/>

<item
android:drawable="@android:color/holo_orange_light"/>

</selector>

output:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments