Skip to main content

Posts

android - How to save image to file in external storage

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"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save Image To External Storage" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/ivSource" android:layout_width="0dp" android:layout_height=...

android - How to save an image to gallery

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"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save Image To Gallery" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/ivSource" android:layout_width="0dp" android:layout_height="200...

android - How to take ScreenShot programmatically

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" android:id="@+id/rootLayout" tools:context=".MainActivity" android:background="#DCDCDC"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Take ScreenShot" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/imageView" android:layout_width="0dp" ...

android - How to change ActionBar title programmatically

MainActivity.java package com.cfsuman.androidtutorials; import android.os.Bundle; import android.widget.Button; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; 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); // Set the button click listener button.setOnClickListener(view -> { // Get the ActionBar ActionBar actionBar = getSupportActionBar(); // Change the ActionBar title text assert actionBar != null; actionBar.setTitle("Updated ActionBar Title"); }); } } activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.Const...

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() ...