How to change text on spinner selection - java

I have a couple of textviews with a couple of spinners lined up in a table. There are 4 choices in the spinners, all of them the same, the first choice being '--'. I want the textview next to the spinner to be red when the default option '--' is selected, and white otherwise.
I coded this to handle this but when you select one spinner's different option, all of the textviews change It's behavior is just really strange.
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch(parent.getId()) {
case R.id.spinner1:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView1.setTextColor(Color.RED);}else{textView1.setTextColor(Color.WHITE);}
case R.id.spinner2:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView2.setTextColor(Color.RED);}else{textView2.setTextColor(Color.WHITE);}
case R.id.spinner3:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView3.setTextColor(Color.RED);}else{textView3.setTextColor(Color.WHITE);}
}
}

You need to put a
break;
at the end of each of your cases.
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch(parent.getId()) {
case R.id.spinner1:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView1.setTextColor(Color.RED);}else{textView1.setTextColor(Color.WHITE);}
break;
case R.id.spinner2:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView2.setTextColor(Color.RED);}else{textView2.setTextColor(Color.WHITE);}
break;
case R.id.spinner3:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView3.setTextColor(Color.RED);}else{textView3.setTextColor(Color.WHITE);}
break;
}
}

You've forgotten "break" in your case blocks:
case R.id.spinner1:
if(parent.getItemAtPosition(pos).toString().equals("--")){
textView1.setTextColor(Color.RED);
} else {
textView1.setTextColor(Color.WHITE);
}
break;
and so on.

Related

How to set the spinner position to selected item from choice selected in different fragment

Add color spinner
the update fragment
this is my first time asking a question and I am a new coder and been trying out android with kotlin and java. I have an app that allows a user to add an item of clothing the options are set up with spinners. The issue is when the user selects an item from the spinner it saves and when I want to update the values like the image shows the blue words saying brown is the option selected form the add clothing item spinner but the spinner is showing grey is there a way I can set the spinner to be on brown and not the default value.
Yes. You need to use the getItemAtPosition() method. From your user interface, the Object you are trying to retrieve is a String. So if you wrote your code in Java. Knowing you've used a TextView to display the selection
Java
TextView clotheColor = findViewById(R.id.whatever_the_id_is);
String selectedColor = "";
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedColor = parent.getItemAtPosition(position).toString();
Log.i(TAG, selectedColor);
switch (selectedSpinnerItem) {
case "Brown":
case "Red":
case "Blue":
clotheColor.SetText(selectedColor);
break;
default:
Log.i(TAG, "");
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i(TAG, "Nothing has been selected");
}
});
KOTLIN
You can convert the code to Kotlin from here. Read this.
How can I convert a part of Java source file to Kotlin?

How can I set different functions according to items selected on Listview?

what I want to do looks easy but I can not handle with that.
I have a google spreadsheet that I am using for recording reservations of a hotel.
In the code there is checkIn() method and that method needs to perceive/understand the item=person clicked wants to check in at the hotel.
For example on the listview when I clicked the 3rd item/person, that click receives data from googlescript spesific for the 3rd row on googlesheet.
In that way 3rd row will be cut both on app and on google spread sheet and
paste into another sheet.
I hope I could describe what I wanted to do.
Thanks in advance
Happy codding;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
break;
case 1:
break;
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
checkIn();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
private void checkIn() {
//StringRequest strq = new StringRequest(Request.Method.POST"")
}
};
final AlertDialog.Builder builder = new AlertDialog.Builder(Reservations.this);
Here what my listview looks like

How to get where is my finger, over of which layout?

I have a block, I drag it somewhere then this cell transform to yellow then I exitted this cell, It transforms again gray but, When I drop it somewhere Everywhere is magenta, How can I paint only that block When I drop it, You can check pictures to understand my problem
Best regards
public boolean onDrag(View v,DragEvent event)
{
int action=event.getAction();
switch (action)
{
case DragEvent.ACTION_DRAG_STARTED:
Log.i("Script",num+"-ACTION_DRAG_STARTED");
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
{
return (true);
}
return (false);
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Script",num+"-ACTION_DRAG_ENTERED");
v.setBackgroundColor(Color.YELLOW);
break;
case DragEvent.ACTION_DRAG_LOCATION:
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.GRAY);
break;
case DragEvent.ACTION_DROP:
View view=(View) event.getLocalState();
ViewGroup owner=(ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container=(LinearLayout) v;
container.addView(view);
view.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundColor(Color.MAGENTA);
break;
}
return true;
}
In DragEvent.ACTION_DRAG_ENDED event you should find view.getX() and view.getY(). This will return x and y co-ordinate in pixels then according to your requirement you should calculate nearest view which occupy this blocks and change its color to MAGENTA.

two ListView not working on NavigationDrawer

I have two ListView in my NavigationDrawer, both of them is displayed on my NavigationDrawer, but I face a problem, when user want to click an item from one ListView, it does't work, this is how I call setOnItemClickListener from my ListView:
list_terbaru=(ListView)findViewById(R.id.list_terbaru);
list_terkirim=(ListView)findViewById(R.id.list_terkirim);
list_terbaru.setOnItemClickListener(new SlideMenuClickListener());
list_terkirim.setOnItemClickListener(new SlideMenuClickListener());
private class SlideMenuClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
switch (v.getId()) {
case R.id.list_terbaru:
id=((TextView)v.findViewById(R.id.nomor_registrasi)).getText().toString();
a=id;
ModelHelper.onModel(me);
getFragmentManager().beginTransaction().replace(R.id.frame_container,new PemegangPolis()).commit();
selectItem(position);
break;
case R.id.list_terkirim:
id=((TextView)v.findViewById(R.id.no_polis)).getText().toString();
a="";
ModelHelper.onModel(me);
getFragmentManager().beginTransaction().replace(R.id.frame_container,new PemegangPolis()).commit();
selectItembaru(position);
}
}
}
private void selectItem(int position) {
ModelHelper.onModel(me);
ModelHelper.onRetrieve(helper,id,me,position,model,SelectPemegang,SelectTertanggung,SelectUA,SelectDi,SelectDA,SelectDP);
getFragmentManager().beginTransaction().replace(R.id.frame_container,new PemegangPolis()).commit();
// helper.close();selectItem
mDrawerLayout.closeDrawer(SlidingMenu);
}
private void selectItembaru(int position) {
ModelHelper.onModel(me);
ModelHelper.onRetrieve(helper,id,me,position,model2,SelectPemegang,SelectTertanggung,SelectUA,SelectDi,SelectDA,SelectDP);
getFragmentManager().beginTransaction().replace(R.id.frame_container,new PemegangPolis()).commit();
// helper.close();selectItem
mDrawerLayout.closeDrawer(SlidingMenu);
}
so my question, is it possible to call 'onItemClick' with switch (v.getId())? and if it is possible, can anyone help me to show where is fault? and how I can solve my problem? thank you.
R.id.list_terbaru and R.id.list_terkirim are the ids of the ListViews. However v is not the ListView, but the View of the item. Switch on the id of the AdapterView (which is one of your ListViews in this case) instead:
switch (arg0.getId()) {
If there is no other error in the code (could not find one), this should solve the problem.
But have you considered creating different listeners for the lists? That way you wouldn't have to switch at all.

Positioning in drag and drop layout

i have a few problem here regarding the drag and drop. I already create the code for drag and drop, its working finely. But here's the problem, as u can see in the images below each jar can fits only 4 sweets, but when i drag sweets from the second jar to the first one, it can still be drop into it. How can i make each jar can only be fixed with four sweets?
This is the coding that I've done for the drag and drop. Should I use the if else statement somewhere in this coding to solve my problem?
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackgroundDrawable(normalShape);
if (dropEventNotHandled(event))
{
view.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
private boolean dropEventNotHandled(DragEvent event) {
// TODO Auto-generated method stub
return !event.getResult();
}
}
}
inside the case DragEvent.ACTION_DRAG_ENDED block you should be checking to see if theres room in the jar and if not then return false so that the drop action fails.

Categories