Android spinner and editText - android

my idea is to have a text field where I can input a name manually.
Additionally I do have a list of names. If there is at east 1 name in that list an icon should appear on the right of the EditText in order to open a Spinner to give a selection.
If no item is selected the EditText stays the same and if an item has been selected the EditText will have that item.
I'mworking with visibilities so far - but my problem is that the onNothingSelected seems not to get fired at all.
public class MainActivity extends ActionBarActivity {
private Spinner spinner;
private EditText text2;
private ImageButton button2;
private String spinner_value = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
String[] str = {"","good", "dislike", "like", "hate", "moderate"};
spinner.setPrompt("Set Text");
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, str);
spinner.setAdapter(list);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView tv = (TextView)arg1;
spinner_value = tv.getText().toString();
if(spinner_value.length() == 0)
{
spinner_value = "Nothing";
}
Toast.makeText(getApplicationContext(), spinner_value, Toast.LENGTH_SHORT).show();
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (EditText) findViewById(R.id.editText);
button2 = (ImageButton) findViewById(R.id.imageButton);
text2.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
String myStr = spinner.getSelectedItem().toString();
text2.setText(myStr);
text2.requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(), "NothingSelected", Toast.LENGTH_SHORT).show();
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (EditText) findViewById(R.id.editText);
button2 = (ImageButton) findViewById(R.id.imageButton);
text2.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
text2.requestFocus();
}
});
}
}

By default, the item at index 0 is selected when you spinner is created. So, even though the user doesn't actually do anything, the first item is selected. You can see this happen by setting a breakpoint (or some other notification) in onItemSelected() and seeing it get triggered on startup without the user doing anything.
For the onNothingSelected() callback to get triggered, whatever item is selected has to disappear.

Related

Change colour of selected items

I made a listview and a spinner with 3 options. I want that spinner to connect with Listview on other intent.
The spinner is on a Edit page so whenever I click on 1 item on the list I get to the edit page.
I want the first option on the spinner to make the selected item on the list green the second will make it yellow and then third will make it red. How do I do it?
public class EditList extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_list);
final Button button=(Button)findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(EditList.this,Vans.class);
startActivity(intent);
}
});
// Spinner element
spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(EditList.this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("רכב עובד");
categories.add("יש תקלה");
categories.add("רכב במוסך");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String work = parent.getItemAtPosition(position).toString();
if (position==0){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.GREEN);
}
Vans.listView.setBackgroundColor(position);
spinner.setSelection(position);
String takala = parent.getItemAtPosition(position).toString();
if (position==1){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.YELLOW);
}
String garage = parent.getItemAtPosition(position).toString();
if (position==2){
View listViewChildAt = Vans.listView.getChildAt(position);
listViewChildAt.setBackgroundColor(Color.RED);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

get spinner position and use it in button onclick

I am trying to make app with one text view, one button and one spinner. I make button work showing random values from string array, but i have a lot different string arrays(list1, list2 etc). Now my question is when change spinner position, how to get button get another string array(from spinner) and show it to textview.
How to pass value from spinner when something is selected in spinner to button.
Any help is appreciated.
Here is my code:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Button btn;
public String[] myString,myString1;
public static final Random rgenerator = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textview
final TextView tv = (TextView) findViewById(R.id.textureView);
final Resources res = getResources();
//string
myString = res.getStringArray(R.array.World_War_I);
myString1 = res.getStringArray(R.array.World_War_II);
//button
btn = (Button) findViewById(R.id.buttonxx);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
if (==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
//drop list
Spinner spinner = (Spinner) findViewById(R.id.spinnerrrr);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.kategorije, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Use this
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
btn.setTag(pos+""); // Passing as string
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
and then
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
int index = Integer.parseInt(btn.getTag().toString());
if (index==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (index==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});

how to add items in edit text when i click on spinner?

In my application i have used spinner and edit text.i have created an XML file and set it as background to the spinner which looks like a drop down arrow.When i click the spinner the items selected from spinner should be set in edit text but it displays in edit text as well spinner as the below image. can any one help me with this??
public class newcard extends Activity {
Spinner spinner;
Button btn;
EditText ed,ed1,ed2;
List<String> list;
private String[] countries_list={"01/2014","02/2014","03/2014","04/2014"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnewcard);
btn= (Button) findViewById(R.id.button1);
spinner = (Spinner) findViewById(R.id.spinner2);
ed = (EditText) findViewById(R.id.editText3);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
list = new ArrayList<String>();
list.add(" ");
list.add("select");
list.add("01/2014");
list.add("02/2014");
list.add("03/2014");
list.add("04/2014");
ArrayAdapter<String> adp = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, list);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
switch(arg2) {
case 0 :
ed.setText("Select");
break;
case 1 :
ed.setText("01/2014");
break;
case 2 :
ed.setText("02/2014");
break;
case 3 :
ed.setText("04/2014");
break;
default :
ed.setText("Nothing");
break;
}
}
//#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
For setting spinner value to ""
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3)
{
item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setText("");
}

calling a button or a textfield from another class | android

i have tried to use the edittext in the list select listener but it can't see it from the main activity
taking me to error at this
edt.setVisibility(View.VISIBLE);
so please if you have anything around help me
public class MainActivity extends Activity {
static int HELLO_ID = 1;
boolean flag = false;
CountDownTimer timer;
//the strings of the notifications
String titlePills = "Time to take Panadol",DescriptionPills = "Panadol";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new SpinnerActivity());
Chronometer test = new Chronometer(this);
Button btn = (Button)findViewById(R.id.button1);
EditText edt = (EditText)findViewById(R.id.editText1);
//hide the button and the edit text
btn.setVisibility(View.GONE);
edt.setVisibility(View.GONE);
}
//*********************************************************************************
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
public void onItemSelected(final AdapterView<?> parent, View view,
int pos, long id) {
String select = parent.getItemAtPosition(pos).toString();
if(select.equals("Type the timee"))
{
//edt error can't be resolved
edt.setVisibility(View.VISIBLE);
}
Try this..
EditText edt;
as globle variable
edt = (EditText)findViewById(R.id.editText1);

Two spinners in android app

I have problem with my spinners,
in my app are two spinners which gets data from database,
when i choose one option from first spinner, i want to remove this option from second spinner,
and add option which i removed from second spinner to first spinner and vice versa.
How can i do that ?
This is my present code:
public void showScoresCalculator() {
setContentView(R.layout.scores_calculator);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/HelveticaProCn.ttf");
TextView txt = (TextView) findViewById(R.id.txt);
txt.setTypeface(font);
TextView team = (TextView) findViewById(R.id.choose_team);
team.setTypeface(font);
TextView group = (TextView) findViewById(R.id.choose_group);
group.setTypeface(font);
TextView title = (TextView) findViewById(R.id.textView1);
title.setTypeface(font);
TextView title2 = (TextView) findViewById(R.id.textView2);
title2.setTypeface(font);
final Spinner sp = (Spinner) findViewById(R.id.spinner3);
final Spinner sp1 = (Spinner) findViewById(R.id.spinner4);
final DataBaseHelper.MyData[] items = this.dh.getTeamsNameAndIDs("38");
final DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
final DataBaseHelper.MyData[] items2 = this.dh
.getTeamsNameAndIDs("47");
final DataBaseHelper.MyData d1 = items2[(int) sp1.getSelectedItemId()];
final ArrayAdapter<DataBaseHelper.MyData> adapter = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("38"));
final ArrayAdapter<DataBaseHelper.MyData> adapter1 = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("47"));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items2[(int) sp1.getSelectedItemId()];
DataBaseHelper.MyData d1 = items[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d1.getValue(), d.getValue(), d1.getSpinnerText(), d.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
DataBaseHelper.MyData d1 = items2[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d.getValue(), d1.getValue(), d.getSpinnerText(), d1.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Button next = (Button) findViewById(R.id.next);
final Button prev = (Button) findViewById(R.id.prev);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showNewsPage();
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showGroupCalculator();
}
});
Page = true;
}
Please, help me ! :)
Update the adapter by removing the selected data from the list. Use Handler concept to update adapter.
Set a new adapter on the Spinner via setAdapter(). Or, if it is an ArrayAdapter, use add(), insert(), and remove() to modify the data. Or, if it is a CursorAdapter, requery() it to get fresh data.

Categories

Resources