Wednesday, 6 September 2017

What�s New in Support Library v26

Support Library v26 comes with a lot of new features, specifically: fonts as resources and downloadable fonts, access to backwards compatible emoji support via the new EmojiCompat library, the ability to allow the text size of your TextView to expand or contract automatically to fill its layout, the option to implement animations driven by physics forces with the �spring and fling� animations, improved parity with the Android framework, various form factors such as Wear and TV, and, finally, a library slimmed down by moving minSdk to 14 and dropping 1.4k methods.


For more info, check out:
Support Library Docs: https://goo.gl/rOJEa7

What's New in Android Support Library, Google I/O '17 talk:

Android Continuous Integration: Build-Deploy-Test Automation for Android Mobile Apps

Android Continuous Integration: Build-Deploy-Test Automation for Android Mobile Apps

Master continuous integration, deployment and automated testing for Android apps. You�ll see how to set up and tear down sandbox environments to test the end-user experience, where you�ll learn how to manage a mobile device in addition to the build machine. Android Continuous Integration applies a real-world CI pattern that has been thoroughly tested and implemented.

This book starts with continuous integration concepts and the tools and code needed to become proficient in continuous integration for Android apps. You�ll also follow acceptance test driven development (ATDD) best practice, giving you all the skills you need to become a better, more effective developer. Finally, you�ll learn about the Appium mobile automation library and the Jenkins continuous integration tool.

What You Will Learn
  • Understand how to build an Android mobile app from source
  • Set up a development or debugging environment for mobile apps
  • Integrate with the Nexus dependency management and application release tool
  • Work with the SonarQube code quality analyzer
  • Use debugging tools in Android
Who This Book Is For
Product owners/business analysts, QA/test engineers, developers and build/deploy engineers.

Wednesday, 16 August 2017

FREE eBook - Learning Python

FREE LEARNING PYTHON EBOOK

Discover the ins and outs of one of the most popular programming languages in the world today with the help of this eBook. Python is a dynamic and diverse language and can be used in everything from web applications to crunching raw data. No matter what you use it for though, we all have to start at the beginning; and Learning Python is your gateway to the world of professional Python development!

This book goes deeper than simply showing you how to build a Python app, giving you the fundamentals of Python programming that every developer needs to know to make the most of the language. Packed with tutorials and examples this title features everything from data structures, writing reusable code, testing, paradigms, and how Python can be adapted. This free eBook will help transform you from a complete beginner to someone ready to bring the best out of their projects.

So get this title now and see just why Python is so beloved today!

Download link: https://www.packtpub.com/packt/free-ebook/learning-python/




Wednesday, 19 July 2017

ExoPlayer: Flexible Media Playback for Android (Google I/O '17)


ExoPlayer is an open source media playback library for Android. Used by thousands of applications, it enables great media experiences and can be customized to suit individual needs. Recent additions to the library have ranged from a new high level API to advanced features such as multi-period DASH support and spatial audio. In this talk you�ll learn what�s new in ExoPlayer, as well as some of ExoPlayer�s key concepts, points of customization and inner workings.


Related Link:
> Android Developers > API Guides > Media and Camera > ExoPlayer


Tuesday, 18 July 2017

EditText with drawable icon


<?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>


Monday, 17 July 2017

Set background and alpha of EditText

Examples of Setting background and alpha of EditText:


<?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>

Sunday, 16 July 2017

EditText with custom shape (drawable)


To create EditText with our own shape, create a drawable XML to define our custom shape:

res/drawable/myshape.xml
<?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>


Reference my shape in layout 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.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>


Saturday, 15 July 2017

Download your FREE copy of 'Android Security Cookbook'

Ensure your apps are as tight as can be from outside threats with this cookbook. With recipes you can take to newer versions when needed this 350 page download shows you how to catch the trickiest vulnerabilities before they become a problem and gives you, and your customers, the peace of mind you deserve.

Download your FREE copy of 'Android Security Cookbook'


Thursday, 13 July 2017

AutoCompleteTextView, subclass of EditText with Auto-complete function

AutoCompleteTextView is a subclass of EditText that shows completion suggestions automatically while the user is typing.


To implement AutoCompleteTextView in your app:

Add the AutoCompleteTextView to your layout:
<?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>



Edit res/values/strings.xml to add array that contains all text suggestions.
<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>


Set adapter using the string array for the AutoCompleteTextView
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);


}

}



Tuesday, 11 July 2017

Another example of TextWatcher to monitor text changed in EditText

Last show Monitor user action on EditText, and do something in onTextChanged() method of TextWatcher. It's another example to do something in afterTextChanged() method, don't care what and where is the change, just do something on the changed text.


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>




Monday, 10 July 2017

Monitor user action on EditText

This example implement TextWatcher for EditText, such that we can detect user action on EditText, no extra "Enter" button is need.



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>



Next:
Another example of TextWatcher to monitor text changed in EditText

Sunday, 9 July 2017

Android Vitals

MUST SEE:
Introducing Android vitals, a guide to better app and games performance that helps drive engagement and installs.

Friday, 7 July 2017

Android Things Projects

Android Things Projects

Key Features

  • Learn to build promising IoT projects with Android Things
  • Make the most out of hardware peripherals using standard Android APIs
  • Build enticing projects on IoT, home automation, and robotics by leveraging Raspberry Pi 3 and Intel Edison

Book Description
Android Things makes developing connected embedded devices easy by providing the same Android development tools, best-in-class Android framework, and Google APIs that make developers successful on mobile.

With this book, you will be able to take advantage of the new Android framework APIs to securely build projects using low-level components such as sensors, resistors, capacitors, and display controllers. This book will teach you all you need to know about working with Android Things through practical projects based on home automation, robotics, IoT, and so on. We'll teach you to make the most of the Android Things and build enticing projects such as a smart greenhouse that controls the climate and environment automatically. You'll also create an alarm system, integrate Android Things with IoT cloud platforms, and more.

By the end of this book, you will know everything about Android Things, and you'll have built some very cool projects using the latest technology that is driving the adoption of IoT. You will also have primed your mindset so that you can use your knowledge for profitable, practical projects.

What you will learn

  • Understand IoT ecosystem and the Android Things role
  • See the Android Things framework: installation, environment, SDK, and APIs
  • See how to effectively use sensors (GPIO and I2C Bus)
  • Integrate Android Things with IoT cloud platforms
  • Create practical IoT projects using Android Things
  • Integrate Android Things with other systems using standard IoT protocols
  • Use Android Things in IoT projects

About the Author
Francesco Azzola is an electronic engineer with over 15 years of experience in computer programming and JEE architecture. He is SCEA certified (Sun Certified Enterprise Architect), SCWCD, and SCJP. He is an Android and IoT enthusiast. He loves creating IoT projects using Arduino, Raspberry Pi, Android, and other platforms.

He is interested in the convergence between IoT and mobile applications. Previously, he worked in the mobile development field for several years. He has created a blog called survivingwithandroid,where he shares posts about coding in Android and IoT projects.

Table of Contents

  1. Getting Started with Android Things
  2. Create an alarm system using Android Things
  3. How to make an environmental monitoring system
  4. Integrate Android Things with IoT cloud platforms
  5. Create a smart system to control ambient light
  6. Remote Weather station
  7. Build a spying eye
  8. Android with Android Things


Reactive Android Programming

Make the most of asynchronous android programming

Reactive Android Programming

About This Book

  • Install and set up RxJava for Android development
  • Implement the Reactive paradigm for Android programming using RxJava
  • Create cutting edge real world Android apps with Reactive programming.

Who This Book Is For
Are you an android developer trying to figure out how to use reactive paradigm for your programming needs? If yes then this is the book for you. No previous knowledge of RxJava is required.

What You Will Learn

  • Set up an environment for asynchronous that is reactive Android programming
  • Write custom observables and higher level abstractions
  • Orchestrating multiple calls using Reactive programming principles
  • Fetch remote financial data using RxJava
  • Integrate and process Twitter streams gracefully
  • Utilize Reactive programming to develop interactive and responsive Android apps
  • Create your own application to follow financial stock updates in real-time based on selected companies' symbols
  • Integrate updates from the Twitter for those companies.

In Detail
Writing code on Android is hard. Writing a high quality code that involves concurrent and parallel tasks is even harder. Ensuring that this code will run without unforeseen race conditions is an the order of magnitude harder. RxJava is the tool that can help write code for such tasks.

In this book a novice developer will be introduced to a wide variety of tools that RxJava provides to enable them to produce robust and high-quality code for their asynchronous tasks by building a relatively simple(and high quality) application using advanced RxJava techniques to produce a high quality product.

Part 1 of the book will lead the developer through RxJava's initial setup in Android environment. In Part 2, the reader will learn RxJava 2.0 step-by-step by starting off with stock data processing and display.The developer will learn to choose appropriate Schedulers and to use Retrofit library for remote requests.In Part 3, the reader will also learn advanced topics such as adding integration to Twitter to process its streaming data by combining it with stock data.

Style and approach
This book is a step by step practical guide which will essentially teach you to set up, implement, and debug Reactive Android Code with ease.

Thursday, 29 June 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, 23 June 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, 22 June 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, 20 June 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, 19 June 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, 17 June 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

Mastering Android Game Development with Unity

Create enthralling Android games with Unity Faster Than Ever Before

Mastering Android Game Development with Unity

About This Book
  • Develop complex Android games with the help of Unity's advanced features such as artificial intelligence, high-end physics, and GUI transformations.
  • Create amazing Graphical User Interfaces (GUIs) with Unity's new uGUI system
  • Unravel and deploy exciting games across Android devices
Who This Book Is For
If you are a Unity 5 developer and want to expand your knowledge of Unity 5 to create high-end complex Android games, then this book is for you. Readers are expected to have a basic understanding of Unity 5, working with its environment, and its basic concepts.

What You Will Learn
  • Develop your own Jetpack Joyride clone game
  • Explore the advanced features of Unity 5 by building your own Action Fighting game
  • Develop remarkable Graphical User Interfaces (GUIs) with Unity's new uGUI system
  • Enhance your game by adding stunning particle systems and complex animations
  • Build pleasing virtual worlds with special effects, lights, sky cube maps, and cameras
  • Make your game more realistic by providing music and sound effects
  • Debug and deploy your games on different Android devices
In Detail
Game engines such as Unity are the power-tools behind the games we know and love. Unity is one of the most widely-used and best loved packages for game development and is used by everyone, from hobbyists to large studios, to create games and interactive experiences for the Web, desktop, mobile, and console. With Unity's intuitive, easy-to-learn toolset and this book, it's never been easier to become a game developer.

You will begin with the basic concepts of Android game development, a brief history of Android games, the building blocks of Android games in Unity 5, and the basic flow of games. You will configure an empty project for the Jetpack Joyride Clone Game, add an environment and characters, and control them. Next you will walk through topics such as particle systems, camera management, prefabs, animations, triggers, colliders, and basic GUI systems. You will then cover the basic setup for 3D action fighting games, importing models, textures and controlling them with a virtual on-screen joystick. Later you will set up Scene for 3D Configuration, create basic gameplays, and manage input controls. Next you will learn to create the interface for the main menu, gameplay, game over, achievements, and high score screens. Finally you will polish your game with stats, sounds, and Social Networking, followed by testing the game on Android devices and then publishing it on Google Play, Amazon, and OUYA Stores.

Style and approach
A step-by-step and detailed guide to developing high-end complex Android games utilizing the advanced concepts of Unity.

Monday, 12 June 2017

Example using SafetyNet reCAPTCHA API in Android App

reCAPTCHA Android API announced as part of Google Play Services. It's a simple example to use SafetyNet reCAPTCHA API in Android App. Basically, it follow the steps in the tutorial SafetyNet reCAPTCHA API.



- Create a Android Project in Android Studio as as usually, with minSdkVersion of 14 or higher. We need the Package Names in the next step.

- Visit reCAPTCHA Android signup site, fill in the blanks to get your Site key and Secret key. (Get your own, I will delete my keys after the samples finished.) Copy the keys to your program as String.

- Open SDK Manager in Android Studio, make sure you include Google Repository in your SDK Tools.

- Add the follow Google Play services APIs into your Gradle dependencies:
    compile 'com.google.android.gms:play-services-base:11.0.0'
compile 'com.google.android.gms:play-services-basement:11.0.0'
compile 'com.google.android.gms:play-services-safetynet:11.0.0'
compile 'com.google.android.gms:play-services-tasks:11.0.0'

This video show how to:


Modify the code:

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

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.safetynet.SafetyNet;
import com.google.android.gms.safetynet.SafetyNetApi;

public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{

final String SiteKey = "6LdMKyUUAAAAAN0ndw7byI03_qpbpjxKY-mTQnLw";
final String SecretKey = "6LdMKyUUAAAAALjcWovpXgcoXiI4i9ykn1U9qs8I";
private GoogleApiClient mGoogleApiClient;

Button btnRequest;
TextView tvResult;

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

tvResult = (TextView)findViewById(R.id.result);
btnRequest = (Button)findViewById(R.id.request);
btnRequest.setOnClickListener(RqsOnClickListener);

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(SafetyNet.API)
.addConnectionCallbacks(MainActivity.this)
.addOnConnectionFailedListener(MainActivity.this)
.build();

mGoogleApiClient.connect();
}

View.OnClickListener RqsOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
tvResult.setText("");

SafetyNet.SafetyNetApi.verifyWithRecaptcha(mGoogleApiClient, SiteKey)
.setResultCallback(new ResultCallback<SafetyNetApi.RecaptchaTokenResult>() {
@Override
public void onResult(SafetyNetApi.RecaptchaTokenResult result) {
Status status = result.getStatus();

if ((status != null) && status.isSuccess()) {

tvResult.setText("isSuccess()\n");
// Indicates communication with reCAPTCHA service was
// successful. Use result.getTokenResult() to get the
// user response token if the user has completed
// the CAPTCHA.

if (!result.getTokenResult().isEmpty()) {
tvResult.append("!result.getTokenResult().isEmpty()");
// User response token must be validated using the
// reCAPTCHA site verify API.
}else{
tvResult.append("result.getTokenResult().isEmpty()");
}
} else {

Log.e("MY_APP_TAG", "Error occurred " +
"when communicating with the reCAPTCHA service.");

tvResult.setText("Error occurred " +
"when communicating with the reCAPTCHA service.");

// Use status.getStatusCode() to determine the exact
// error code. Use this code in conjunction with the
// information in the "Handling communication errors"
// section of this document to take appropriate action
// in your app.
}
}
});

}
};

@Override
public void onConnected(@Nullable Bundle bundle) {
Toast.makeText(this, "onConnected()", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this,
"onConnectionSuspended: " + i,
Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this,
"onConnectionFailed():\n" + connectionResult.getErrorMessage(),
Toast.LENGTH_LONG).show();
}
}


layout:
<?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:orientation="vertical"
android:layout_margin="20dp"
tools:context="com.blogspot.android_er.recaptcha.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/request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Request SafetyNet reCAPTCHA API"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>