Micro blog about Answer to the Ultimate Question of Life, the Universe, and Everything
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. Android

AsyncTask

Details
Written by: Stanko Milosev
Category: Android
Published: 19 January 2016
Last Updated: 03 February 2016
Hits: 4347

In order to have responsive UI use AsyncTask.

Here is one example. 

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.stanko.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <EditText
        android:id="@+id/myCounter"
        android:layout_width="match_parent"
        android:layout_height="1200px"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/myCounter"
        android:text="Start"
        android:onClick="startCounter"/>

</RelativeLayout>

MainActivity.java:

public class fillEdit extends AsyncTask<Void, String, String> {

	@Override
	protected String doInBackground(Void... params) {
		for (Integer i=0; i<10000; i++) {
			try {
				Thread.currentThread().sleep(60);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			publishProgress(i.toString());
		}
		return null;
	}

	@Override
	protected void onProgressUpdate (String... result) {
		EditText myCounter = (EditText) findViewById(R.id.myCounter);
		myCounter.append(result[0]);
		myCounter.append("\n");
	}
}

public void startCounter (View view) {
	new fillEdit().execute();
}

Here note:

try {
  Thread.currentThread().sleep(60);
} catch (InterruptedException e) {
  e.printStackTrace();
}

I needed to slow down code execution to make UI responsive, I also needed try / catch block because otherwise I would receive error:

Error:(51, 49) error: unreported exception InterruptedException; must be caught or declared to be thrown

Also, note how I am adding new line:

myCounter.append("\n");

Example of buttons one under another one

Details
Written by: Stanko Milosev
Category: Android
Published: 18 January 2016
Last Updated: 18 January 2016
Hits: 3924
<Button
	android:id="@+id/firstOne"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="First one"
	android:layout_alignParentLeft="true"
	android:layout_alignParentStart="true"
	android:layout_marginTop="53dp"
	/>

<Button
	android:id="@+id/secondOne"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:layout_below="@+id/firstOne"
	android:text="Second one"
	/>

Notice line:

android:layout_below="@+id/firstOne"

Align button to the bottom of page

Details
Written by: Stanko Milosev
Category: Android
Published: 18 January 2016
Last Updated: 18 January 2016
Hits: 4063

Example of button aligned to bottom of page:

<Button
	android:id="@+id/clickMe"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:layout_alignParentBottom="true"
	android:text="Click me"
	/>

Note line:

android:layout_alignParentBottom="true"

Getting rid of e-mail icon

Details
Written by: Stanko Milosev
Category: Android
Published: 18 January 2016
Last Updated: 18 January 2016
Hits: 4252

When you start new Android project, with SDK version 23, you will notice e-mail icon, like on picture:

Open activity_main.xml, in my case it is located in app\src\main\res\layout\activity_main.xml, and delete code:

<android.support.design.widget.FloatingActionButton
	android:id="@+id/fab"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="bottom|end"
	android:layout_margin="@dimen/fab_margin"
	android:src="@android:drawable/ic_dialog_email" />

Then in MainActivity.java, in my case it is located in app\src\main\java\com\example\smi\myapplication\MainActivity.java delete code:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View view) {
		Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
				.setAction("Action", null).show();
	}
});
  1. Simple Service Discovery Protocol
  2. Hello world
  3. Global exception handler
  4. Set the version

Page 8 of 11

  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11