Pages

Showing posts with label beginner. Show all posts
Showing posts with label beginner. Show all posts

Monday, February 2, 2015

Android beginner tutorial Part 86 Localization

In this part we will talk about application localization.

After deployment, your application may be used on Android devices in many regions. To get the most users, you should think about making your application suitable for people speaking different languages - in other words, you should localize it.

Localization is actually fairly easy to implement. Up until now, when we set labels to buttons and text values to textviews and other components, we stored those values in strings.xml file (except some individual cases where we simply hard-coded the text in the xml files for testing purposes). Localization system lets us create multiple string, audio, visual, etc. files for different regions.

By using the res/values/strings.xml file, youre setting the default values for all regions. When told to look for a resource, the Android system picks the one that suits the region of the user. If no such item exists, the default value is picked. Thats why if youre using resources in your application, you should always include all of the used values that would be used by default.

The default values stored in res/values/ directory should be using the most common language you expect your application users to speak. Normally, its English.

To set resources for other regions, use the values-<qualifiers> pattern to create new directories and store your resources there. The <qualifiers> can be replaced by one or a group of qualifiers (for example, we know we can store images for different pixel density using qualifiers such as mdpi, hdpi, etc.).

As the Android official docs state: the language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase "r").

Examples of such language codes are "en", "fr", "ca", as well as "en-rUS", "fr-rFR" and "fr-rCA".

This means that we can store values in directories like res/values/strings.xml, res/values-fr/strings.xml, res/values-ja/strings.xml, etc.

And that is pretty much it. Using this information youll be able to make a flexible Android application which adapts to the users region.

Thanks for reading!
Read more »

Friday, January 30, 2015

Android beginner tutorial Part 1 Introduction

This is the introduction part of the Android development tutorial series.

So far Ive written only Actionscript 3 tutorials for this blog. I believe it is time to move on to another technology, which Ive chosen to be Android. The language used here isnt AS3, its Java - a language Im sure youve heard of. The syntax is similar to Actionscript, but some things are different. The Android SDK itself is pretty complex too. It is not going to be easy to learn Android development, but, in my opinion, its definitely worth it.

Since this used to be an AS3 blog, I am going to assume that my readers are people with some knowledge of AS3 and are beginners to Android SDK like me. The official Android website offers a series of tutorial to get developers started, however it is aimed at Java programmers.

For the first few tutorials, I am going to basically show you how to do the same thing they show at their official website, but explaining every detail that would be unclear to people with no previous experience with Java, for example, Ill explain the differencies in syntax between AS3 and Java as we write code.

I will try to explain everything the way that a person with some previous AS3 experience would understand. This means when we encounter unusual and new techniques or syntax features, Ill compare them with AS3, make analogies and give examples of how the same functionality could be achieved with AS3. I believe this will be a good learning experience for both you, my readers, and myself too.

All the tools used in Android development are free. It is also very good if you have an android device you can debug the applications on. The Eclipse IDE, which we will be using, together with Android SDK provide virtual device emulators too, which will show us how the application looks on a phone or a tablet by emulating the device. This will especially be useful when we want to see how our application looks on devices with different screen sizes.

First thing we need to do is head to the android developer website and download the Android SDK together with Eclipse IDE. Click here to do that now.

Download the bundle, unpack the ZIP file somewhere (the IDE has no installer, just a standalone exe), and run the eclipse.exe located in eclipse folder. Theres the IDE with all the Android SDK tools already built-in!

Well start writing our first Hello World application in the next tutorial.

Thanks for reading!
Read more »

Saturday, January 17, 2015

Android beginner tutorial Part 36 Spinner selection management

In this tutorial we will learn how to handle user selection in Spinner widgets, as well as how to select items ourselves.

Well make it so that when the user selects an item in the Spinner, a TextView, which is located under the Spinner, displays the selected item. Well also add a button that, when clicked, will randomly choose and select one of the options in the Spinner.

Firstly, go to the Activity layout and add the necessary components:

<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"
tools:context=".MainActivity" >

<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button_random"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_random" />

</LinearLayout>

Go to strings.xml and add a "button_random" item for the buttons label:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Code For Food Test</string>
<string name="menu_settings">Settings</string>
<string name="button_random">Choose randomly</string>
<string-array name="choices_array">
<item>One</item>
<item>Two</item>
<item>Three</item>
<item>Four</item>
<item>Five</item>
<item>Seven</item>
<item>Eight</item>
<item>Nine</item>
<item>Ten</item>
</string-array>

</resources>

In the MainActivity.java class, implement OnItemSelectedListener interface:

public class MainActivity extends Activity implements OnItemSelectedListener{

Declare a text variable. Also declare spinner, which is already used in the code, but well declare it as a private variable this time:

private TextView text;
private Spinner spinner;

Changes to make in the onCreate() function - apply value to text variable, declare a button variable, which references the "Choose randomly" button, add a listener for it, which sets the selection of the spinner randomly. Use the setSelection() method to do it. Use Math.round() and Math.random() to generate an index number. Use (int) in front of the number expression so that the setSelection() method comprehends it as an integer.

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

spinner = (Spinner)findViewById(R.id.spinner);
text = (TextView)findViewById(R.id.text);

String[] choices = getResources().getStringArray(R.array.choices_array);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(this);

Button button = (Button)findViewById(R.id.button_random);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
spinner.setSelection((int)Math.round(Math.random()*8));
}
});

}

Add two functions - onItemSelected and onNothingSelected. The first one is executed when an item is selected in the Spinner. The second is executed when nothing is selected. This happens rarely. It can happen, for example, if the data provider for the spinner is empty.

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
text.setText("The selected item is " + parent.getItemAtPosition(pos).toString());
}

public void onNothingSelected(AdapterView<?> parent) {
text.setText("Nothing is selected!");
}

Full code:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener{

private TextView text;
private Spinner spinner;

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

spinner = (Spinner)findViewById(R.id.spinner);
text = (TextView)findViewById(R.id.text);

String[] choices = getResources().getStringArray(R.array.choices_array);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(this);

Button button = (Button)findViewById(R.id.button_random);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
spinner.setSelection((int)Math.round(Math.random()*8));
}
});

}

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
text.setText("The selected item is " + parent.getItemAtPosition(pos).toString());
}

public void onNothingSelected(AdapterView<?> parent) {
text.setText("Nothing is selected!");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

Thanks for reading!
Read more »