I've made a custom spinner but the sizing isn't exactly what I want it to be. The spinner become far to large to get the spacing in the list that I need. I want to be able to size the rows of the spinner independently of the size of the spinner button. I want the spinner to be thin and then I want the section rows to be spaced generously. (See image at the bottom):
Currently the xml for the rows of the spinner is this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="5dip">
<ImageView android:layout_width="32sp" android:src="#drawable/icon"
android:id="#+id/spinnerimage" android:layout_height="32sp" />
<TextView android:textSize="22sp" android:textStyle="bold" android:textColor="#000"
android:layout_width="fill_parent" android:id="#+id/category"
android:layout_height="fill_parent" android:paddingLeft="5sp" />
</TableRow>
</TableLayout>
I wanted to use a relative layout but the table layout gave me slightly better spacing. If I try to make the height it will cut off the text and icons in the rows. The spinner in my main.xml is:
<Spinner android:id="#+id/catspinner"
android:layout_marginLeft="25dip" android:layout_marginRight="25dip"
android:layout_width="fill_parent" android:layout_centerHorizontal="true"
android:layout_height="wrap_content" android:prompt="#string/prompt"
android:background="#drawable/yellow_btn"
android:layout_centerVertical="true" android:drawSelectorOnTop="true" />
I would like to have the spinner the sizing to be the same as the standard android spinner (on right.) Mine is currently reversed the spinner is to big and the rows spacing is too small.
Any ideas??
while setting adapter say adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this should work...
Maybe this is quite late but I'll share this anyway because I came across a similar problem.
You need to use the Views "simple_spinner_item" for the Spinner itself and "simple_spinner_dropdown_item" for the dropdown.
Here's a code snippet:
Spinner spnCat = (Spinner) findViewById(R.id.idea_category);
Cursor c = myDBHelper.getCategories();
startManagingCursor(c);
if (c != null) {
// use the simple_spinner_item view and text1
SimpleCursorAdapter adapterCat = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
c,
new String[] {c.getColumnName(1)},
new int[] {android.R.id.text1});
spnCat.setAdapter(adapterCat);
// use the simple_spinner_dropdown_item
adapterCat.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
}
Hope that helps.
Related
I have a spinner with the selections at max character length of 100. The text fits within the spinner but when I select the entry it doesn't autosize in the result. If I remove the android:layout_height then the app crashes.
Spinner xml with autoSizeTextType
<Spinner
android:id="#+id/spinnerCompany"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="43dp"
android:paddingLeft="16dp"
android:spinnerMode="dropdown"
app:autoSizeTextType="uniform"
app:layout_constraintTop_toBottomOf="#+id/otpLabel"
tools:layout_editor_absoluteX="27dp" />
Spinner open, text fits
Spinner item selected, text is cut-off
It's not a good practice to specify the layout height for a Text, instead, use textSize and let the height and width wrap_content.
<Spinner
android:id="#+id/spinnerCompany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp" (this is just random, find the ones fits your usage)
android:layout_marginTop="43dp"
android:paddingLeft="16dp"
android:spinnerMode="dropdown"
app:autoSizeTextType="uniform"
app:layout_constraintTop_toBottomOf="#+id/otpLabel"
tools:layout_editor_absoluteX="27dp" />
activity_main.xml
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
spinner_item.xml create custom spinner:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
/>
In your java class onCreate method write:
// Get reference of widgets from XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Initializing a String Array
String[] plants = new String[]{
"Black birch",
"Bolean birch",
"Canoe birch",
"Cherry birch",
"European weeping birch"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this,R.layout.spinner_item,plants
);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(spinnerArrayAdapter);
You can simply do this
<Spinner
android:id="#+id/bloodGrpSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:entries="#array/bloodGrps" />
You are giving wrap content to width in your layout simply make it match_parent and it will automatically pick the max length it can
I would like to create a listview that allows multiple choice. The typical solutions is to get a cursor and use the SimpleCursorAdapter.
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
R.layout.simple_list_item_multiple_choice, cur2, cols2, views2);
I am able to get this working when using the R.layout.simple_list_item_multiple_choice. I get the checkmarks to work when multiple items are selected.
So I decided to try it with a custom made layout. Here is the XML code for my layout.
<?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" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>
</LinearLayout>
So here is my issue. The layout is inflated fine using the same code and setting the ChoiceMode to multiple on my listview. The data from the cursor is populated fine.
However, the issue I have is that the checkmarks do not show up as selected (a check in the box) when I click on the item. Is there something I am missing that would not involve creating a custom adapter?
l2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
l2 is my listview.
I'm not up on CheckedTextView... from my understanding, it should toggle the check when you click the line.
I suppose you could try and force it like this:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView chkTxt = (CheckedTextView) v.findViewById(R.id.CheckedTextView1);
chkTxt.toggle();
}
Note this would be a hack and you should really find the root problem, but it might get you going in the meantime.
EDIT
After much googling, I found the issue... the root of the row layout needs to be checkable to use CheckedTextView, and a LinearLayout is not.
More googling found a solution... override the LinearLayout class.
Instructions/code here
The problem with "the issue I have is that the checkmarks do not show up as selected" is because of your wrong layout. It should be without LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/hasphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckedTextView
android:id="#+id/checkedTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
Then the SimpleCursorAdapter can set properly your "checkedTextView1" (with visible effect).
In my case I used the ArrayAdapter and it worked.
list.setAdapter(new ArrayAdapter<String>(this, R.layout.category_layout,
R.id.category_checkedText, this.categories));
I am using a spinner in several places in my program, but I will stick with one case.
I have two xml files -- small_new_system and new_system. They both have the a spinner that is named state_spinner.
The odd thing is that when I use this code on a tablet running 3.2, which uses new_system, they are displayed but when I put the app on my phone that is running 2.1, which uses small_new_system, they do not show up. The items are in the spinner list, but there is no text being displayed. I have tried naming the spinners differently, as well as not using a custom spinner layout.
The other odd thing is that when I use the identical layouts, which do not look very good, on the small device they are not populated with the text either.
Thank you for any help! My code is as follows:
Code to populate the spinner:
states = (Spinner) findViewById(R.id.state_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.states,
R.layout.spinner_layout);
adapter.setDropDownViewResource(R.layout.spinner_layout);
states.setAdapter(adapter);
states.setOnItemSelectedListener(new MyItemsOnSelectListener());`
Spinner in small_new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:inputType="textPersonName"
android:textSize="40dp" >
</Spinner>
Spinner in new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="500dp"
android:layout_height="75dp"
android:layout_below="#+id/city_edit"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:textSize="60dp" >
</Spinner>`
Code in custom spinner:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:textSize="40dp" >
</TextView>`
Try removing:
android:inputType="textPersonName"
from small_new_system. Then it would look like:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:textSize="40dp" >
</Spinner>
Or you might try adding a background color:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:background="#FFFFFFFF"
android:textSize="40dp" />
The solution I came up with was to use android.R.layout.simple_dropdown_item for my spinner when an sdk less than 11 is detected, and when the sdk is greater than 11 I use my custom spinner. This seems to work ok. Not my favorite solution, but it does work.
Try this one ..
spnPlan = (Spinner) findViewById(R.id.set_spinner1);
ArrayAdapter<CharSequence> planAdapter = ArrayAdapter.createFromResource(
this, R.array.plan_size, android.R.layout.simple_spinner_item);
planAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnPlan.setAdapter(planAdapter);
spnPlan.setOnItemSelectedListener(new MyOnItemSelectedListener());
I want to make a list contains some username from my sqlite database. I want it has white background and black text. I felt difficult when find every tutorial thought me to use extends listactivity....is there any technique to make it ?
here is some of my code in nir.java
setContentView(R.layout.changeplayer);
btnCreateN = (Button)findViewById(R.id.btnCreateNew);
btnCreateN.setOnClickListener(this);
btnSetP = (Button)findViewById(R.id.btnOK);
btnSetP.setOnClickListener(this);
//buat listview
lstUnameView = (ListView)findViewById(R.id.listUsername);
adapter = new ArrayAdapter <String>(this,
android.R.layout.simple_list_item_1, lstUname);
lstUnameView.setAdapter(adapter);
//koneksi ke db, ngambil username-username
helper = new sqlite(this, "tamadb", 1);
db1 = helper.getWritableDatabase();
c = db1.rawQuery("SELECT username FROM tama", null);
if(c.moveToFirst())
{
do
{
lstUname.add(c.getString(0));
}while(c.moveToNext());
}
helper.close();
and this my code in layout changeplayer
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/listUsername"
android:layout_width="fill_parent"
android:layout_height="350px"
android:drawSelectorOnTop="false"
android:background = "#FFFFFFFF"
android:cacheColorHint = "#191919" />
<Button android:id="#+id/btnCreateNew"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="Create New"
android:layout_below="#id/listUsername"
android:layout_marginLeft="40px" />
<Button android:id="#+id/btnOK"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="OK"
android:layout_below="#id/listUsername"
android:layout_toRightOf="#id/btnCreateNew" />
</RelativeLayout>
Please help, i want to finish this project too...he9x..thx before...^^
If you want your whole app to have white background and black text you can set the theme for your app to Theme.Light. Therefore you have to add the attribute android:theme="#android:style/Theme.Light" to the element of the manifest file.
This is the layout.xml I have put together for a listview with white background and black text.
You need to set the colours list_bg and menu_text to white and black respectively in your res folder then call the layout and id in your activity.
As far as populating a listview there is a good example here with downloadable code:
http://www.youtube.com/watch?v=Awu7Rlsez_k
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/list_bg">
<TextView android:background="#color/list_bg"
android:textColor="#color/menu_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding ="10dp"
android:textSize="22sp"
android:textStyle="bold"
android:id="#+id/row" />
<ListView android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
I can't fill a ListView layout with an array of strings. Here is the Activity:
ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom);
String symptomsArray[] = new String[1024];
// Then I fill up symptomsArray
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray);
symptomList.setAdapter(adapt);
Here is menu_item:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:textSize="#dimen/menu_item_size"
android:text="test string"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:gravity="center"
android:shadowDy="3"
android:shadowDx="3" />
And here is symptom.xml -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_height="wrap_content"
android:id="#+id/ListView_Symptom"
android:layout_width="fill_parent"
android:divider="#drawable/divider"
android:listSelector="#drawable/textured"
android:layout_alignParentTop="true"></ListView>
</LinearLayout>
Does anyone have any idea what is wrong? Does my public class need to extend "ListActivity". I tried that and that didn't work.
Try stripping out some of the presentational bits from your TextView. For example, I don't think that you want android:layout_gravity="center" there. Also, try setting your ListView's android:layout_height="fill_parent".
Turns out Eclipse is so slow running on my 5 GB Dram MacPro that it eventually worked and displayed the array in the ListView, it just took a few minutes! Sorry about the confusion.