‏إظهار الرسائل ذات التسميات Android code sample: Spinner. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات Android code sample: Spinner. إظهار كافة الرسائل

الثلاثاء، 15 أبريل 2014

Display currency symbols

This example TRY to show various available currency symbols on Android. This symbols reference to the file http://www.unicode.org/charts/PDF/U20A0.pdf, it contains an excerpt from the character code tables and list of character names for The Unicode Standard, Version 6.3.

Please note that some symbols cannot be shown, because it have not been installed in Android system.


MainActivity.java
package com.example.androidshowcurrency;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

MyCurrency[] MyCurrencyAll ={
new MyCurrency("$", "dollar sign"),
new MyCurrency("¢", "cent sign"),
new MyCurrency("£", "pound sign"),
new MyCurrency("¤", "currency sign"),
new MyCurrency("¥", "yen sign"),
new MyCurrency("ƒ", "latin small letter f with hook"),
new MyCurrency("", "afghani sign"),
new MyCurrency("৲", "bengali rupee mark"),
new MyCurrency("૱", "gujarati rupee sign"),
new MyCurrency("௹", "tamil rupee sign"),
new MyCurrency("฿", "thai currency symbol baht"),
new MyCurrency("¤", "khmer currency symbol riel"),
new MyCurrency("ℳ", "script capital m"),
new MyCurrency("元", "cjk unified ideograph-5143"),
new MyCurrency("円", "cjk unified ideograph-5186"),
new MyCurrency("圆", "cjk unified ideograph-5706"),
new MyCurrency("圓", "cjk unified ideograph-5713"),
new MyCurrency("", "rial sign"),
new MyCurrency("₠", "EURO-CURRENCY SIGN"),
new MyCurrency("₡", "COLON SIGN"),
new MyCurrency("₢", "CRUZEIRO SIGN"),
new MyCurrency("₣", "FRENCH FRANC SIGN"),
new MyCurrency("₤", "LIRA SIGN"),
new MyCurrency("₥", "MILL SIGN"),
new MyCurrency("₦", "NAIRA SIGN"),
new MyCurrency("₧", "PESETA SIGN"),
new MyCurrency("₨", "RUPEE SIGN"),
new MyCurrency("₩", "WON SIGN"),
new MyCurrency("₪", "NEW SHEQEL SIGN"),
new MyCurrency("₫", "DONG SIGN"),
new MyCurrency("€", "EURO SIGN"),
new MyCurrency("₭", "KIP SIGN"),
new MyCurrency("₮", "TUGRIK SIGN"),
new MyCurrency("₯", "DRACHMA SIGN"),
new MyCurrency("₰", "GERMAN PENNY SIGN"),
new MyCurrency("₱", "PESO SIGN"),
new MyCurrency("₲", "GUARANI SIGN"),
new MyCurrency("₳", "AUSTRAL SIGN"),
new MyCurrency("₴", "HRYVNIA SIGN"),
new MyCurrency("₵", "CEDI SIGN"),
new MyCurrency("₶", "LIVRE TOURNOIS SIGN"),
new MyCurrency("₷", "SPESMILO SIGN"),
new MyCurrency("₸", "TENGE SIGN"),
new MyCurrency("₹", "INDIAN RUPEE SIGN"),
new MyCurrency("₺", "TURKISH LIRA SIGN")
};

Spinner spinnerCurrency;
TextView textBigCurrency;

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

textBigCurrency = (TextView)findViewById(R.id.bigcurrency);
spinnerCurrency = (Spinner)findViewById(R.id.spinnerCurrency);

MySpinnerAdapter adapterCurr =
new MySpinnerAdapter(MainActivity.this,
R.layout.row,
MyCurrencyAll);
spinnerCurrency.setAdapter(adapterCurr);
spinnerCurrency.setOnItemSelectedListener(onItemSelectedListener);

}

OnItemSelectedListener onItemSelectedListener =
new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
MyCurrency curr = (MyCurrency)(parent.getItemAtPosition(position));
textBigCurrency.setText(String.valueOf(curr.getSymbol()));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}

};

// define our custom class
public class MyCurrency {

private String symbol;
private String desc;

public MyCurrency(String symbol, String desc) {
this.symbol = symbol;
this.desc = desc;
}


public String getSymbol() {
return this.symbol;
}

public String getDesc() {
return this.desc;
}
}

// custom adapter
public class MySpinnerAdapter extends ArrayAdapter<MyCurrency> {

private MyCurrency[] myCurrencyArray;

public MySpinnerAdapter(Context context, int textViewResourceId,
MyCurrency[] myObjs) {
super(context, textViewResourceId, myObjs);
this.myCurrencyArray = myObjs;
}

public int getCount() {
return myCurrencyArray.length;
}

public MyCurrency getItem(int position) {
return myCurrencyArray[position];
}

public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

private View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.row, parent, false);

TextView textSymbol = (TextView) view
.findViewById(R.id.textSymbol);
textSymbol.setText(myCurrencyArray[position].getSymbol());
TextView textDesc = (TextView) view
.findViewById(R.id.textDesc);
textDesc.setText(myCurrencyArray[position].getDesc());

return view;
}
}

}

/res/layout/row.xml
<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="wrap_content"
android:orientation="vertical"
android:padding="10dp" >

<TextView
android:id="@+id/textSymbol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/textDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="italic" />
</LinearLayout>

/res/layout/activity_main.xml
<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="com.example.androidspinnertext.MainActivity" >

<TextView
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" />

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

<TextView
android:id="@+id/bigcurrency"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:textSize="150sp" />

</LinearLayout>


download filesDownload the files.

Or, download the APK HERE.

Related:
Display available currencies java.util.Currency

الأحد، 13 أبريل 2014

Custom Spinner with different normal view and drop-down view

It's another example of custom Spinner, which have different normal view and drop-down view, using custom object and adapter for the spinner. Our custom object have two String, one for display text, another for Internet address.

In normal view, returned by getView() of our custom ArrayAdapter, there are one TextView to show the display text, and another button to open the target address if clicked. In drop-down view, returned by getDropDownView() of our custom ArrayAdapter, there are two TextView to show the display text, and the target address.


/res/layout/row.xml, the layout of the normal view.
<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="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/gotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/gobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

/res/layout/dropdown.xml, the layout of the drop-down view.
<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="wrap_content"
android:orientation="vertical"
android:padding="10dp" >

<TextView
android:id="@+id/gotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/goaddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="italic" />
</LinearLayout>

/res/layout/activity_main.xml, the layout of our activity.
<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="com.example.androidspinnertext.MainActivity" >

<TextView
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" />

<Spinner
android:id="@+id/spinnergo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textgo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java
package com.example.androidspinnertext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
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 {

MyClass[] objGo ={
new MyClass("Android-er", "http://android-er.blogspot.com/"),
new MyClass("Arduino-er", "http://arduino-er.blogspot.com/"),
new MyClass("Hello Raspberry Pi", "http://helloraspberrypi.blogspot.com/"),
new MyClass("MyPhotoBlog", "http://photo-er.blogspot.com/"),
new MyClass("g+ Androider+", "https://plus.google.com/102969667192015169220"),
new MyClass("Youtube playlist: Android Development", "http://www.youtube.com/playlist?list=PLP7qPet500deChwUlhq-GsDl8Tun4_WMD"),
new MyClass("Google Play", "https://play.google.com/store")
};

Spinner spinnerGo;
TextView textViewGo;

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

textViewGo = (TextView)findViewById(R.id.textgo);
spinnerGo = (Spinner)findViewById(R.id.spinnergo);
MySpinnerAdapter adapterGo =
new MySpinnerAdapter(MainActivity.this,
R.layout.row,
objGo);
spinnerGo.setAdapter(adapterGo);
//spinnerGo.setOnItemSelectedListener(onItemSelectedListenerGo);

}

OnItemSelectedListener onItemSelectedListenerGo =
new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
MyClass obj = (MyClass)(parent.getItemAtPosition(position));
textViewGo.setText(String.valueOf(obj.getTarget()));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}

};

//define our custom class
public class MyClass{

private String text;
private String target;

public MyClass(String text, String target){
this.text = text;
this.target = target;
}

public void setText(String text){
this.text = text;
}

public String getText(){
return this.text;
}

public void setValue(String target){
this.target = target;
}

public String getTarget(){
return this.target;
}
}

//custom adapter
public class MySpinnerAdapter extends ArrayAdapter<MyClass>{

private MyClass[] myObjs;

public MySpinnerAdapter(Context context, int textViewResourceId,
MyClass[] myObjs) {
super(context, textViewResourceId, myObjs);
this.myObjs = myObjs;
}

public int getCount(){
return myObjs.length;
}

public MyClass getItem(int position){
return myObjs[position];
}

public long getItemId(int position){
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View spView = inflater.inflate(R.layout.row, parent, false);

TextView sp_GoText = (TextView)spView.findViewById(R.id.gotext);
sp_GoText.setText(myObjs[position].getText());

Button sp_GoButton = (Button)spView.findViewById(R.id.gobutton);
sp_GoButton.setText(myObjs[position].getTarget());

sp_GoButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
Uri uri = Uri.parse(myObjs[position].getTarget());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}});

return spView;
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View dropDownView = inflater.inflate(R.layout.dropdown, parent, false);

TextView dd_GoText = (TextView)dropDownView.findViewById(R.id.gotext);
dd_GoText.setText(myObjs[position].getText());

TextView dd_GoAddr = (TextView)dropDownView.findViewById(R.id.goaddr);
dd_GoAddr.setText(myObjs[position].getTarget());

return dropDownView;

}

}

}



download filesDownload the files.

الجمعة، 11 أبريل 2014

Spinner with different display text and return value

In the most basic Spinner implementation, selected item can be retrieved by calling parent.getItemAtPosition(position) in onItemSelected() method in OnItemSelectedListener. It will be the same object of the display items, as show in the spinner0 of the example.

Sometimes, we want to display some meaningful text in Spinner (such as "Sunday", "Monday"...), but return some other value when any item selected (such as 0, 2...).

Spinner with different display text and return value
Here I show two approaches:
  • The first one may be the simplest method, spinner1 in the example. Create another array to hold the values we want to return. And return the coresponding item on position in onItemSelected().
  • The second approach implement our custom class to hold the display text and the return value. And implement our custom Adapter, as shown in spinner2 in the example.

package com.example.androidspinnertext;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

String[] text0 = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };

String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
"WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};

MyClass[] obj2 ={
new MyClass("SUN", 0),
new MyClass("MON", 1),
new MyClass("TUE", 2),
new MyClass("WED", 3),
new MyClass("THU", 4),
new MyClass("FRI", 5),
new MyClass("SAT", 6)
};

Spinner spinner0, spinner1, spinner2;
TextView textView0, textView1, textView2;

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

textView0 = (TextView)findViewById(R.id.text0);
spinner0 = (Spinner)findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);

textView1 = (TextView)findViewById(R.id.text1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(onItemSelectedListener1);

textView2 = (TextView)findViewById(R.id.text2);
spinner2 = (Spinner)findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 =
new MySpinnerAdapter(MainActivity.this,
android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);

}

OnItemSelectedListener onItemSelectedListener0 =
new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s0 = (String)parent.getItemAtPosition(position);
textView0.setText(s0);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}
};

OnItemSelectedListener onItemSelectedListener1 =
new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s1 = String.valueOf(val1[position]);
textView1.setText(s1);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}

};

OnItemSelectedListener onItemSelectedListener2 =
new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
MyClass obj = (MyClass)(parent.getItemAtPosition(position));
textView2.setText(String.valueOf(obj.getValue()));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}

};

//define our custom class
public class MyClass{

private String text;
private int value;


public MyClass(String text, int value){
this.text = text;
this.value = value;
}

public void setText(String text){
this.text = text;
}

public String getText(){
return this.text;
}

public void setValue(int value){
this.value = value;
}

public int getValue(){
return this.value;
}
}

//custom adapter
public class MySpinnerAdapter extends ArrayAdapter<MyClass>{

private Context context;
private MyClass[] myObjs;

public MySpinnerAdapter(Context context, int textViewResourceId,
MyClass[] myObjs) {
super(context, textViewResourceId, myObjs);
this.context = context;
this.myObjs = myObjs;
}

public int getCount(){
return myObjs.length;
}

public MyClass getItem(int position){
return myObjs[position];
}

public long getItemId(int position){
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setText(myObjs[position].getText());
return label;
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = new TextView(context);
label.setText(myObjs[position].getText());
return label;
}
}

}

<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="com.example.androidspinnertext.MainActivity" >

<TextView
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" />

<Spinner
android:id="@+id/spinner0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>



download filesDownload the files.