Spinner not working (Android) - android

I have made a simple demo for spinners in android with one activity. I have tried the following code but it's not working. when I run this app on my phone it shows:- "Unfortunately the app has stopped working "
MainActivity.Java
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemClickListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=findViewById(R.id.spinner);
ArrayAdapter Adapter=ArrayAdapter.createFromResource
(this,R.array.days,R.layout.support_simp
le_spinner_dropdown_item);
spinner.setAdapter(Adapter);
spinner.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long
l) {
TextView mytext=(TextView)view;
Toast.makeText(this,"you selected"+mytext.getText(),
Toast.LENGTH_SHORT).show();
}
}
XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rakib.myapplication.MainActivity">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp">
</Spinner>

use this
String[] myarray =getResources().getStringArray(R.array.days);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.support_simp le_spinner_dropdown_item, myarray);
(or)
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,
myarray); //selected item will look like a spinner set from XML
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Use this code to get values from the spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(this,"you selected"+parent.getItemAtPosition(pos),Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

Related

How to show Spinner-List when we tapped on button in android?

Hi I recently came to the android technology and in my app I have two buttons(one for showing movies-list and another one for showing countrieslist)
When I tap on the second button I want to display movies-list in spinner-list as in the first image bellow.
But according to my code, when I tap on the button first spinner is appearing and I selected any one item and set that to my button title but after selection spinner still visible B/w two button as like my second screen how can remove it.
How can I resolve this problem?
Please help me.
I want to show directly spinner-list when I tap on button.
xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button1Action"
android:text="CountiesList"/>
<Spinner
android:visibility="gone"
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mainLayout"
android:layout_alignParentRight="true"
android:paddingRight="0dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button2Action"
android:text="MoviesList"/>
<Spinner
android:visibility="gone"
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mainLayout"
android:layout_alignParentRight="true"
android:paddingRight="0dp"
android:layout_marginTop="5dp"
/>
</LinearLayout>
activity:-
public class spinnerListProgramatically extends AppCompatActivity{
String [] countriesList = {"india","usa","england"
};
String [] moviesList = {"fury","300 rise of an empire","troy"
};
Spinner spinner1,spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerlist_runtime);
}
public void Button1Action(View view){
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setVisibility(View.VISIBLE);
}
public void Button2Action(View view){
spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, moviesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setVisibility(View.VISIBLE);
}
}
---
Why not remove Spinner and have just a Button instead. Having a Spinner and Button both, makes no sense.
As per the image you have shown, you require PopupMenu:
public void Button2Action(View view){
showFilterPopup(view);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
// Inflate the menu from xml
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
// Setup menu item selection
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.troy:
Toast.makeText(MainActivity.this, "troy", Toast.LENGTH_SHORT).show();
return true;
case R.id.rise:
Toast.makeText(MainActivity.this, "300 Rise of Empire", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
// Handle dismissal with: popup.setOnDismissListener(...);
// Show the menu
popup.show();
}
Your R.menu.popup will contain every item you need.
This approach will be useful when you have static set of data.
With few more steps you can make it dynamic. Hope this helps.
Use this it work
public void Button1Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setVisibility(View.INVISIBLE);
spinner1.performClick();
}
public void Button2Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, moviesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setVisibility(View.INVISIBLE);
spinner2.performClick();
}
View this Screen
Can set this as the screen Display.
OK, Finally I knew what you really meant to say after a long discussion. I have provided you full source codes which are optimized and modified a little bits from yours.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_marginTop="50dp"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button1Action"
android:text="CountiesList"/>
<Spinner
android:visibility="invisible"
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<Spinner
android:visibility="gone"
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Button2Action"
android:text="MoviesList"/>
</LinearLayout>
Your Activity
public class spinnerListProgramatically extends AppCompatActivity {
Button button1, button2;
String [] countriesList = {"NONE","india","usa","england"};
String [] moviesList = {"NONE","fury","300 rise of an empire","troy" };
Spinner spinner1,spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
//Your other setup codes
spinner1 = (Spinner)findViewById(R.id.spinner1);
spinner2 = (Spinner)findViewById(R.id.spinner2);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
}
public void Button1Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setVisibility(View.VISIBLE);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position!=0) {
button1.setText(countriesList[position]);
spinner1.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void Button2Action(View view){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, moviesList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setVisibility(View.VISIBLE);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position!=0) {
button2.setText(moviesList[position]);
spinner2.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
show Spinner-List item without tap on spinner
Spinner spi_type = findViewById(R.id.spi_type);
Button button = findViewById(R.id.button);
imgVIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// performClick() this method show Spinner-List item without tap on spinner
spi_type.performClick();
}
});
cant comment cause am not having that much reputation.. can u be more specific of what you want to achieve??
The problem was that
if(spinner2.getSelectedItem() == null)
is not null.. it by default tasks the first value
so use this code instead
if(spinner2.getSelectedItem() == "fury") {
// for checking user havent selected anything
spinner2.performClick();
}
if(spinner1.getSelectedItem() == "india") {
// for checking user havent selected anything
spinner1.performClick();
}
and for setting the title for button
public void Button1Action(View view){
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesList);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
spinner1.setVisibility(View.VISIBLE);
String s = (String) spinner1.getSelectedItem();
spinner1.performClick();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
button.setText(""+ spinner1.getSelectedItem());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
in the button onCLick
and also intialize the buttons on onCreate
button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
and placing adaptor is not an issue..
find spinner and set adapetr before Button onClick method

Setting color for textview dynamically using spinner?

I want to create one textview, the colour of that text is to be changed by the user by selecting colour from spinner object.
The spinner object contains list of colours, that is options for user to change the colour of textview dynamically.
Excuse for my english and help me out..
Try this way,hope this will help you to solve your problem.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>
strings.xml
<array name="colorname">
<item>Red</item>
<item>Yellow</item>
<item>Green</item>
<item>Blue</item>
<item>Pink</item>
</array>
<array name="colorcode">
<item>#FF0000</item>
<item>#ffff00</item>
<item>#00ff00</item>
<item>#0000ff</item>
<item>#FF0080</item>
</array>
MainActivity.java
public class MainActivity extends Activity {
private Spinner spinner;
private TextView textview;
private ArrayList<String> colorNameList;
private ArrayList<String> colorCodeList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textview);
spinner = (Spinner) findViewById(R.id.spinner);
colorNameList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.colorname)));
colorCodeList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.colorcode)));
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, colorNameList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setTextColor(Color.parseColor(colorCodeList.get(position)));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
YourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
YourTextView.setTextColor(getResources().getColor(R.color.some_color));
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
It would be nice if you've tried searching on Stack Site.
public class MainActivity extends ActionBarActivity {
private TextView text;
private Spinner spin;
ArrayList al;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.textView1);
spin=(Spinner)findViewById(R.id.spinner1);
al=new ArrayList();
al.add("RED");
al.add("BLUE");
ArrayAdapter adapter=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_activated_1,al);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch(arg2)
{
case 0:
text.setText("srikanth");
text.setTextColor(Color.RED);
break;
case 1:
text.setText("SRIkANTH BLUE");
text.setTextColor(Color.BLUE);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
I hope it helps you

how to show list view record into second listview on setOnItemClickListener

I have created list view, what I want to do is that when user clicks on first list view the selected record should show in second list, in my code it show on text view, but I want to show that record in second list view so please give me the code/idea how to do this as I am new in android..
public class MainActivity extends Activity {
String item[]=new String[]{"rk","kk","kk","ll","mm","uu"};
TextView tv,tv2,tv3,tv4,tv5;
List<TextView> arrayTV = new ArrayList<TextView>();
ListView li,li2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=(ListView)findViewById(R.id.listView1);
li2=(ListView)findViewById(R.id.listView2);
tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.textView5);
arrayTV.add(tv);
arrayTV.add(tv2);
arrayTV.add(tv3);
arrayTV.add(tv4);
arrayTV.add(tv5);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, item);
li.setAdapter(adapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
// TODO Auto-generated method stub
int itm=position;
Toast.makeText(getApplicationContext(),""+itm+""+li.getItemAtPosition(position),40).show();
arrayTV.get(position).setText(""+li.getItemAtPosition(position));
}
});
}
Here is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight=".5" >
</ListView>
set the adapter for second listview in onitemclick of first listview
List<String> a = new ArrayList<String>();
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
String clickedItem =item[postion];
if(!a.contains(clickedItem))
a.add(clickedItem);
String[] newitem = new String[a.size()];
a.toArray(newitem);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1, newitem);
li2.setAdapter(adapter);
}
});

spinner defined in main activity is null

I have populated a spinner with items in xml file using entries option. I have defined the spinner in main activity class as "s1". In debug mode i found that s1 is null. That is why setOnItemSelectedListener is not working for spinner. When an item is selected, toast message is not displayed. I am giving mainactivity.java, activity_main.xml and strings.xml files.
MainActivity.java
public class MainActivity extends ActionBarActivity{
String[] presidents;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
presidents = getResources().getStringArray(R.array.presidents);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.presidents, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(s1 != null){
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), presidents[index], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop= "true"
android:entries="#array/presidents" />
strings.xml
<resources>
<string-array name = "presidents">
<item>eisenhower</item>
<item>kennedy</item>
</string-array>
</resources>
your setContentView(R.layout.activity_main); is wrong, use R.layout.fragment_main instead
You have initialized your Spinner at the class level. At this point, the Spinner object is null. You should instead initialize your Spinner within the onCreate() method. So you can have it like this:
Spinner s1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Only here is the layout inflated and only after this can Android see the Spinner
s1 = (Spinner) findViewById(R.id.spinner);
// Other statements
}
Try this way,hope this will help you to solve your problem.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private Spinner s1;
private String[] presidents;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.spinner1);
presidents = getResources().getStringArray(R.array.presidents);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.presidents, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(s1 != null){
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3)
{
Toast.makeText(MainActivity.this, presidents[arg2], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
}
}
}

Spinner activity not working

I'm trying to create an activity, RateCardActivity, which has a spinner in it. My layout file for RateCardActivity is rate_card. My RateCardActivity looks like the following.
public class RateCardActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.rate_card);
Spinner spinner = (Spinner) findViewById(R.id.select_city);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.select_city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
The layout file rate_card is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.olacabs.customer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="#string/rate_card"
android:textColor="#color/white"
android:textSize="20dp"
custom:customFont="litera_bold.ttf" />
<Spinner
android:id="#+id/select_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The RateCardActivity is called from another activity using an intent (I'm sure there is nothing wrong with that part of the code as when I substitute RateCardActivity with another activity, the application works fine). When I try to open the RateCardActivity in the application in emulator, the application crashes and I got the message "The application has stopped unexpectedly. Please try again later."
I can't seem to understand what I'm doing wrong, and want to know how to correct this?
Improve :public class RateCardActivity extends Activity
and add RateCardActivity to AndroidManifiest.xml
Hi you can use spinner activity by this way, I gave a sample code for the help..
public class MyActivity extends Activity {
public static EditText edtsample;
public static EditText edtchannel;
public static EditText edtencoding;
private static Spinner samplespin;
private static Spinner channelspin;
private static Spinner encodingspin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
edtsample = (EditText)findViewById(R.id.audvalue1);
edtchannel = (EditText)findViewById(R.id.chanvalue1);
edtencoding = (EditText)findViewById(R.id.encodingvalue1);
edtchannel.setFocusable(false);
edtchannel.setClickable(false);
edtencoding.setFocusable(false);
edtencoding.setClickable(false)
samplespin = (Spinner) findViewById(R.id.audspinner1);
samplespin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
edtsample.setText(parent.getItemAtPosition(position).toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
edtsample.setText("");
}
});
channelspin = (Spinner) findViewById(R.id.chanspinner1);
channelspin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(parent.getItemAtPosition(position).equals("CHANNEL_PHONE")){
edtchannel.setText(R.string.chan1);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
if(parent.getItemAtPosition(position).equals("CHANNEL_CD")){
edtchannel.setText(R.string.chan2);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
if(parent.getItemAtPosition(position).equals("CHANNEL_HD")){
edtchannel.setText(R.string.chan2);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
edtchannel.setText("");
}
});
**And in XML part you do by this**
<Spinner
android:id="#+id/audspinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/audioText1"
android:spinnerMode= "dropdown"
android:entries="#array/sample_array" />
<EditText
android:id="#+id/audvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/audspinner1"
android:hint="Enter Sampling Rate"
android:ems="10" >

Categories

Resources