Skip to main content

Posts

Showing posts from August, 2015

How to show an indeterminate ProgressBar in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.os.Bundle; import android.app.Activity; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private int progressStatus = 0; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout Button button = findViewById(R.id.button); ProgressBar progressBar = findViewById(R.id.progressBar); TextView textView = findViewById(R.id.textView); // Set a click listener for Button widget button.setOnClickListener(view -> { // Disable the button itself view.setEnabled(false); // Set the progress status zero on ea...

How to create a ProgressBar programmatically in Android

Create a ProgressBar In Code ProgressBar is used to indicate the progress of an operation or long-running task, so the user can get a visual idea of how much time it will take to finish the job. We can simply add a ProgressBar widget to our XML layout file but when android developers want to put a ProgressBar in their app interface is a little bit harder. This android app development tutorial will guide you on how you can add a ProgressBar widget to your app rapidly in java code. In the first step, we will create a ProgressBar in Java code. After that, we will set its layout parameters and define relative layout rules, and put it to relative layout on a specific location. The relative layout addView method allows us to add a dynamic view to it. In the second step, we will show the operation progress in our newly added ProgressBar after a button click event. This is a determinate progress bar, so we can simply show the progress percentage in our widget. We also applied a color filter on...

How to change ProgressBar color programmatically in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.graphics.Color; import android.graphics.PorterDuff; import android.os.Bundle; import android.app.Activity; import android.os.Handler; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private int progressStatus = 0; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout Button button = findViewById(R.id.button); ProgressBar pbDefault = findViewById(R.id.pbDefault); ProgressBar pbColored = findViewById(R.id.pbColored); TextView tvProgress = findViewById(R.id.tvProgress); // Change the default color of progress bar programmatically pbColored.getProgressDrawable() ...

Android horizontal ProgressBar example

MainActivity.java package com.cfsuman.androidtutorials; import android.os.Bundle; import android.app.Activity; import android.os.Handler; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private int progressStatus = 0; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout Button button = findViewById(R.id.button); ProgressBar progressBar = findViewById(R.id.progressBar); TextView textView = findViewById(R.id.textView); // Set a click listener for Button widget button.setOnClickListener(view -> { // Disable the button itself view.setEnabled(false); // Set the progress status zero on each button click ...

How to set ImageView width and height programmatically in Android

Set ImageView Width & Height Programmatically ImageView widget allows us to display an image object to the android app user interface. We can add an ImageView element to the app interface by putting an ImageView tag inside the XML layout file. Then we can define the image source of the ImageView widget. We also can set the ImageView width and height in the XML layout file using ImageView layout width and height attributes. This is an easy task. But sometimes android app developers need to change the Imageview size (height and width) using their java or kotlin scripting file. This is called programmatically changing ImageView height and width in an android application. This android app development tutorial demonstrates to you how can we change the ImageView width and height dynamically in an android application. To do that, at first we will put an ImageView widget to our XML layout file. We will wrap our ImageView widget with a constraint layout. We will also add a Button widget to ...

How to create an ImageView programmatically in Android

Create An ImageView Programmatically The ImageView element displays an image to its visible area. Android app developers can simply add an ImageView tag and available attributes to their XML layout file to show an ImageView to the android app user interface. In this XML layout file, they can set the ImageView image source by using its src attribute. This is a simple job. But sometimes, android app developers want to add an ImageView widget programmatically to their android application. So they have to write a few lines of code to their java or kotlin programming file to achieve this. This android app development tutorial will demonstrate to us how can we add an ImageView widget to our android application dynamically and set an image source to display it. Though the following code snippets are very clear to understand the full scenario, we will describe here the code. To add an ImageView programmatically to the layout we first add a relative layout in the XML layout file. Then we will p...

How to set a bitmap to ImageView 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="#DCDCDC" android:padding="24dp" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Bitmap To ImageView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/iv" android:layout_width="0dp" android:layout_height="0dp...

How to set ImageView image from Assets 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="#DCDCDC" android:padding="24dp" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set ImageView Image From Assets" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/iv" android:layout_width="0dp" android:layout_height=...

How to set ImageView image from drawable 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="#DCDCDC" android:padding="24dp" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set ImageView Image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="25...

How to set ImageView image from URL 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="#DCDCDC" android:padding="24dp" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DownLoad And Set Image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/iv" android:layout_width="0dp" android:layout_height="0dp...

How to programmatically set image to ImageView in Android

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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:padding="24dp" tools:context=".MainActivity" android:background="#DCDCDC"> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </androidx...

How to set an image to ImageView in XML in Android

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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:padding="24dp" tools:context=".MainActivity" android:background="#F0F8FF"> <!-- ImageView Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources. --> <!-- android:src Sets a drawable as the content of this ImageView. May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[pac...

How to set an image to ImageView 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="#d8dcda" android:padding="24dp" tools:context=".MainActivity"> <!-- Set ImageView image by XML--> <ImageView android:id="@+id/ivXML" android:layout_width="match_parent" android:layout_height="150dp" android:src="@drawable/flower" android:scaleType="centerCrop" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> ...

How to set DatePickerDialog cancel button click listener in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Button; import android.widget.DatePicker; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; import java.util.Calendar; 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 = findViewById(R.id.button); // Show the date picker dialog button.setOnClickListener(v -> { // Initialize a new date picker dialog fragment DialogFragment dialogFragment = new DatePickerFragment(); // Show the date p...

How to create a custom Title for DatePickerDialog in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.app.DatePickerDialog; import android.app.Dialog; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.util.TypedValue; import android.view.Gravity; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams; import androidx.fragment.app.DialogFragment; import java.util.Calendar; 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 = findViewById(R.id.button); // Show the date picker dialog button.setOnClickListener(v -> { ...

How to set DatePickerDialog max date and min date in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; 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 = findViewById(R.id.button); // Show the date picker dialog button.setOnClickListener(v -> { // Initialize a new date picker dialog fragment DialogFragment dialogFragment = new DatePickerFr...

How to use theme in DatePickerDialog in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.widget.Button; import android.widget.DatePicker; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.view.ContextThemeWrapper; import androidx.fragment.app.DialogFragment; import java.util.Calendar; 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 = findViewById(R.id.button); // Show the date picker dialog button.setOnClickListener(v -> { // Initialize a new date picker dialog fragment DialogFragment dialogFragment = new DatePickerFragment(); // Show the date picker dialog frag...

How to format DatePickerDialog selected date in Android

MainActivity.java package com.cfsuman.androidtutorials; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; 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 = findViewById(R.id.button); // Show the date picker dialog button.setOnClickListener(v -> { // Initialize a new date picker dialog fragment DialogFragment dFragment = new DatePickerFragmen...

How to get AM PM value from TimePickerDialog 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(), ...