Creating a Dynamic view with spinner Dependable in Android - 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.

Related

How to add customize Spinner?

I am working on a project where I have to implement a spinner. My spinner code is below:
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, CatagoryName);
// create a spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// add adapter to spinner
spinner.setAdapter(stringArrayAdapter);
// create listener and add to spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// put code which recognize a selected element
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Basically, I want to use my custom spinner layout at
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, CatagoryName);
R.layout.simple_spinner_dropdown is not what I want. How to replace this layout with mine?
this is worked for me,
firstly define spinner items in strings.xml which in values directory as above,
<string-array name="spinner_array">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
then try above code,
Spinner customSpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_array, R.layout.custom_spinner_dropdown);
customSpinner.setAdapter(adapter);
Try this approach:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
String[] CatagoryName = {"A","B","C","D"};
ArrayAdapter<CharSequence> langAdapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.your_layout, CatagoryName);
langAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown);
spinner.setAdapter(langAdapter);

android - how to set and get value from spinner

I am trying to set and get value to a spinner for item dynamically ?
any ideas ?
I just need help with the Spinner behavior right now, the rest should be quite easy.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
1
2
3
XML file:
<Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);
}
// To get value from spenner
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView parent) {
}
});
}
Try this this will help you.
Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(myAdapter);
If you want to add the elements dynamically, you can by doing this:
myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();
Spinner spinn = findViewById(R.id.socialmedia_spinner_adsocmeda);
ArrayList<String> fam = new ArrayList<>();
fam.add("INSTAGRAM");
fam.add("FACEBOOK");
fam.add("GOODWALL");
fam.add("TWITTER");
fam.add("YOUTUBE");
fam.add("LINKEDIN");
fam.add("SNAPCHAT");
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (ProfileActivity.this,
android.R.layout.simple_list_item_1, fam.toArray(new String[0]));
spinn.setAdapter(myAdapter);
That's a way we can add data to spinner.

Default text shown on spinner but hide from drop down list - Android

I want to show first item as a default text on spinner but in the drop down list that item is not shown like there is arraylist
final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("number");
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
The number is always shown as a default text on spinner but after clicking on spinner there is one, two, three is shown in drop down list. when we select any number one, two three the default text number is not changed. How can i do it?
ArrayAdapter<String> adapte = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapte.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapte);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this, it show the default as Number..
May be this help you
String[] priceString = { "Number", "one, "two"};
Spinner priceSpinner = (Spinner) findViewById(R.id.price_spinner);
ArrayAdapter priceAdapter = new ArrayAdapter( this.getActivity(), android.R.layout.simple_spinner_item,priceString); priceSpinner.setAdapter(priceAdapter);

Android fill spinner dynamically

if I have two spinner and add to first spinner as:
int pos=0;
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
pos=position;
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
And now I want to fill the second spinner Based on the position of first spinner How do I do that?
e.g.
if pos->0 second spinner Contains {A,B,C}
if pos->1 second spinner Contains {D,E,F}
... etc.
thanks in advance...
In onItemSelected(...), change the adapter of the second spinner based on the position of the first. You should be able to reuse some of your code to accomplish this.

Create a spinner programmatically in 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);
}
}

Categories

Resources