creating scrollable spinner to show some items - android

i used spinner to show items but it only works when the items are less than 5 items, and when i want to show 10 items this error happens : unfortunately stopped :( how can i use scrollable spinner to show all items in the spinner
here is:
spinner.xml :and main
<?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:background="#drawable/shw"
android:orientation="vertical" >
<TextView
android:id="#+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="which"
android:textColor="#ffffff" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/bt1help2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Spinner01"
android:layout_marginRight="100dp"
android:layout_toLeftOf="#+id/txt3"
android:text="ok"
android:textColor="#ffffff" />
<Button
android:id="#+id/btn2help2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="no"
android:textColor="#ffffff" />
</LinearLayout>
<Spinner
android:id="#+id/Spinner01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:verticalScrollbarPosition="left"
android:scrollbarThumbVertical="#drawable/scrollbar_style"
android:layout_below="#+id/linearLayout1"
android:layout_marginTop="22dp" />
final Dialog dl = new Dialog(Main.this);
dl.setContentView(R.layout.spinner);
dl.setTitle("show");
TextView txt2 = (TextView) dl.findViewById(R.id.txt2);
TextView txt3 = (TextView) dl.findViewById(R.id.txt3);
///تنظیم متن
txt2.setText(" which one do you want to show?");
Button btn1help2 = (Button) dl.findViewById(R.id.bt1help2);
btn1help2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String array_spinner[];
array_spinner=new String[5];
array_spinner[0]="letter_one";
array_spinner[1]="letter_2";
array_spinner[2]="letter_3";
array_spinner[3]="letter_4";
array_spinner[4]="letter_5";
array_spinner[5]="letter_6";
array_spinner[6]="letter_7";
array_spinner[7]="letter_8";
array_spinner[8]="letter_9";
array_spinner[9]="letter_10";
Spinner s = (Spinner) dl.findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
how can change it to scrollable spinner?
Thanks for your answers

You can use ScrollView. Put your RelativeLayout inside ScrollView because it just can have one child.
<ScrollView>
<RelativeLayout>
.... other views
</RelativeLayout>
</ScrollView>

your problem is this array_spinner=new String[5]; you set size of 5 items but you put 10 so, you initialize the array this String array_spinner[]; array_spinner=new String[10];

Related

TextView taller than containing TableRow after calling spinner.setdapter() in same row

I have a xml view that I'm inflating. I'm then doing a network call and with the results populating all these spinners. This is what the view looks like before I call setadapter on the spinners.
However, when I call spinner.setadapter(), my last TextView gets cut off.
As you can see, the TextView is larger than the TableRow.
Here is snippet of xml layout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bcast_table">
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_num_retx"
android:id="#+id/num_retx_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/num_retx_spinner" />
</TableRow>
<TableRow style="#style/row">
<CheckBox style="#style/chbx"
android:text="#string/title_cwm0"
android:id="#+id/cwm0_chbx" />
<Spinner style="#style/spinner"
android:id="#+id/cwm0_spinner" />
</TableRow>
</TableLayout>
</LinearLayout>
And here is how I setadapter()
static void populateSpinner(int spinId, ArrayList<String> fields, String currPos, View template, Context context){
Spinner spinner = (Spinner) template.findViewById(spinId);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, fields);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
int position = adapter.getPosition(currPos);
spinner.setSelection(position);
}
Thanks for the help!

Adding TextView(s) via java android but inserting blank views

With help of a suggestion from another answer I wrote the below code. But for some odd reason, it displays the text for the first TextView just fine and then, every other TextView below it is a blank! I tried many different ways but unable to figure it out. Someone please assist me..THANKS!
JAVA code(MainActivity.java)
ToDoListDB info = new ToDoListDB(MainActivity.this);
info.open();
String data = info.getData();
info.close();
//Split the List item string into individual strings
String delims = "[\\n]+";
String[] tokens = data.split(delims);
List<TextView> itemList = new ArrayList<TextView>(tokens.length);
for(int i = 0; i < tokens.length; i++)
{
TextView tvItem = new TextView(MainActivity.this);
tvItem.setText(tokens[i]);
tvItem.setTextColor(getResources().getColor(R.color.text_color));
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
itemList.add(tvItem);
myLayout.addView(itemList.get(i));
//myLayout.addView(tvItem);
//itemList.add(tvItem);
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10" >
<EditText
android:id="#+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#DDD"
android:hint="Enter a list item"
android:textColor="#D00" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8" >
<LinearLayout
android:id="#+id/itemListLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Button" />
</LinearLayout>
add the layout_orientation in your linearlayout
<LinearLayout
android:id="#+id/itemListLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000">

AutoCompleteTextView causes items in ListView to disappear

I'm doing the Extra Credit section of the Adding a List section in Android Tutorials. I tried populating the AutoCompleteTextView (ACTV) with the items contained in the ArrayAdapter, but if the addr field has characters in it above the threshold limit, the list doesn't show any items. Here is the code:
public class MyActivity extends Activity
{
List<Restaurant> model = new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list = (ListView)findViewById(R.id.restaurants);
adapter = new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.addr);
textView.setAdapter(adapter);
}
private View.OnClickListener onSave= new View.OnClickListener(){
...
};
}
And the XML...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="1"
android:layout_alignParentBottom="true"
>
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<AutoCompleteTextView android:id="#+id/addr"
android:completionThreshold="5"
/>
</TableRow>
<TableRow>
<TextView android:text="Type:"/>
<RadioGroup android:id="#+id/types">
<RadioButton android:id="#+id/take_out"
android:text="Take Out"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
/>
<RadioButton android:id="#+id/sit_down"
android:text="Sit Down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="#+id/delivery"
android:text="Delivery"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RadioGroup>
</TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/save"
android:text="Save"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/details"/>
</RelativeLayout>
Changing the completionThreshold changes the number of characters I can enter before the items in the List disappear.
Thanks for the help
You are attempting to use the same adapter for both the ListView and the AutoCompleteTextView. That is not a good idea. Please use separate adapters. You might even want a separate layout for the adapter used with the AutoCompleteTextView (e.g., android.R.layout.simple_dropdown_item_1line, as I used in this sample project).

Android- empty space at top of list view

I currently have a tabview with a listview inside. I only have three items in the listview and for some reason there is a bunch of white space at top. That is, the first item doesn't start at the top but rather in the middle. Any ideas? Also, i'm thinking that I am using the wrong layout. anything better than a listview if i only need to display three items? Thanks.
Code:
// Data to put in the ListAdapter
private String[] sdrPlaylistNames = new String[]{ "More Playlists...","Dubstep", "House"};
Intent playbackServiceIntentDUB, playbackServiceIntentHOUSE;
//alert dialog
AlertDialog.Builder builder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlists_layout);
//fill the screen with the list adapter
playlistFillData();
playbackServiceIntentDUB = new Intent(this, DUBAudioService.class);
playbackServiceIntentHOUSE = new Intent(this, HOUSEAudioService.class);
}
public void playlistFillData() {
//create and set up the Array adapter for the list view
ArrayAdapter<?> sdrListAdapter = new ArrayAdapter<Object>(this, R.layout.list_item, sdrPlaylistNames);
setListAdapter(sdrListAdapter);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/selectP" android:stackFromBottom="false">
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="#+id/selectP"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Select A Playlist Above"
android:textColor="#FF3300"
android:textSize="22dp"
android:textStyle="bold" >
</TextView>
</RelativeLayout>
If you want to display only three items then I think you have to use TableLayout.
Android - Table Layout.
Oh I missed your real problem in my previous answer.
Simply set android:layout_alignParentTop="true" to your list view as
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/selectP" android:stackFromBottom="false">
</ListView>
Hope this time I am clear..
Set your list view to AlignParentTop = true

ListView is not working for android

So I am reading Commonsware's Android Programming Tutorials and I am stuck with the part where the book asks me to add a ListView. Here is my layout's xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<EditText android:id="#+id/address"/>
</TableRow>
<TableRow>
<TextView android:text="Type: "/>
<RadioGroup android:id="#+id/types">
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/take_out" android:text="Take-Out"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/sit_down" android:text="Sit-Down"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/delivery" android:text="Delivery"/>
</RadioGroup>
</TableRow>
<Button android:id="#+id/save"
android:text="Save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/details"
/>
</RelativeLayout>
</ScrollView>
and this is my activity code
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import java.util.LinkedList;
import java.util.List;
public class LunchList extends Activity {
List<Restaurant> model = new LinkedList<Restaurant>();
ArrayAdapter<Restaurant> arrayAdapter = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView listView = (ListView) findViewById(R.id.restaurants);
arrayAdapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
listView.setAdapter(arrayAdapter);
}
private View.OnClickListener onSave = new View.OnClickListener(){
public void onClick(View view) {
EditText nameField = (EditText) findViewById(R.id.name);
EditText addressField = (EditText) findViewById(R.id.address);
Restaurant restaurant = new Restaurant();
restaurant.setName(nameField.getText().toString());
restaurant.setAddress(addressField.getText().toString());
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.types);
switch (radioGroup.getCheckedRadioButtonId()) {
case (R.id.take_out):
restaurant.setType("take_out");
break;
case (R.id.sit_down):
restaurant.setType("sit_down");
break;
case (R.id.delivery):
restaurant.setType("delivery");
break;
case (R.id.korean):
restaurant.setType("korean");
break;
case (R.id.chinese):
restaurant.setType("chinese");
break;
case (R.id.japanese):
restaurant.setType("japanese");
break;
case (R.id.italian):
restaurant.setType("italian");
break;
case (R.id.indonesian):
restaurant.setType("indonesian");
break;
}
arrayAdapter.add(restaurant);
Log.i("LunchList", "Array Adapter Size: " + arrayAdapter.getCount());
}
};
}
I added a logging line to see whether the object is being added to the adapter or not and it looks like it is being added in there. The UI however is not showing the ListView and I do not see stuff getting added in the list.
Edit XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableLayout
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:"/>
<EditText android:id="#+id/name"/>
</TableRow>
<TableRow>
<TextView android:text="Address:"/>
<EditText android:id="#+id/address"/>
</TableRow>
<TableRow>
<TextView android:text="Type: "/>
<RadioGroup android:id="#+id/types">
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/take_out" android:text="Take-Out"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/sit_down" android:text="Sit-Down"/>
<RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/delivery" android:text="Delivery"/>
</RadioGroup>
</TableRow>
<Button android:id="#+id/save"
android:text="Save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</TableLayout>
<ListView android:id="#+id/restaurants"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#id/details"
/>
</RelativeLayout>
</ScrollView>
As far as I can see, you initialise model:
List<Restaurant> model = new LinkedList<Restaurant>();
But don't put any content in it, so there is nothing for your list to show
EDIT: If you are adding content to your list dynamically, make sure that you are updating the list like this:
model.add(restaurant);
arrayAdapter.notifyDataSetChanged();
Or:
arrayAdapter.add(restaurant);
arrayAdapter.notifyDataSetChanged();
notifyDataSetChanged() lets the ListView know that the list contents have changed and it should redraw itself
EDIT: Also, you are adding the ListView above the TableLayout called details, which has its heigh and width set to fill_parent, so you may not see the ListView if the TableLayout is taking up the whole screen. Try changing the height of details to wrap_content
How exactly do you want this UI to look like? Your ListView says this:
android:layout_alignParentTop="true"
android:layout_above="#id/details"
So you're basically saying you want your ListView to be above details. But details is the first element, so that on is presumably on the top of the screen already!
EDIT: I wrote it in a comment, but I should amend my answer: use a LinearLayout instead (vertical orientation, of course), and give each element a weight of 1 (or adjust the weight as you wish). That way, one element can't drown out the other.

Categories

Resources