Sep 29, 2013

Request focus on a specified EditText view in startup, in XML

Last example show Java code to "Request focus on a specified EditText view in startup, by calling requestFocus()". Alternative, it can be achieve by specifying <requestFocus /> in XML.

It's a example to request focus on edittext3, modify layout from last example.

    <EditText
        android:id="@+id/edittext3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <requestFocus />
    </EditText>


focus on edittext3
Focus on edittext3, defined in XML.

Request focus on a specified EditText view in startup, by calling requestFocus()

The example force a specified EditText, editText2, to be focused when the app start-up, by calling its requestFocus().

Remark: somebody advise to specify android:focusable="true" and android:focusableInTouchMode="true" in XML.

For reference, I display the focus view in onCreate(). And also implement OnGlobalLayoutListener() to monitor the focus view after layout finished, and when focus view changed.

focus on a specified EditText view
focus on a specified EditText view


package com.example.androidfocus;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 TextView textInfo;
 EditText editText1, editText2, editText3;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textInfo = (TextView)findViewById(R.id.info);
  editText1 = (EditText)findViewById(R.id.edittext1);
  editText2 = (EditText)findViewById(R.id.edittext2);
  editText3 = (EditText)findViewById(R.id.edittext3);
  
  editText2.requestFocus();
  
  View focusViewOnCreate = getWindow().getCurrentFocus();
  if(focusViewOnCreate == null){
   Toast.makeText(MainActivity.this, 
     "NO View get focus onCreate()!", Toast.LENGTH_LONG).show();
  }else{
   Toast.makeText(MainActivity.this, 
     "focus onCreate(): " + focusViewOnCreate.toString(), 
     Toast.LENGTH_LONG).show();
  }
  
  ViewTreeObserver viewTreeObserver = textInfo.getViewTreeObserver();
  viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){

   @Override
   public void onGlobalLayout() {
    /*
     *  onGlobalLayout() will be called whenthe global layout state 
     *  or the visibility of views within the view tree changes.
     *  
     *  In this case, it will be called after layout displayed, or 
     *  focus view changed. 
     */
    
    View focusViewOnGlobalLayout = getWindow().getCurrentFocus();
    if(focusViewOnGlobalLayout == null){
     textInfo.setText("NO View get Focus!");
    }else{
     textInfo.setText("Focus View:\n" +  focusViewOnGlobalLayout.toString());
    }
   }});
 }

}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="android-coding" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
        android:id="@+id/edittext3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Alternatively, you can also Request focus by defining in layout XML.

Sep 22, 2013

Create Dialog with List, using AlertDialog.Builder setItems()

You can add list to Dialog, use the setItems() method of AlertDialog.Builder. Because the list appears in the dialog's content area, the dialog cannot show both a message and list.


Dialog with ListView, using AlertDialog.Builder setItems()
Dialog with ListView, using AlertDialog.Builder setItems()

package com.example.androidlistdialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MainActivity extends Activity {
 
 Button buttonOpenDialog;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonOpenDialog = (Button)findViewById(R.id.opendialog);
  buttonOpenDialog.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    openDialog();
   }});
 }

 private void openDialog(){
  
  final String[] days = {
    "Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday"  
  };
  
  AlertDialog.Builder myDialog = 
    new AlertDialog.Builder(MainActivity.this);
  myDialog.setTitle("My Dialog with ListView");
  myDialog.setItems(days, new DialogInterface.OnClickListener(){

   @Override
   public void onClick(DialogInterface dialog, int which) {
    String item = days[which];
    Toast.makeText(MainActivity.this, 
      item, Toast.LENGTH_LONG).show();
    
   }});
  
  myDialog.setNegativeButton("Cancel", null);
  
  myDialog.show();
 }

}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <Button 
        android:id="@+id/opendialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Dialog"/>

</LinearLayout>


Related: Create Dialog with traditional ListView, using AlertDialog.Builder.

Sep 21, 2013

Create Dialog with ListView, using AlertDialog.Builder.

Dialog with ListView
Dialog with ListView

package com.example.androidlistdialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ListView;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;

public class MainActivity extends Activity {
 
 Button buttonOpenDialog;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonOpenDialog = (Button)findViewById(R.id.opendialog);
  buttonOpenDialog.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    openDialog();
   }});
 }

 private void openDialog(){
  
  String[] days = {
    "Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday"  
  };
  
  AlertDialog.Builder myDialog = 
    new AlertDialog.Builder(MainActivity.this);
  myDialog.setTitle("My Dialog with ListView");
  
  ListView listView = new ListView(MainActivity.this);
  listView.setBackgroundColor(Color.argb(255, 255, 255, 255));
  ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, days);
  listView.setAdapter(adapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    String item = (String)parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, 
      item, Toast.LENGTH_LONG).show();
   }});
  
  myDialog.setNegativeButton("Cancel", null);
  
  myDialog.setView(listView);
  myDialog.show();
 }

}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <Button 
        android:id="@+id/opendialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Dialog"/>

</LinearLayout>


Related: Create Dialog with List, using AlertDialog.Builder setItems()

Sep 20, 2013

New Features in the Google Maps Mobile APIs for Android and iOS

Maps Live: New Features in the Google Maps Mobile APIs for Android and iOS

Google Maps API Team members Daniel Schramm and Chris Broadfoot discuss exciting new cross platform features in the Google Maps Android API v2 (http://goo.gl/5k18Es) and the Google Maps SDK for iOS (http://goo.gl/r2c2e). New to the latest versions are map padding, marker rotation, and flat markers.

Sep 11, 2013

Common user experience issues

Android Design in Action: Common UX Issues

This video run through a top-ten-style list of the most common user experience issues.

Sep 9, 2013

Html.ImageGetter load image from internet

Last example "Display HTML String with multi images on TextView, with Html.ImageGetter" load image from local drawable. This example demonstrate how to implement Html.ImageGetter() to load image from internet, and also how to handle images from difference source, local drawable and internet.

Html.ImageGetter load image from internet
Html.ImageGetter load image from internet


package com.example.androidhtmltextview;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='http://android-coding.blogspot.com'>Android Coding</a></b>"
   + "<br/><br/>"
   + "another image load from internet<br/>"
   + "<img src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_vBn5OxsMM1fUIK0w8awJdKJjgJJ07cBYdWcCscYMujdoEYWWi549p7J5FYnYJSggLjw2pomiwcseExrMX6X8OarAb-bEQwH-sccJtSyHlGnoUrrVtIbgbXP_60n3nzhAwAA1mMuXoNRf/s400/AndroidHtmlTextView_multi_images.png'><br/>";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TextView htmlTextView = new TextView(this);
  setContentView(htmlTextView);

  htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter() {

   @Override
   public Drawable getDrawable(String source) {

    Toast.makeText(getApplicationContext(), source,
      Toast.LENGTH_LONG).show();

    Drawable drawable = null;
    if (source.startsWith("http")) {
     // load from internet

     URL sourceURL;
     try {
      sourceURL = new URL(source);
      URLConnection urlConnection = sourceURL.openConnection();
      urlConnection.connect();
      InputStream inputStream = urlConnection.getInputStream();
      BufferedInputStream bufferedInputStream = 
        new BufferedInputStream(inputStream);
      Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream);

      // convert Bitmap to Drawable
      drawable = new BitmapDrawable(getResources(), bm);
      
      drawable.setBounds(0, 0, 
        bm.getWidth(),
        bm.getHeight());

     } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

    } else {
     // load from local drawable

     int dourceId = getApplicationContext()
       .getResources()
       .getIdentifier(source, "drawable", getPackageName());

     drawable = getApplicationContext().getResources()
       .getDrawable(dourceId);

     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
       drawable.getIntrinsicHeight());
    }

    return drawable;
   }

  }, null));

  htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());

 }

}


In order to load image from internet, permission of "android.permission.INTERNET" have to be added in AndroidManifest.xml.

To solve error of android.os.NetworkOnMainThreadException, read Html.ImageGetter load image from internet, in AsyncTask.

Display HTML String with multi images on TextView, with Html.ImageGetter

Previous example load single image in HTML String with Html.ImageGetter. This example load two different images in HTML String. Because both images in drawable, so we can use a common Html.ImageGetter.

Display HTML String with multi images on TextView, with Html.ImageGetter
Display HTML String with multi images on TextView, with Html.ImageGetter


package com.example.androidhtmltextview;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='http://android-coding.blogspot.com'>Android Coding</a></b>" +
   "<br/>" +
   "<img src='page'><b><a href='https://plus.google.com/b/113141750089533146251/'>My G+ Page</a></b><br/>";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  TextView htmlTextView = new TextView(this);
  setContentView(htmlTextView);
  
  htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter(){

   @Override
   public Drawable getDrawable(String source) {
    
    Toast.makeText(getApplicationContext(), 
      source, 
      Toast.LENGTH_LONG).show();
    
    Drawable drawable;
    int dourceId = 
      getApplicationContext()
      .getResources()
      .getIdentifier(source, "drawable", getPackageName());
    
    drawable = 
      getApplicationContext()
      .getResources()
      .getDrawable(dourceId);
    
    drawable.setBounds(
      0, 
      0, 
      drawable.getIntrinsicWidth(),
      drawable.getIntrinsicHeight());
    
    return drawable;
   }
   
  }, null));
  
  htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());
  
 }

}


Next: Html.ImageGetter load image from internet

Sep 8, 2013

zxing, multi-format 1D/2D barcode image processing library with clients for Android, Java

ZXing (pronounced "zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Our focus is on using the built-in camera on mobile phones to scan and decode barcodes on the device, without communicating with a server. However the project can be used to encode and decode barcodes on desktops and servers as well. We currently support these formats:
  • UPC-A and UPC-E
  • EAN-8 and EAN-13
  • Code 39
  • Code 93
  • Code 128
  • ITF
  • Codabar
  • RSS-14 (all variants)
  • RSS Expanded (most variants)
  • QR Code
  • Data Matrix
  • Aztec ('beta' quality)
  • PDF 417 ('alpha' quality)

link: https://code.google.com/p/zxing/


Android-Universal-Image-Loader, a powerful image loading library for Android

The project Universal Image Loader for Android (Android-Universal-Image-Loader) aims to provide a reusable instrument for asynchronous image loading, caching and displaying. It support Android 2.0+ with tje features:
  • Multithread image loading
  • Possibility of wide tuning ImageLoader's configuration (thread executors, downlaoder, decoder, memory and disc cache, display image options, and others)
  • Possibility of image caching in memory and/or on device's file sysytem (or SD card)
  • Possibility to "listen" loading process
  • Possibility to customize every display image call with separated options
  • Widget support

Android-Universal-Image-Loader screenshoots
Android-Universal-Image-Loader screenshoots

Sep 6, 2013

Cardflip Animation

DevBytes: Cardflip Animation

This video shows how the basic graphics and animation APIs can be used to rotate and flip views from different perspectives, as well as how to apply darkening shadow effects onto a View. code: http://developer.android.com/shareables/devbytes/CardFlip.zip

Sep 3, 2013

Sliding Fragments: animate transition between two fragments

DevBytes Sliding Fragments

How to animate Fragment Transitions.This video discusses several different methods how animations can be incorporated into the transition between two fragments.

code: http://developer.android.com/shareables/devbytes/SlidingFragments.zip

Infolinks In Text Ads