I\'m using three spinners inside of my XML file. Want to change spinner color until press the next spinner.
This is my xml I used:
You can change mybg.xml as below.
-
-
If want to show the arrow (">"). You can change your file mybg.xml as below. The nine-patch file can be found in /Android/android-sdks/plataforms//data/res/spinner_default_holo_light.9.png. Copy this to your drawable folder.
File res/drawable/mybg.xml
-
-
File res/layout/activity_main
File MainActivity.java
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Spinner;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner sp1, sp2, sp3;
sp1 = (Spinner)findViewById(R.id.spinner1);
sp2 = (Spinner)findViewById(R.id.spinner2);
sp3 = (Spinner)findViewById(R.id.spinner3);
Drawable d = sp1.getBackground();
sp1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sp1.setBackgroundResource(R.drawable.mybg);
sp2.setBackgroundResource(R.drawable.spinner_default_holo_light);
sp3.setBackgroundResource(R.drawable.spinner_default_holo_light);
return false;
}
});
sp2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sp1.setBackgroundResource(R.drawable.spinner_default_holo_light);
sp2.setBackgroundResource(R.drawable.mybg);
sp3.setBackgroundResource(R.drawable.spinner_default_holo_light);
return false;
}
});
sp3.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sp1.setBackgroundResource(R.drawable.spinner_default_holo_light);
sp2.setBackgroundResource(R.drawable.spinner_default_holo_light);
sp3.setBackgroundResource(R.drawable.mybg);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}