Skip to main content

Posts

How to change TimePickerDialog theme in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.os.Bundle; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout Button button = (Button) findViewById(R.id.button); button.setOnClickListener(v -> { // Initialize a new time picker dialog fragment DialogFragment dialogFragment = new TimePickerFragment(); // Show the time picker dialog fragment dialogFragment.show( getSupportFragmentManager(), ...

Android TimePickerDialog Example

MainActivity.java package com.cfsuman.androidtutorials; import android.os.Bundle; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout Button button = (Button) findViewById(R.id.button); button.setOnClickListener(v -> { // Initialize a new time picker dialog fragment DialogFragment dialogFragment = new TimePickerFragment(); // Show the time picker dialog fragment dialogFragment.show( getSupportFragmentManager(), ...

How to set ImageButton pressed state background in Android

activity_main.xml <androidx.constraintlayout.widget.ConstraintLayout 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:background="#F4F0EC" android:padding="16dp" tools:context=".MainActivity"> <!-- ImageButton with default click effect/pressed state --> <ImageButton android:id="@+id/ib" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="64dp" android:src="@drawable/share_transparent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constra...

How to add a border to an ImageButton in Android

activity_main.xml <androidx.constraintlayout.widget.ConstraintLayout 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:background="#F4F0EC" android:padding="16dp" tools:context=".MainActivity"> <!-- ImageButton with default border, no border --> <ImageButton android:id="@+id/ib" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="64dp" android:src="@drawable/share_solid" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf=...

How to use ImageButton different image ScaleType in Android

activity_main.xml <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="24dp" tools:context=".MainActivity" android:background="#F8F8FF"> <!-- ImageButton with default size, no scaling --> <ImageButton android:id="@+id/ibDefault" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:src="@drawable/share_solid" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_to...

How to resize/scale an ImageButton in Android

Resize/Scale an ImageButton ImageButton displays an image on a regular Button instead text. As a regular Button widget users can click on the ImageButton and it acts the same as a regular Button. Sometimes android developers want to show an image to a Button instead of using a regular Button widget. Then it looks awesome. The image of the ImageButton surface can be defined by the src attribute in the XML layout file or by the setImageResource method in the java or kotlin script file. But the problem is when we use an image in the ImageButton widget the original image may be bigger than the expected size. In this situation, we have to resize/scale it. In this android app development tutorial, we will demonstrate how can an app developer scale their image in an ImageButton widget. We can scale down or scale up our image by using the ImageButton width and height attribute in the XML layout file. So easy, but the actual problem arrives when the scaled image not showing as expected inside t...

How to add padding to an ImageButton in Android

activity_main.xml <androidx.constraintlayout.widget.ConstraintLayout 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:background="#F8F8FF" android:padding="16dp" tools:context=".MainActivity"> <!-- ImageButton no padding by XML --> <ImageButton android:id="@+id/ib" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:padding="0dp" android:scaleType="fitCenter" android:src="@drawable/share_stroke" app:layout_constraintEnd_toEndOf="parent" app:layout_constraint...