• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
Posted by Jack - - 2 comments

GET SELECTED ITEM FROM SPINNER


SOURCE CODE [main.xml] is


<?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" >

<TextView  android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"  />

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

</LinearLayout>


SOURCE CODE [SpinnerExample.java] is


package com.SpinnerExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerExample extends Activity
{
                Spinner sp;
                ArrayAdapter<String> adapter;
                String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
                                                "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                sp = (Spinner) findViewById(R.id.spinner1);

                                adapter = new ArrayAdapter<String>(this,
                                                                android.R.layout.simple_spinner_item, numbers);
                                sp.setAdapter(adapter);
                               
                                sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
                                                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                                                                int arg2, long arg3)
{
Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();                                                 
                                                }
                                                public void onNothingSelected(AdapterView<?> arg0)
{
                                                                // TODO Auto-generated method stub                 
                                                }
});
                }
}


The OUTPUT will be

 https://lh3.googleusercontent.com/-eLkCsPmtrok/ToLIKd9vNuI/AAAAAAAAAM8/CIs9XMWspbg/spinner1.png

https://lh6.googleusercontent.com/-ErdfAXtD2KU/ToLIKiK9y7I/AAAAAAAAANA/1GKCRJwoiQ8/spinner2.png

Posted by Jack - - 0 comments


IMAGE URL TO BITMAP

Use this function “convertURLtoBitmap”

public Bitmap convertURLtoBitmap(String src) {

                try {

                                URL url = new URL(src);
                                               
                                HttpURLConnection connection = (HttpURLConnection) url
                                                .openConnection();

                                connection.setDoInput(true);
                                connection.connect();

                                InputStream input = connection.getInputStream();
                                Bitmap myBitmap = BitmapFactory.decodeStream(input);

                                return myBitmap;

                }

                catch (IOException e) {

                                e.printStackTrace();
                                return null;

                }
}