rb1=(RadioButton)findViewById(R.id.radioButton1);
rb2=(RadioButton)findViewById(R.id.radioButton2);
rb3=(RadioButton)findViewById(R.id.radioButton3);
rb4=(RadioButton)findViewById(R.id.radioButton4);
rb5=(RadioButton)findViewById(R.id.radioButton5);
spiner1=(Spinner)findViewById(R.id.spinner1);
spiner2=(Spinner)findViewById(R.id.spinner1);
spiner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
This is my code there are 5 value in my spiner if i select first one then rb1,rb2,rb3,rb5 shud display and rest disappear if i select 2,3,4,5 value from spinner then it hud display 1st,4,5 plz tell me how to write code for this i m new in android try to implement some apps. i need code where and how to apply logic for this.
There are some demos about spinner
Here is an example.
you can set dropdown styles in this method
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Based on your condition or logic you can show/hide buttons.
rb1.setVisibility(View.VISIBLE); // show
rb2.setVisibility(View.INVISIBLE); // Hide
You can do this :
spiner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adView, View view,int pos, long id) {
selectedoption = adapter.get(pos);
if(selectedoption == rb1)
{
rb1.setVisibility(View.INVISIBLE);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Related
I've been trying to set an ImageView (Default empty) based on the value selected from a Spinner.
So in the mainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp_home = (Spinner) findViewById(R.id.spinner_home_team);
sp_away = (Spinner) findViewById(R.id.spinner_away_team);
sp_home.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (!sp_home.getSelectedItem().toString().equals("Seleziona Squadra")) {
setTeamLogo(sp_home.getSelectedItem().toString(), "home");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp_away.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (!sp_away.getSelectedItem().toString().equals("Seleziona Squadra")) {
setTeamLogo(sp_away.getSelectedItem().toString(), "away");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
The setOnItemSelectedListener method works perfectly the problem comes when I execute the code inside setTeamLogo.
private void setTeamLogo(String teamName, String home_or_away_team){
int resId = getResources().getIdentifier(teamName, "drawable", getPackageName());
Toast.makeText(getBaseContext(), resId,
Toast.LENGTH_LONG).show();
}
The exception that has been thrown is:
android.content.res.Resources$NotFoundException: String resource ID #0x0
Any idea why if I use the name of the image instead of the variable teamName everything works perfectly?
Use String.valueOf()
If you pass it an integer it will try to look for the corresponding string resource id - which it can't find, which is your error.
check out this post:-
android.content.res.Resources$NotFoundException: String resource ID #0x0
I created a spinner in my main.xml:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/categoria_arrays"
android:prompt="#string/categoria_prompt" />
And in the strings.xml the values:
<string name="categoria_prompt">Choose</string>
<string-array name="categoria_arrays">
<item>All</item>
<item>One</item>
<item>Two</item>
<item>Three</item>
</string-array>
I can display it normally but actually there are no interactions.. I need that onClick over a item open a new activity for example. So if i click the item at position 2 i need go in the activity One. Is it possible?
I tried to create a toast when i click a item but not works:
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
I How could i do?
Simply call something like this in your onCreate after your setContentView:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes it is possible. Do it like this
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
if (pos == 1){
Intent i = new Intent(currentActivity.this, One.class);
startActivity(i);
}else if (pos == 2)
{
Intent i = new Intent(currentActivity.this, Two.class);
startActivity(i);
}else if (pos == 3){
Intent i = new Intent(currentActivity.this, Three.class);
startActivity(i);
}
}
and so on...
You need to set item selected listener like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
You need to explicitly set the listener for the spinner.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
// your code here
}
public void onNothingSelected(AdapterView<?> arg0) {
// your code here
}
});
Did you just added onItemSelected method, or setted onItemSelectListener? onItemSelected and onNothingSelected are just methods, you need to use onItemSelectListener.
I am trying to attach an OnClickListener on the child elements in a custom list view.The class that I am using extends ListActivity. In my custom list view I have two text views and a button. I need to attach an OnClickListener on each of them. The following is my code.
final ListView listview = getListView();
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
callBut= (Button)listview.getChildAt(position);
callBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Call Phone","");
}
});
}
But when I click on the child element Nothing is shown on the log.
You can atach a OnItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
change
callBut= (Button)listview.getChildAt(position);
to
callBut= (Button)findViewById(R.id.yourCallButton);
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// my code here, but I don't know how..
}
}
I think it will be something like this:
get the selected item position of mySpinner
switch(pos)
Update spinner entries? I don't know...
I'm not sure what to do for the last step. Help?
try in this way
car.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
itemSelected = arg2;
// add items to the cardetails spinner's adapter using the itemselected and refresh the adapter using nofifyDataSetChanged()
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
cardetails.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
here car and cardetails are spinners
you need to refresh the adapter that is related to the car spinner so that the spinner also gets refreshed
I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance
IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.
private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}
you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?