Android Spinner Not Working - android

I'm trying to implement an Android spinner in my app but am running into some problems with the java file associated with the Activity its placed in.
The errors I am getting are as follows:
1. setContentView(R.layout.activity_display_asleep_at);
- cannot be resolved or is not a field
2. cycles_array (where ArrayAdapter<CharSequence> is...)
- cannot be resolved or is not a field
3. spinner (where "android.widget.Spinner spinner" is...)
- cannot be resolved or is not a field
The code I have is as follows:
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.Spinner;
public class DisplayAsleepAt extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_asleep_at);
getActionBar().setDisplayHomeAsUpEnabled(true);
android.widget.Spinner spinner = (android.widget.Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.cycles_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Here is the xml file I am using:
<Spinner
android:id="#+id/AA_spinner_prompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/AA_spinner_promptTXT"
android:layout_alignParentRight="true" />
And here is the strings.xml file I have the array in:
<string-array name="cycles_array">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
If anyone has any ideas as to what might have gone wrong, it would be greatly appreciated. I can always post more if you need. Thanks all.
EDIT:
Here is the entire xml file I am using. I've been looking through it for a while and can't seem to find anything wrong with it...maybe I'm just tired at this point...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/AA_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AA_titleTXT"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:textSize="35dp"
android:textStyle="italic"
tools:context=".DisplayAsleepAt" />
<TextView
android:id="#+id/AA_intro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AA_introTXT"
android:textSize="15dp"
android:layout_below="#id/AA_title" />
<Button
android:id="#+id/AA_button_pick"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/AA_button_pickTXT"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:onClick="showTimePickerFragment" />
<TextView
android:id="#+id/AA_rem_cycles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AA_rem_cyclesTXT"
android:textSize="15dp"
android:layout_marginBottom="20dp"
android:layout_below="#id/AA_button_pick" />
<Spinner
android:id="#+id/AA_spinner_prompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/AA_spinner_promptTXT" />
<TextView
android:id="#+id/AA_final_time"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="#string/AA_final_timeTXT"
android:textSize="15dp" />
<Button
android:id="#+id/AA_create_alarm_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/AA_create_alarm_buttonTXT"
android:layout_centerHorizontal="true" />
</RelativeLayout>

The error means there's something syntactically wrong with your XML layouts and R cannot be auto-generated. Try looking at your xml code and if you still have problem then post your xml code.
Change this code
android.widget.Spinner spinner = (android.widget.Spinner) findViewById(R.id.spinner);
To
android.widget.Spinner spinner = (android.widget.Spinner) findViewById(R.id.AA_spinner_prompt);
Hope it helps

Remove this line from your code :
import android.R;
and then Clean & Rebuild your project.

Related

ListView using array adapter and customizing xml file

I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?
This is my MenuPage.java file
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
this is my menu.xml for the java file above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="#drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="#+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
also, I created an xml with the drawables for the images i want to use;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="#drawable/coffee"/>
<item
android:duration="30"
android:drawable="#drawable/steak"/>
<item
android:duration="30"
android:drawable="#drawable/icecream"/>
and my logcat gives this error
any help would be appreciated! thank you
I think the problem in the menu layout file
android:text="#+id/values"
try to write some text or specify link to string resource like
android:text="#string/my_button_text"
And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here
ListActivity

AutocompleteTextView filled with Google Search results

I need to have a TextView that, when the user type in it a word, a dropdown list box is be shown exactly with the same phrases that Google Search will suggest if those words are typed in the search textbox of google.com site.
Is it possible to have this service. How?
I searched on the web with no luck.
Thank you all.
For the dropdown you can use:
Autocomplete Android
For show result you must call your own web service or cached data. If you need google results i don't know if google expose a public API to obtain it...
Hi Geltrude here i added the sample source code for you. Please try and let me know. Hope it should helpful for you. Thanks.
First create autocomplete_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:ems="10"
android:hint="Search Name"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:singleLine="true" />
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
AutocompleteSample.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutocompleteSample extends Activity {
private static AutoCompleteTextView autoCompleteText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_textview);
autoCompleteText = (AutoCompleteTextView)findViewById(R.id.autoCompleteText);
List<String> nameList = new ArrayList<String>();
nameList.add("Steve");
nameList.add("Bill");
nameList.add("Tom");
nameList.add("Arnold");
nameList.add("Vishva");
nameList.add("Durai");
nameList.add("John");
nameList.add("Sam");
nameList.add("Johnson");
nameList.add("Hentry");
nameList.add("Thamos");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, nameList);
autoCompleteText.setAdapter(dataAdapter);
}
}

The spinner menu shows very long text, but not the triangle image

When I make the spinner with very long text, the spinner button (not drop down menu), which usually shows the selected text and the triangle image(▼), doesn't show the triangle image.
I tested with normal (android.R.layout.simple_spinner_item) layout and multiline layout.
But neither did.
The right side of spinner button is the outside of the window.
(Sorry, I can't attach the image because I'm a new user.)
The sources are following;
SpinnerTestActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class SpinnerTestActivity extends Activity {
String[] mStrings = {"verrrrrrrrrry looooooooooooooong",
"abc loooooooooooooooooooooooooong",
"xyz"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mStrings);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.multiline_spinner_item, mStrings);
spinner.setAdapter(adapter);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
multiline_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
Thank you for your observations on my question.
I found the answer.
add
spinner.getLayoutParams().width = spinner.getWidth();
after
spinner.setAdapter(adapter);
Have you defined the android:prompt attribute in your Android Spinner controller?
It will help you. Please see the following code:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/planet_prompt"
/>
You can refer to the following link: Can an Android AsyncTask doInBackground be synchronized to serialize the task execution?

Getting View cannot be resolved to a type error on function

I am new to Android/java programming. I really have no clue what I am doing, and would like a little help. What I am trying to do is build a form very similar to the one below. I have already put in all of the layout, and strings information, but I am having trouble with the functions. The error that I am getting is View cannot be resolved to a type line 14. I am using eclipse to help me program this app. Here is my main.xml, and MadisonStudios.java file's contents as well.
http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-forms/
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome" />
<EditText
android:id="#+id/EditTextName"
android:layout_height="wrap_content"
android:hint="#string/name"
android:inputType="textPersonName"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<Spinner
android:id="#+id/SpinnerStatus"
android:layout_height="wrap_content"
android:prompt="#string/status"
android:layout_width="fill_parent"
android:entries="#array/statuslist">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/changebody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/ButtonSendChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendChange"
android:text="#string/changebutton">
</Button>
</LinearLayout>
MadisonStudios.java
package com.madisonstudios.supportapp;
import android.app.Activity;
import android.os.Bundle;
public class MadisonStudios extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendChange(View button) {
// Do click handling here
}
}
import android.view.View;
Add this line in import statements..
You should import it. If you are using Eclipse, press Ctrl+Shift+o.

Android dynamically change checkbox problem

I'm trying to change my checkbox dynamically.
for some reason the app throw my an exception.
what am I doing wrong?
this is my java code:
package com.school.commtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
public class OtherPlacesMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox c1 = (CheckBox)this.findViewById(R.id.schools);
c1.setChecked(true);
setContentView(R.layout.otherplacesmenu);
}
}
and this is the xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/table">
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow1">
<CheckBox android:text="School" android:id="#+id/schools" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow2">
<CheckBox android:text="Supermaker" android:layout_height="wrap_content" android:id="#+id/supermarkets" android:layout_width="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow3">
<CheckBox android:text="Gardens" android:layout_height="wrap_content" android:id="#+id/gardens" android:layout_width="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
I'm sure its a simple question, but I cannot find the answer anywhere.
p.s.
I can't set the value of the checkbox to be true at the xml because I would later want to change the checkbox from the user setting.
10x!
Usually you should post the exception message when asking a question like this, but it looks like your problem is that you're trying to findViewById before you setContentView. Your view doesn't exist to be found when you look for it.

Categories

Resources