Thursday, June 29, 2017

Get system date/time and display in formatted form


MainActivity.java
package com.blogspot.android_er.androiddatetime;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

TextView tvNow, tvNowFormatted1, tvNowFormatted2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvNow = (TextView)findViewById(R.id.now);
tvNowFormatted1 = (TextView)findViewById(R.id.nowformatted1);
tvNowFormatted2 = (TextView)findViewById(R.id.nowformatted2);

Date now = new Date();
tvNow.setText(now.toString());

String nowFormatted1 = DateFormat.getDateTimeInstance().format(now);
tvNowFormatted1.setText(nowFormatted1);

String nowFormatted2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss").format(now);
tvNowFormatted2.setText(nowFormatted2);
}
}



activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androiddatetime.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

<TextView
android:id="@+id/now"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"/>

<TextView
android:id="@+id/nowformatted1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textStyle="bold"
android:textSize="28dp"/>
<TextView
android:id="@+id/nowformatted2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textStyle="italic"
android:textSize="28dp"/>
</LinearLayout>



Reference:
java.util.Date
java.text.DateFormat
java.text.SimpleDateFormat


Friday, June 23, 2017

Question of using ContentLoadingProgressBar

android.support.v4.widget.ContentLoadingProgressBar is subclass of ProgressBar. That waits a minimum time to be dismissed before showing. Once visible, the progress bar will be visible for a minimum amount of time to avoid "flashes" in the UI when an event could take a largely variable time to complete (from none, to a user perceivable amount).

 I TRY to use it in this exercise. To display and hide it by calling its show() and hide() methods. By calling show(), show the progress view after waiting for a minimum delay. If during that time, hide() is called, the view is never made visible.

When long progress run after show(), the ContentLoadingProgressBar shown after a short time.
When short progress run after show(), the ContentLoadingProgressBar will not shown.

But the problem is: once ContentLoadingProgressBar no shown once, it will not be shown again! I don't is it normal behavior, or anything wrong.


Here is my exercise:

MainActivity.java
package com.blogspot.android_er.androidcontentloadingprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartShortProgress, btnStartLongProgress;
ProgressBar progressBar;
ContentLoadingProgressBar contentLoadingProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartShortProgress = (Button)findViewById(R.id.startshortprogress);
btnStartLongProgress = (Button)findViewById(R.id.startlongprogress);
progressBar = (ProgressBar)findViewById(R.id.ProgressBar);
contentLoadingProgressBar =
(ContentLoadingProgressBar)findViewById(R.id.ContentLoadingProgressBar);

btnStartShortProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartShortProgress.setEnabled(false);
btnStartLongProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(3);
}
});

btnStartLongProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartShortProgress.setEnabled(false);
btnStartLongProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(50);
}
});
}

public class MyAsyncTask extends AsyncTask<Integer, Integer, Void> {

@Override
protected Void doInBackground(Integer... integers) {
int i = integers[0];

while(i-- > 0){
SystemClock.sleep(100);
}

return null;
}

@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
contentLoadingProgressBar.show();
}

@Override
protected void onPostExecute(Void aVoid) {
progressBar.setVisibility(View.GONE);
contentLoadingProgressBar.hide();
btnStartShortProgress.setEnabled(true);
btnStartLongProgress.setEnabled(true);
}
}
}



activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidcontentloadingprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
/>

<Button
android:id="@+id/startshortprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start SHORT Progress"/>
<Button
android:id="@+id/startlongprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start LONG Progress"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProgressBar"
android:textStyle="bold"/>
<ProgressBar
android:id="@+id/ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ContentLoadingProgressBar"
android:textStyle="bold"/>
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/ContentLoadingProgressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>

</LinearLayout>


Examples of ProgressBar and AsyncTask

Thursday, June 22, 2017

Custom ProgressBar with SecondaryProgress

The former post show how to implement "Custom ProgressBar with progressDrawable" and "ProgressBar with SecondaryProgress". This example show how to Custom ProgressBar with SecondaryProgress.


Create a custom drawable xml res/drawable/myprogressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="180"
android:centerY="1.0"
android:startColor="#E8E8E8"
android:centerColor="#F0F0F0"
android:endColor="#F8F8F8" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerY="1.0"
android:startColor="#6000F000"
android:centerColor="#60008080"
android:endColor="#600000F0" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerY="1.0"
android:startColor="#E000F000"
android:centerColor="#E0008080"
android:endColor="#E00000F0" />
</shape>
</clip>
</item>
</layer-list>

Edit the layout to add android:progressDrawable
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>

<Button
android:id="@+id/startprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start ProgressBar"/>

<ProgressBar
android:id="@+id/progressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:max="10"
android:progress="0"
android:secondaryProgress="0"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/myprogressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:max="10"
android:progress="0"
android:secondaryProgress="0"
android:visibility="gone"
android:progressDrawable="@drawable/myprogressbar"/>

</LinearLayout>



MainActivity.java
package com.blogspot.android_er.androidprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartProgress;
ProgressBar progressBar, myProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnStartProgress = (Button)findViewById(R.id.startprogress);
progressBar = (ProgressBar)findViewById(R.id.progressbar);
myProgressBar = (ProgressBar)findViewById(R.id.myprogressbar);

btnStartProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
myProgressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
progressBar.setSecondaryProgress(0);
myProgressBar.setProgress(0);
myProgressBar.setSecondaryProgress(0);
}

@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
publishProgress(i, j);
SystemClock.sleep(100);
}
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
progressBar.setSecondaryProgress(values[1]);
myProgressBar.setProgress(values[0]);
myProgressBar.setSecondaryProgress(values[1]);
}

@Override
protected void onPostExecute(Void aVoid) {
progressBar.setVisibility(View.GONE);
myProgressBar.setVisibility(View.GONE);
btnStartProgress.setEnabled(true);
}
}
}


~ More examples of ProgressBar

ProgressBar with SecondaryProgress

Example to use ProgressBar with SecondaryProgress


MainActivity.java
package com.blogspot.android_er.androidprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartProgress;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnStartProgress = (Button)findViewById(R.id.startprogress);
progressBar = (ProgressBar)findViewById(R.id.progressbar);

btnStartProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
progressBar.setSecondaryProgress(0);
}

@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
publishProgress(i, j);
SystemClock.sleep(100);
}
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
progressBar.setSecondaryProgress(values[1]);
}

@Override
protected void onPostExecute(Void aVoid) {
progressBar.setVisibility(View.GONE);
btnStartProgress.setEnabled(true);
}
}
}



activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
/>

<Button
android:id="@+id/startprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start ProgressBar"/>

<ProgressBar
android:id="@+id/progressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="0"
android:secondaryProgress="0"
android:visibility="gone"/>

</LinearLayout>


Next:
Custom ProgressBar with SecondaryProgress

~ More examples of using ProgressBar


Tuesday, June 20, 2017

Add ProgressBar in ToolBar

This example show how to add android.support.v7.widget.Toolbar to layout and add ProgressBar to the ToolBar.


New a project of empty activity in Android Studio.

Edit res/values/styles.xml to change style to "Theme.AppCompat.Light.NoActionBar".
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>


Refer to the above video, add a ToolBar to the layout, and add a ProgressBar to the ToolBar, using Android Studio's graphical design tool.

res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.blogspot.android_er.toolbarprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintVertical_bias="0.501" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:visibility="gone"/>

</android.support.constraint.ConstraintLayout>


MainActivity.java
package com.blogspot.android_er.toolbarprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView tvTitle;
ProgressBar myProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvTitle = (TextView)findViewById(R.id.title);
myProgressBar = (ProgressBar)findViewById(R.id.progressBar);

tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(),
"ProgressBar start running",
Toast.LENGTH_LONG).show();
tvTitle.setClickable(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

@Override
protected void onPreExecute() {
myProgressBar.setVisibility(View.VISIBLE);
myProgressBar.setProgress(0);
}

@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<100; i++){
publishProgress(i);
SystemClock.sleep(100);
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
myProgressBar.setProgress(values[0]);
}

@Override
protected void onPostExecute(Void aVoid) {
myProgressBar.setVisibility(View.GONE);
tvTitle.setClickable(true);
}
}
}


Result:

To replace the indeterminate ProgressBar to Horizontal Progress Bar, edit res/layout/activity_main.xml. (maybe you want to set android:visibility="gone" as in progressBarStyle, depends on your case)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.blogspot.android_er.toolbarprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintVertical_bias="0.501" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layout_constraintBottom_toBottomOf="@+id/toolbar"
app:layout_constraintLeft_toLeftOf="@+id/toolbar"
app:layout_constraintRight_toRightOf="@+id/toolbar" />

</android.support.constraint.ConstraintLayout>



More examples of:
android.support.v7.widget.Toolbar
- ProgressBar

Monday, June 19, 2017

Custom ProgressBar with progressDrawable


To implement our custom ProgressBar, create a drawable xml under /res/drawable/.

res/drawable/myprogressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="10dip" />
<gradient
android:startColor="#000000"
android:centerColor="#FF0000"
android:centerY="1.0"
android:endColor="#B0B0B0"
android:angle="180"
/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="10dip" />
<gradient
android:startColor="#101010"
android:centerColor="#808080"
android:centerY="1.0"
android:endColor="#F0F0F0"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>

Edit the layout to add android:progressDrawable
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
/>

<Button
android:id="@+id/startprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start ProgressBar"/>

<ProgressBar
android:id="@+id/progressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:visibility="gone"/>

<ProgressBar
android:id="@+id/myprogressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:visibility="gone"
android:progressDrawable="@drawable/myprogressbar"/>

</LinearLayout>


MainActivity.java
package com.blogspot.android_er.androidprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartProgress;
ProgressBar progressBar, myProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnStartProgress = (Button)findViewById(R.id.startprogress);
progressBar = (ProgressBar)findViewById(R.id.progressbar);
myProgressBar = (ProgressBar)findViewById(R.id.myprogressbar);


btnStartProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

@Override
protected void onPreExecute() {
myProgressBar.setVisibility(View.VISIBLE);
myProgressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}

@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<100; i++){
publishProgress(i);
SystemClock.sleep(100);
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
myProgressBar.setProgress(values[0]);
progressBar.setProgress(values[0]);
}

@Override
protected void onPostExecute(Void aVoid) {
myProgressBar.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
btnStartProgress.setEnabled(true);
}
}
}


Next:
ProgressBar with SecondaryProgress
Custom ProgressBar with SecondaryProgress


Examples of ProgressBar and AsyncTask


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
/>

<Button
android:id="@+id/startprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start ProgressBar"/>

<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

<ProgressBar
android:id="@+id/determinateBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar2"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar3"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar4"
style="@android:style/Widget.ProgressBar.Large.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar5"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar6"
style="@android:style/Widget.ProgressBar.Small.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>



package com.blogspot.android_er.androidprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartProgress;
ProgressBar indeterminateBar;
ProgressBar determinateBar1, determinateBar2, determinateBar3,
determinateBar4, determinateBar5, determinateBar6;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnStartProgress = (Button)findViewById(R.id.startprogress);
indeterminateBar = (ProgressBar)findViewById(R.id.indeterminateBar);
determinateBar1 = (ProgressBar)findViewById(R.id.determinateBar1);
determinateBar2 = (ProgressBar)findViewById(R.id.determinateBar2);
determinateBar3 = (ProgressBar)findViewById(R.id.determinateBar3);
determinateBar4 = (ProgressBar)findViewById(R.id.determinateBar4);
determinateBar5 = (ProgressBar)findViewById(R.id.determinateBar5);
determinateBar6 = (ProgressBar)findViewById(R.id.determinateBar6);

btnStartProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

@Override
protected void onPreExecute() {
indeterminateBar.setVisibility(View.VISIBLE);
determinateBar1.setVisibility(View.VISIBLE);
determinateBar1.setProgress(0);
determinateBar2.setVisibility(View.VISIBLE);
determinateBar3.setVisibility(View.VISIBLE);
determinateBar4.setVisibility(View.VISIBLE);
determinateBar5.setVisibility(View.VISIBLE);
determinateBar6.setVisibility(View.VISIBLE);
}

@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<100; i++){
publishProgress(i);
SystemClock.sleep(100);
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
determinateBar1.setProgress(values[0]);
}

@Override
protected void onPostExecute(Void aVoid) {
indeterminateBar.setVisibility(View.GONE);
determinateBar1.setVisibility(View.GONE);
determinateBar2.setVisibility(View.GONE);
determinateBar3.setVisibility(View.GONE);
determinateBar4.setVisibility(View.GONE);
determinateBar5.setVisibility(View.GONE);
determinateBar6.setVisibility(View.GONE);
btnStartProgress.setEnabled(true);
}
}
}


Next:
Custom ProgressBar with progressDrawable
Add ProgressBar in ToolBar
ProgressBar with SecondaryProgress
Custom ProgressBar with SecondaryProgress
Question of using ContentLoadingProgressBar

Saturday, June 17, 2017

Install Python 3.6 (and IDLE/pip) on Ubuntu 17.04


To install Python 3.6 on Ubuntu 17.04, enter the command:
$ sudo apt-get install python3.6

Install IDLE for Python 3.6, idle-python3.6:
$ sudo apt install idle-python3.6


To install package using pip for Python 3.6:

- Download get-pip.py from https://pip.pypa.io/en/latest/installing/

- Install pip for Python 3.6
$ sudo python3.6 get-pip.py

- Install package:
$ sudo python3.6 -m pip install <package>
reference: Python documenet - Installing Python Modules - ... work with multiple versions of Python installed in parallel