First Create a layout in the res/layout folder, (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/treeimage"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:textColor="#CCCCCC"
/>
<Spinner android:id="@+id/Spinner01"
android:layout_width="300dp"
android:layout_height="50dp"
android:prompt="@string/Convertion"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
Run the application, it will display the page like below, A Spinner will be added in the layout.
After that Add your Array values in the string.xml, available in the res/values folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Convertion List</string>
<string name="app_name">DCConvertion</string>
<string name="Convertion">Select Convertion</string>
<string-array name="Convertion_Element">
<item>(Select)</item>
<item>Dollar To Currency</item>
<item>Currency To Dollar</item>
</string-array>
<string name="title">Dollar To Currency</string>
<string name="titlecur">Currency To Dollar</string>
<string name="dollar">Dollar Value</string>
<string name="currency">Currency Value</string>
<string name="convert">Convert</string>
</resources>
The above bolded item is the array element that are going to load in the spinner. You can add as many item you needed inside the <item></item> tag.
After that, Create a class file called DCConvertion.java
package com.convertion.dc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class DCConvertion extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// con=getApplicationContext();
Spinner spinner=(Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.Convertion_Element, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
// TODO Auto-generated method stub
//Toast.makeText(parent.getContext(), "Selected : "+parent.getItemIdAtPosition(pos), Toast.LENGTH_LONG).show();
switch(pos)
{
case 0:
Toast.makeText(parent.getContext(), "Please Select Your Choice", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(parent.getContext(), "You are Selected Doller to Currency Convertion", Toast.LENGTH_LONG).show();
Intent indtoc=new Intent(DCConvertion.this,Dollartocurrencyconvert.class);
startActivity(indtoc);
break;
case 2:
Toast.makeText(parent.getContext(), "You are Selected Currency to Dollar Convertion", Toast.LENGTH_LONG).show();
Intent inctod=new Intent(DCConvertion.this,Currencytodollarconvert.class);
startActivity(inctod);
break;
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
}
The above bolded code will create an array adapter for the spinner, inside the array adapter you should load your string array resource R.id.Convertion_Element. Finally set the loaded adapter to the spinner.
Run the program, and click the spinner, it will display like above image.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/treeimage"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:textColor="#CCCCCC"
/>
<Spinner android:id="@+id/Spinner01"
android:layout_width="300dp"
android:layout_height="50dp"
android:prompt="@string/Convertion"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
Run the application, it will display the page like below, A Spinner will be added in the layout.
After that Add your Array values in the string.xml, available in the res/values folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Convertion List</string>
<string name="app_name">DCConvertion</string>
<string name="Convertion">Select Convertion</string>
<string-array name="Convertion_Element">
<item>(Select)</item>
<item>Dollar To Currency</item>
<item>Currency To Dollar</item>
</string-array>
<string name="title">Dollar To Currency</string>
<string name="titlecur">Currency To Dollar</string>
<string name="dollar">Dollar Value</string>
<string name="currency">Currency Value</string>
<string name="convert">Convert</string>
</resources>
The above bolded item is the array element that are going to load in the spinner. You can add as many item you needed inside the <item></item> tag.
After that, Create a class file called DCConvertion.java
package com.convertion.dc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class DCConvertion extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// con=getApplicationContext();
Spinner spinner=(Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.Convertion_Element, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
// TODO Auto-generated method stub
//Toast.makeText(parent.getContext(), "Selected : "+parent.getItemIdAtPosition(pos), Toast.LENGTH_LONG).show();
switch(pos)
{
case 0:
Toast.makeText(parent.getContext(), "Please Select Your Choice", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(parent.getContext(), "You are Selected Doller to Currency Convertion", Toast.LENGTH_LONG).show();
Intent indtoc=new Intent(DCConvertion.this,Dollartocurrencyconvert.class);
startActivity(indtoc);
break;
case 2:
Toast.makeText(parent.getContext(), "You are Selected Currency to Dollar Convertion", Toast.LENGTH_LONG).show();
Intent inctod=new Intent(DCConvertion.this,Currencytodollarconvert.class);
startActivity(inctod);
break;
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
}
The above bolded code will create an array adapter for the spinner, inside the array adapter you should load your string array resource R.id.Convertion_Element. Finally set the loaded adapter to the spinner.
Run the program, and click the spinner, it will display like above image.
Nice one..
ReplyDelete