Here is my XML of the spinner but it did not work.
<Spinner
android:id="#+id/frequency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/spinner_voices"
android:gravity="center|center_horizontal"
/>
You need to set your own layout for spinner item.
ArrayAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"ABC", "DEF", "GHI"});
spriner.setAdapter(adap);
Where R.layout.spinner_item is a layout with content:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
Related
I am trying to align selected text of a Spinner in Android. It should align to the matching indentation of the other TextView and EditText in the layout.
What I have in my layout:
http://i.imgur.com/Y5sgbsN.png
What i want to do:
http://i.imgur.com/viVHpiC.png
My Layout code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TextView01"
android:layout_below="#+id/TextView01"
android:entries="#array/bank_arrays"
android:prompt="#string/bank_prompt"
android:fontFamily="arial,helvetica"
android:hint="#string/bank_prompt"
android:spinnerMode="dialog"
android:ellipsize = "none"/>
Please help me out with this UI change.
Thanks
Update 1:
Parent Layout code:
<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:layout_marginBottom="14.5sp"
android:layout_marginLeft="14.5sp"
android:layout_marginRight="14.5sp"
android:layout_marginTop="14.5sp"
tools:context=".RegistrationActivity" >
This is the spinnerinflate.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f5ff"
android:textSize="#dimen/text_size"
android:text="Text Here" >
</TextView>
In your code you can set the custom Layout for spinner as
MyAdapter myAdapter = new MyAdapter(this, R.layout.spinnerinflate,
arraylist);
spinner.setAdapter(myAdapter);
You can use SimpleAdapter as well if you don't need to have your own adapter as below
ArrayAdapter<String> simpleadapter = new ArrayAdapter<String>(YourActivityName.this, R.layout.spinnerinflate, getResources().getStringArray(R.array.yourarray));
spinner.setAdapter(simpleadapter);
By this inflate you can do whatever formatting you want. Please let me know if it helps.
you can use this lines
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="0dp"
android:textDirection="ltr" />
and you can increase padding from left to more space
I have this line in my ListActivity
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MealsActivity.this, R.layout.meal_row);
and 2 of my layouts are like this:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/android:list"
android:layout_weight="1" />
<TextView
android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string/empty" />
meals_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" >
</TextView>
I believe smth is wrong with 'R.layout.meal_row' parameter.
Process: mycalories.com.jalle.mycalories, PID: 2659
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
How do I specify textview in which data should be listed ?
You can try this to set custom adapter for array list..
//creating String Array
ArrayList<String> arraylist = new ArrayList<String>;
arraylist = "Your String Array";
//Creating adapter
ArrayAdapter<String> adpt = new ArrayAdapter<String>(getApplicationContext(), R.layout.meals_row.xml, R.id.text, arraylist);
//setting adapter to string
list.setAdapter(adpt);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textSize="25sp" />
</RelativeLayout>
I just need that items in a Spinner rows wraps when their length is large.
So I did this:
item.xml
<TextView
android:id="#+id/tvAddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Address"
android:textSize="14sp" />
layout with Spinner:
...
<Spinner
android:id="#+id/spAddr"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
and a code to implement data
Cursor c = tbl.getAddr(id);
getActivity().startManagingCursor(c);
String[] from = new String[] { "address" };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, c,
from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner s = (Spinner) v.findViewById(R.id.spAddr);
s.setAdapter(adapter);
But I do not know how to use my row-resource item.xml in here. How can I implement my custom row?
UPD: after I change item.xml with MSquare code.
Closed Spinner
Opened Spinner
And when I choose item I've got picture 1 again.
Try this:
Your item.xml should be like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Address"
android:textSize="14sp" />
and in the code you can use now:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
R.layout.item, c, from, to);
Try it out.
It's too late but maybe it will be helpfull for someone
This is my code for custom spinner
final CardDAO cardDAO = new CardDAO(activity);
final Cursor cursor = cardDAO.getNameAllActiveCard();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(activity,
R.layout.spinner_row,
cursor, new String[] {"pan", "name"}, new int[] {R.id.spinner_pan, R.id.spinner_name}, 0);
adapter.setDropDownViewResource(R.layout.spinner_dropdown);
Spinner spinner = (Spinner) view.findViewById(R.id.operCardList);
spinner.setAdapter(adapter);
spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_pan"
android:paddingRight="5dp"
android:textSize="13sp" />
spinner_dropdown.xml
<?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:id="#+id/spinner_name"
android:padding="7dp"
android:textSize="16sp" />
Now you can customise it as you wish
I'm trying to use an ArrayAdapter in a ListActivity:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.id.text1, new String[] { "a", "b"});
setListAdapter(adapter);
This is the layout:
<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" >
<TextView
android:id="#id/android:text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_below="#id/android:text1" >
</ListView>
</RelativeLayout>
None of the solutions I found on StackOverflow seems to work. I'm using API 10. Can you help please?
You are using the wrong type of resource, you need to reference R.layout not R.id. Try android.R.layout.simple_list_item_1:
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { "a", "b"});
Define a layout for the rows of your ListView, and call it, for example, simple_list_item_1 (create the file simple_list_item_1.xml in the folder res/layout). Put your TextView into this layout:
<?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" >
<TextView
android:id="#id/android:text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
</RelativeLayout>
and then create your ArrayAdapter like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, new String[] { "a", "b"});
Here you'll find a detailed explanation about Lists and Adapters.
I have made a list view containing two childs. Now i am trying to append a series of children to each of these two childs.
I am using following procedure.
super.onCreate(savedInstanceState);
setContentView(R.layout.report_main_layout);
CharSequence[] dummyChar = {"a","b"};
listView = (ListView) findViewById(R.id.UIMainAccountListView);
adapter = new CustomArrayAdapterOfMainList(this, dummyChar);
listView.setAdapter(adapter);
In the custom adapter i am using following code.
if (position == 0)
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, debitAccounts,sumDebit);
viewHolder.listView.setAdapter(adapter);
}
else
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, creditAccounts, sumCredit);
viewHolder.listView.setAdapter(adapter);
}
All i am getting is like this, I want to extend it so that all of the accounts show, List view should wrap content but it is not wrapping content
This will stack two listviews and force them to take up half of their parent each.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
You can remove the cache color by setting android:cacheColorHint="#android:color/transparent" for each listview
<?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"
android:paddingBottom="20sp" >
<ListView
android:id="#+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/child1" />
</RelativeLayout>
This layout for first child1 listview above the second child2 listview
Thanks