Create a spinner programmatically in Android - android

I want to create a spinner without using XML. I am new in android and my knowledge is limited. By now I have this code (see above) and I want my spinner in one of the tabs of my TabActivity.
There is no obvious error but when I open my activity the tab is empty. I would appreciate some help.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
}

You need to add the Spinner to a layout.
First create a container for the Spinner and then create the Spinner and add it to your container. Next set content of you Activity to your container.
This could be done like this, in your onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
LinearLayout layout = new LinearLayout(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
EDIT:
Just to clarify: if the Spinner isn't added to the content of the Activity inside a layout, it isn't visible, so that's why you don't get any errors or anything, because there isn't any errors in your code, per se ;-)

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = new LinearLayout(this);
// The following can also be done using a loop
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(MainActivity.this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
}

Related

Change the string names in spinner dynamically from previous spinner chosen item

I want to change the string names in spinner dynamically android from previous spinner chosen item.
This is my Activity class:
public class SelectionActivity extends AppCompatActivity implements View.OnClickListener {
final String LOG = "Selection";
private Spinner spBranch;
private Spinner spSection;
private Spinner spSemester,spSubject;
private Button btnSend;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spBranch = (Spinner) findViewById(R.id.spBranch1);
spSection = (Spinner) findViewById(R.id.spSection1);
spSemester = (Spinner) findViewById(R.id.spSemester1);
spSubject=(Spinner) findViewById(R.id.spSubject1);
String[] items1 = new String[]{"CSE", "EEE", "EE", "ECE", "MECH", "CIVIL"};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items1);
spBranch.setAdapter(adapter1);
String[] items2 = new String[]{"A", "B", "C", "D", "E", "F"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items2);
spSection.setAdapter(adapter2);
String[] items3 = new String[]{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"};
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items3);
spSemester.setAdapter(adapter3);
String[] items4 = new String[]{"Math 1", "Programming in C", "Thermodynamics", "Communication English", "Physics", "Basic Electronics"};
String[] items5 = new String[]{"Chemistry", "Data Structure", "Mechanics", "Buiseness English", "Basic Electrical Engineering l ", "Math 2"};
if(spSemester.getSelectedItem().toString().equals("1st"))
{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
spSubject.setAdapter(adapter4);
}
else{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
spSubject.setAdapter(adapter4);
}
btnSend = (Button) findViewById(R.id.btnSend);
}
#Override
public void onClick(View v) {
Intent in = new Intent(SelectionActivity.this, ListActivity.class);
startActivity(in);
HashMap postData = new HashMap();
postData.put("txtBranch", spBranch.getSelectedItem().toString());
postData.put("txtSection", spSection.getSelectedItem().toString());
postData.put("txtSemester", spSemester.getSelectedItem().toString());
}
}
I am getting Error at r this line spSubject.setAdapter(adapter4);
New to andorid, so is there any other way to do this
Note that all of the code you have posted will run immediately when the Activity is created. This is the whole point of the onCreate() method. Nothing will happen when the user selects a new item in any of the Spinners. If you want the Spinners to change more dynamically, you need to add appropriate event listeners. In particular, you need to implement AdapterView.OnItemSelectedListener. An example is shown here. I suggest creating separate classes which implement the listener interface rather than implementing it on your activity class.
Try this code to know if first index in spSemester spinner is selected or not
if(spSemester.getSelectedItemPosition() == 1)
{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items4);
spSubject.setAdapter(adapter4);
}
else{
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,items5);
spSubject.setAdapter(adapter4);
}

How to use onclicklistener

I am trying to use an onclick listener on radio buttons to update a spinner I have. The default numbers in the spinner will be 15, 25, 35, and 45. When you select the KG radio button I want it to change to 8, 15, and 20. I have the following:
public class PlateCalc_Tab extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.platecalc_tab);
RadioButton weightSettingLB = (RadioButton)findViewById(R.id.weightSettingLB);
RadioButton weightSettingKG = (RadioButton)findViewById(R.id.weightSettingKG);
weightSettingLB.setChecked(true);
weightSettingLB.setOnClickListener(updateToLB);
weightSettingKG.setOnClickListener(updateToKG);
List<String> weightOfBarArray = new ArrayList<String>();
weightOfBarArray.add("15");
weightOfBarArray.add("25");
weightOfBarArray.add("35");
weightOfBarArray.add("45");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner weightOfBarSpinner = (Spinner) findViewById(R.id.weightOfBarSpinner);
weightOfBarSpinner.setAdapter(adapter);
}
View.OnClickListener updateToKG = new View.OnClickListener()
{
public void onClick(View view)
{
}
};
This code currently works. When I enter the following code into my onclick listener I get an error.
View.OnClickListener updateToKG = new View.OnClickListener()
{
public void onClick(View view)
{
List<String> weightOfBarArray = new ArrayList<String>();
weightOfBarArray.add("8");
weightOfBarArray.add("15");
weightOfBarArray.add("20");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner weightOfBarSpinner = (Spinner) findViewById(R.id.weightOfBarSpinner);
weightOfBarSpinner.setAdapter(adapter);
}
};
I get "Cannot Resolve Constructor" # this line:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightOfBarArray);
Replace the array adapter initialization like below.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(PlateCalc_Tab.this, android.R.layout.simple_spinner_item, weightOfBarArray);
use
ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_spinner_item, weightOfBarArray);

Creating a Dynamic view with spinner Dependable in Android

I have an issue of loading view dynamically. I have three dynamic spinner view when i select first item from the spinner it should create a text view above second spinner but its creating a view below third spinner. Can any one guide me how to achieve it.I have shared my code below.
/add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
rl.setOrientation(LinearLayout.VERTICAL);
// add spinner
String[] spinnerArray = {"Android","ios","other"};
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
String[] spinnerArray1 = {"flowers","vegetables","other"};
Spinner spinner1 = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray1); //selected item will look like a spinner set from XML
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinnerArrayAdapter1);
String[] spinnerArray2 = {"cat","dog","other"};
Spinner spinner2 = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray2); //selected item will look like a spinner set from XML
spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinnerArrayAdapter2);
rl.addView(spinner);
rl.addView(spinner1);
rl.addView(spinner2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==1)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
rl.addView(tv);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Do you know how LinearLayout works?
http://developer.android.com/reference/android/widget/LinearLayout.html
You should call
addView(tv, 1);
Specifying position on where to insert view.

Spinner didnt update

public class MainActivity extends Activity {
private GestureDetector gestureDetector;
String dateData;
int choice ;
public HashSet<String> keyList = new HashSet<String>();
public ArrayList<String> temperatures = new ArrayList<String>();
public ArrayList<String> time = new ArrayList<String>();
public ArrayList<String> atList=new ArrayList<String>();
public ArrayList dataList=new ArrayList();
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this,new SwipeGestureDetector());
Spinner toList = (Spinner) findViewById(R.id.toList);
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
........................
protected void onPostExecute(String result)
{
Dialog.dismiss();
Iterator<String> iter = keyList.iterator();
while(iter.hasNext())
{
String key =iter.next();
dataAdapter.add(key);
dataAdapter.notifyDataSetChanged();
}
......................
..............................................................................
Sorry for this silly question , but i hav been doing it again and again from 0. it still doesn't get what i want. WHat problem i having now is bout the spinner didn't update itself after i set dataAdapter.notifyDataSetChanged().
Before this it work but my table isn't working .
now my table is working, this spinner not working. Omg
really need help desperately.
It seems that you are trying to add toList.setAdapter(dataAdapter) before initialize dataAdapter.So set dataAdapter after getting data in it,i.e. change
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
to
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, new ArrayList<String>());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
toList.setAdapter(dataAdapter);
// toList.setOnItemSelectedListener(new CustomOnItemSelectedListener());

android.R.simple_spinner_adapter cannot be resolved

Android 2.3.3
I am having a Spinner in my xml file and I wish to set an ArrayList as a source. I am trying to instantiate the ArrayAdapter with the ArrayList, but i don't get the R.simple_spinner_adapter to select.
Here is the code :::
public class UnitConverter extends Activity{
Spinner spnUnit;
ArrayAdapter<String> ad;
ArrayList<String> alAngle = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.unitconverter);
spnUnit = (Spinner) findViewById(R.id.spinnerUnit);
ad = new ArrayAdapter<String>(this, android.R.simple_spinner_adapter, alAngle);
// Error at above line near android.R.simple_spinner_adapter
setDataToArrayLists();
}
The ans to ur question is u are not using adapter properly:-
android.R.simple_spinner_adapter should be android.R.layout.simple_spinner_item
Sample to this can be:-
Spinner spinner = (Spinner) findViewById(R.id.font_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.font_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
check using as above this is sample used in my code...!!!!

Categories

Resources