top of page
Adding Widgets and Layout in Java

package com.mycompany.arraysexample;

import android.graphics.Color;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

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

        //CREATE layout and widget first
        RelativeLayout relLay = new RelativeLayout(this);
        relLay.setBackgroundColor(Color.BLUE);
        Spinner newSpinner =new Spinner(this);
        newSpinner.setTag("array_list1");


        //add button to the layout
        relLay.addView(newSpinner);

        //then add to the content view so you can see it
        setContentView(relLay);

        //Create a string array to hold the text to be displayed
        String[] myStringArray={"Alpha","Bravo","Charlie"};


        //Create the ArrayAdapter
        ArrayAdapter<String> myAdapter=new
                ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                myStringArray);



        //Associate the ArrayAdapter with the Spinner

        Spinner myList=
                (Spinner) newSpinner;
        myList.setAdapter((myAdapter));


        //
    }
}

bottom of page