Showing posts with label Android code sample: ListView. Show all posts
Showing posts with label Android code sample: ListView. Show all posts

Display available currencies java.util.Currency

This example list available currencies java.util.Currency. Please notice that the methods getAvailableCurrencies() and getDisplayName() used in this example introduced in API Level 19. So android:minSdkVersion in AndroidManifest.xml have to be set ="19".


package com.example.androidcurrency;

import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

ListView listCurrency;
Set<Currency> availableCurrenciesSet;
List<Currency> availableCurrenciesList;
ArrayAdapter<Currency> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listCurrency = (ListView)findViewById(R.id.currencylist);

//available from API Level 19
availableCurrenciesSet =
Currency.getAvailableCurrencies();

availableCurrenciesList = new ArrayList<Currency>(availableCurrenciesSet);
adapter = new ArrayAdapter<Currency>(
this,
android.R.layout.simple_list_item_1,
availableCurrenciesList);
listCurrency.setAdapter(adapter);

listCurrency.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Currency currency = (Currency) parent.getItemAtPosition(position);
String currencyCode = currency.getCurrencyCode();
String displayName = currency.getDisplayName();
String symbol = currency.getSymbol();

Toast.makeText(MainActivity.this,
displayName + "\n" +
currencyCode + "\n" +
symbol,
Toast.LENGTH_LONG).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="com.example.androidcurrency.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://arteluzevida.blogspot.com/"
android:textStyle="bold" />

<ListView
android:id="@+id/currencylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>



Related:
- Display currency symbols, hard coded.

DrawerLayout with ListView

Follow the exercise "Android DrawerLayout and DrawerListener", we are going to add a ListView inside DrawerLayout, and implement OnItemClickListener.

DrawerLayout with ListView
DrawerLayout with ListView

Layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
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="Main layout" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://arteluzevida.blogspot.com/"
android:textStyle="bold" />

<Button
android:id="@+id/opendrawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Drawer" />

<TextView
android:id="@+id/prompt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" />

<TextView
android:id="@+id/prompt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" />

<TextView
android:id="@+id/selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>

<LinearLayout
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/background_dark"
android:orientation="vertical"
android:padding="5dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Drawer" />

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<ListView
android:id="@+id/drawerlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0" />

<Button
android:id="@+id/closedrawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close Drawer" />
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

Main code:
package com.example.androiddrawerlayout;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

DrawerLayout drawerLayout;
View drawerView;
TextView textPrompt, textPrompt2;
ListView drawerList;
TextView textSelection;

private String[] dayOfWeek = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
ArrayAdapter<String> arrayAdapter;

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

textPrompt = (TextView)findViewById(R.id.prompt);
textPrompt2 = (TextView)findViewById(R.id.prompt2);

drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerView = (View)findViewById(R.id.drawer);

Button buttonOpenDrawer = (Button)findViewById(R.id.opendrawer);
buttonOpenDrawer.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
drawerLayout.openDrawer(drawerView);
}});

Button buttonCloseDrawer = (Button)findViewById(R.id.closedrawer);
buttonCloseDrawer.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
drawerLayout.closeDrawers();
}});

drawerLayout.setDrawerListener(myDrawerListener);

/*
* In my trial experiment:
* Without dummy OnTouchListener for the drawView to
* consume the onTouch event, touching/clicking on
* un-handled view on drawView will pass to the view
* under it!
* - Touching on the Android icon will
* trigger the TextView("http://arteluzevida.blogspot.com/")
* to open the web.
*/
drawerView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
});

textSelection = (TextView)findViewById(R.id.selection);
drawerList = (ListView)findViewById(R.id.drawerlist);
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
dayOfWeek);
drawerList.setAdapter(arrayAdapter);

drawerList.setOnItemClickListener(new OnItemClickListener(){

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

DrawerListener myDrawerListener = new DrawerListener(){

@Override
public void onDrawerClosed(View drawerView) {
textPrompt.setText("onDrawerClosed");
}

@Override
public void onDrawerOpened(View drawerView) {
textPrompt.setText("onDrawerOpened");
}

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
textPrompt.setText("onDrawerSlide: " + String.format("%.2f", slideOffset));
}

@Override
public void onDrawerStateChanged(int newState) {
String state;
switch(newState){
case DrawerLayout.STATE_IDLE:
state = "STATE_IDLE";
break;
case DrawerLayout.STATE_DRAGGING:
state = "STATE_DRAGGING";
break;
case DrawerLayout.STATE_SETTLING:
state = "STATE_SETTLING";
break;
default:
state = "unknown!";
}

textPrompt2.setText(state);
}};

}


download filesDownload the files.

Related: DrawerLayout with ListFragment