For more info, check out:
Support Library Docs: https://goo.gl/rOJEa7
What's New in Android Support Library, Google I/O '17 talk:
<?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.androidedittextchanged.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:mipmap/sym_def_app_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="normal EditText"/>
<EditText
android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with drawableLeft"
android:drawableLeft="@mipmap/ic_launcher"/>
<EditText
android:id="@+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with drawableRight"
android:drawableRight="@mipmap/ic_launcher_round"/>
<EditText
android:id="@+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with drawableTop"
android:drawableTop="@mipmap/ic_launcher"/>
<EditText
android:id="@+id/edittext5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with drawableBottom"
android:drawableBottom="@mipmap/ic_launcher_round"/>
<EditText
android:id="@+id/edittext6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with background"
android:background="@mipmap/ic_launcher"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<?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.androidedittextchanged.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:mipmap/sym_def_app_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"/>
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:text="normal EditText"/>
<EditText
android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with background #50FFFFFF"
android:textSize="28dp"
android:background="#50FFFFFF"/>
<EditText
android:id="@+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with background #FFFFFF"
android:textSize="28dp"
android:background="#FFFFFF"/>
<EditText
android:id="@+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EditText with background #FFFFFF and alpha=0.5"
android:textSize="28dp"
android:background="#FFFFFF"
android:alpha="0.5"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#505050"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
<?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.androidedittextchanged.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"/>
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="normal EditText"/>
<EditText
android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="EditText with custom shape"
android:textColorHint="#B0B0B0"
android:textColor="#F0F0F0"
android:background="@drawable/myshape" />
</LinearLayout>
<?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.androidedittextchanged.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"/>
<AutoCompleteTextView
android:id="@+id/autocompletetextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<resources>
<string name="app_name">AndroidEditTextChanged</string>
<string-array name="suggestion">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
</resources>
package com.blogspot.android_er.androidedittextchanged;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView =
(AutoCompleteTextView)findViewById(R.id.autocompletetextview);
String[] suggestion = getResources().getStringArray(R.array.suggestion);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, suggestion);
autoCompleteTextView.setAdapter(adapter);
}
}
package com.blogspot.android_er.androidedittextchanged;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView tvMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.edittext);
tvMsg = (TextView)findViewById(R.id.msg);
editText.addTextChangedListener(myTextWatcher);
}
TextWatcher myTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
tvMsg.setText(editable.toString().toUpperCase());
}
};
}
<?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.androidedittextchanged.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"/>
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text"
android:textSize="24dp"/>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="24dp"/>
</LinearLayout>
package com.blogspot.android_er.androidedittextchanged;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView tvMsg, tvInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.edittext);
tvMsg = (TextView)findViewById(R.id.msg);
tvInfo = (TextView)findViewById(R.id.info);
editText.addTextChangedListener(myTextWatcher);
}
TextWatcher myTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
tvInfo.setText("beforeTextChanged(): \n"
+ charSequence + "\n"
+ i + "\n"
+ i1 + "\n"
+ i2);
}
@Override
public void onTextChanged(CharSequence charSequence,
int i, int i1, int i2) {
tvInfo.setText("onTextChanged(): \n"
+ charSequence + "\n"
+ i + "\n"
+ i1 + "\n"
+ i2);
tvMsg.setText(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
/*
String s = editable.toString();
tvInfo.setText("afterTextChanged(): \n"
+ s);
*/
}
};
}
<?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.androidedittextchanged.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"/>
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text"
android:textSize="24dp"/>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="24dp"/>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textSize="20dp"/>
</LinearLayout>
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);
}
}
<?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>
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);
}
}
}
<?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>
<?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>
<?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>
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);
}
}
}
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);
}
}
}
<?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>
<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>
<?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>
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);
}
}
}
<?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>
<?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>
<?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>
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);
}
}
}