create Spinner - Android User Interface

Android examples for User Interface:View

Description

create Spinner

Demo Code


import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Main{
    public static int SPINNER_COLOR = LIGHT_BLUE;
    /**/*  ww  w .j  a v a2 s .c o m*/
     * @fn public static Spinner createSpinner(Context context, String[] spinnerList,   int backgroundColor)
     * @brief Creates a Spinner object with default background.
     * @param context 
     * @param spinnerlist list of possible values for the spinner
     * @return Created Spinner Object.
     */

    public static Spinner createSpinner(Context context,
            String[] spinnerList) {
        return createSpinner(context, spinnerList, SPINNER_COLOR);
    }
    /**
     * @fn public static Spinner createSpinner(Context context, String[] spinnerList,   int backgroundColor)
     * @brief Creates a Spinner object.
     * @param context 
     * @param spinnerlist list of possible values for the spinner
     * @param backgroundColor Numerical representation of color. Use android.graphics.Color.rgb(red,green,blue)
     * @return Created Spinner Object.
     */

    public static Spinner createSpinner(Context context,
            String[] spinnerList, int backgroundColor) {
        /// http://stackoverflow.com/questions/11504635/layout-margin-for-text-view-programmatically

        // Create an ArrayAdapter using the string array and a default spinner layout
        Spinner spinner = new Spinner(context);

        // Create spinner from array http://stackoverflow.com/questions/2784081/android-create-spinner-programmatically-from-array
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, spinnerList);

        TableRow.LayoutParams tvlp = new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.MATCH_PARENT);
        spinner.setLayoutParams(tvlp);
        tvlp.setMargins(2, 2, 2, 2);
        spinner.setPadding(0, 0, 0, 0);
        //spinner.setText(message);
        //spinner.setTextSize(size);
        spinner.setBackgroundColor(backgroundColor);

        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        //spinner.setOnItemSelectedListener(context);

        //spinner.setTextColor(textColor);
        /// http://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertical-in-a-textview-in-android
        spinner.setGravity(Gravity.CENTER_VERTICAL
                | Gravity.CENTER_HORIZONTAL);

        return spinner;
    }
}

Related Tutorials