Some items in my spinner are long and I would like the spinner to wrap the text over multiple lines depending on how long the text is. I looked over serval solutions for this but they are not working for me.
Here showing the text not fitting:
Here is my code in Java:
public void addItemsOnSpinner3() {
spinner3D = (Spinner) findViewById(R.id.activities1);
List<String> list = new ArrayList<String>();
list.add("Academic Year Opening Program");
list.add("Academic Year Closing Program");
list.add("LR1: Defining Primary Care, Health Care Disparities & Vulnerable Populations");
list.add("LR2: Community Resources and the Elderly");
list.add("LR3: Inter-professional Teams and the Homeless");
list.add("LR4: Quality Improvement and Veterans");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.multiline_spinner_row, list);
dataAdapter.setDropDownViewResource(R.layout.multiline_spinner_row);
spinner3D.setAdapter(dataAdapter);
}
I created a custom row called "multiline_spinner_row.xml" where singleline is changed to false:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
xmlns:android="http://schemas.android.com/apk/res/android" />
All this does is make my selection wrap the text but not the items:
Any help would be greatly appreciated! Thanks in advance!
in spinner set this property:
android:spinnerMode="dialog"
in text view set these properties:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
This actually worked for me (in Xamarin Android, but the problem is the same):
Instead of
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
write
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleExpandableListItem1);
create an xml file like this, your xml will need to have a parent layout to wrap the TextView.
<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="wrap_content">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#333333"
android:textSize="15sp"
tools:text="123" />
</RelativeLayout>
make sure your TextView has an id, then create your adapter like this.
private ArrayAdapter<CharSequence> stringAdapter = new ArrayAdapter<>(context, R.layout.spinner_textview, R.id.textView, displayTexts);
use this consturctor
public ArrayAdapter(#NonNull Context context, #LayoutRes int resource,
#IdRes int textViewResourceId, #NonNull List<T> objects) {
this(context, resource, textViewResourceId, objects, false);
}
or any constructor that has textViewResourceId as a parameter
that's it. you won't need to override anything else.
Check out the below image to view the effect.
enter image description here
enter image description here
Try this
I solved the problem with simple coding.
Just add Android.Resource.Layout.SimpleExpandableListItem1 when assigning the Adapter for Spinner as display below.
please check more on
https://notostuck.blogspot.com/2020/08/how-to-populating-spinner-items-with.html
Related
Can I move the arrow somewhat closer to the text, in a easy way? Don't really understand the purpose of this as default when its transparent.
<Spinner
android:id="#+id/spinner_months"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I have answered a similar question here: https://stackoverflow.com/a/42596698/1260126
This can be achieved by creating a custom layout for the selected spinner item custom_spinner_item.xml. I have added a TextView which displays the currently selected spinner item. The arrow icon is added in an ImageView. You can use any icon. The arrow icon moves depending on the length of the text. In fact you can completely modify the look of your spinner in this layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/spinner_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#mipmap/ic_arrow_down"/>
</LinearLayout>
Create a custom spinner adapter and inflate the above view. Also set the text of your selected spinner item from your list by overriding the default getView() method.
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
List<String> spinnerItems;
public CustomSpinnerAdapter(Context applicationContext, int resource, List<String> spinnerItems) {
super(applicationContext, resource, spinnerItems);
this.spinnerItems = spinnerItems;
inflater = (LayoutInflater.from(applicationContext));
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.custom_spinner_item, null);
TextView type = (TextView) view.findViewById(R.id.spinner_item_text);
type.setText(spinnerItems.get(i));
return view;
}
}
Then instantiate the CustomSpinnerAdapter class and set it as your spinner's adapter. spinnerList is the list of items to be shown in the spinner.
CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(getContext(), android.R.layout.simple_spinner_item, spinnerList);
spinner.setAdapter(customSpinnerAdapter);
have problem with 'listView'. I searched everywhere but the problem does not arise none. Let me explain:
I have classic 'listview' in my 'layout':
<?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"
android:id="#+id/roza"
android:tileMode="repeat">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80ffffff"
android:alpha="200">
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_todos" />
</LinearLayout>
And i create custom list_row xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/list_name"
android:layout_alignParentTop="true"
android:textSize="25dp"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/list_rune" />
------------bla bla bla -----------------
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_alignTop="#+id/list_date_update"
android:layout_toLeftOf="#+id/list_rune"
android:layout_toStartOf="#+id/list_rune"
android:id="#+id/textTester" />
</RelativeLayout>
My main class -
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener, SharedPreferences.OnSharedPreferenceChangeListener {
private NordBase dbHelper;
private Cursor cursor;
int rage = list_row; // my custom row
-------------------on create----------------
setContentView(R.layout.activity_main);
this.getListView().setDividerHeight(2);
dbHelper = new NordBase(this);
fillData();
------------------fillData-----------------
cursor = dbHelper.getAll();
startManagingCursor(cursor);
String[] from = new String[] { NordBase.NORD_NAME, NordBase.NORD_DESCRIPTION,
NordBase.NORD_PUNKTS, NordBase.NORD_PUNKTS_CROSS,
NordBase.NORD_DATE_CREATE, NordBase.NORD_DATE_UPDATE, NordBase.NORD_RUNE,
NordBase.NORD_IMPORTANT, NordBase.NORD_SUM };
int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum, };
notes = new SimpleCursorAdapter(this,
rage, cursor, from, to);
setListAdapter(notes);
getListView().setOnItemLongClickListener(this);
Alas, I can not get to the items inside list_row, to bind them for comparison operators. for exp: if price == 0, then textview.setvisible = false...
I try LayoutInflater, but he make view element difrent from those in listview.
I try adapter binder class - but i think its no good for that.
Mostly i have an error - can't do that for the "null element"... that case - looks like all i ever do - my program can't findViewById elements in the list_row.
There is one point of contact
int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum, };
notes = new SimpleCursorAdapter(this,
rage, cursor, from, to);
So help me Obi-Van Kenobi, and sorry for my dummy ENG, and i'm not sure that i past cod in right way)))
First of all your layout is not having the views which you have mention in the adapter like
R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum
put this thing in the layout list_row xml
if you mean by ------------bla bla bla -----------------
this views than ok
also simpleCursorAdapter will not solve your purpose as you want to hide and show the views based on the data in respective views
so my first suggestion Change your layout to linearLayout or tableLayout or other layout as you feel and then do this in Your CustomAdapter class
like
class MyAdapter extends BaseAdapter
{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//here you infalteYour layout and and do your logical thing
//you can take your data out from cursor and store in the arrayList<YourModelClass> type and have better control
}
}
otherwise in relative layout just fill the data don't hide it so it may work but if you do hiding of some views chances are high it will not solve your problem
and typo mistake is there i hope so like
int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum, };
//here no item but still u have written "," after R.id.list_sum this like "R.id.list_sum, "
notes = new SimpleCursorAdapter(this,
rage, cursor, from, to);
Im very new in android programming, and my task is to load up the items in my shared preference to a listview, and i just want to change the design of my listview, like changing its font color, font size and etc.. I have tried this solution but it doesn't work, please tell me where I did wrong. Thanks..
heres my code in loading the listview in a listactivity
Map<String,?> datakeys = datapref.getAll();
for(Map.Entry<String,?> entry : datakeys.entrySet()){
Log.d("values123",entry.getKey() + ": " + entry.getValue().toString());
lvlist.add(entry.getValue().toString());}
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
//lvadapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, lvlist);
setListAdapter(lvadapter);
heres the mytextview xml..
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dip"
android:textColor="#666666"
android:textStyle="italic" />
and my listview xml
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:background="#b6fcd5" >
</ListView>
here's the screenshots
You need other constructor of ArrayAdapter :
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
so Change this line to :
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview, lvlist);
To :
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
And mytextview xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dip"
android:textColor="#666666"
android:textStyle="italic" />
If this doesn't work , then Go with Custom Adapter
one more approach: have a look
String [] yourStringarrayorlist= {"Lionssss","Cats", "yeaaahhh :)"};
ListView listView =new ListView(
getApplicationContext());
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, yourStringarrayorlist) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
// if you want to change any specific items color
int textColor = textView.getText().toString().equals("Cats") ? R.color.any : android.R.color.black;
textView.setTextColor(VideoviewActivity.this.getResources().getColor(textColor));
//If you want to change the text color of all use below code
// textView.setTextColor(VideoviewActivity.this.getResources().getColor( R.color.any));
return textView;
}
});
layoutrela.addView(listView);
You can change the textcolor easily. However for just a check i've created ListView through code and added to the layout :) but it works like awesome..
Inspired by : How to change text color of simple list item
not a new answer so i would not expect any vote or so.
I'm using a SimpleCursorAdapter to display a single CheckedTextView. I know this is done best using simple_list_item_multiple_choice and android.R.id.text1.
adapter = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice, rules,
new String[]{Constants.KEY_RULE}, new int[]{android.R.id.text1});
If the text from KEY_RULE is more than two lines, android doesn't wrap, instead it hides it. Is there an easy work-around for this without having to implement my own adapter?
Find my xml code below:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/header" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/rule_selection_for_revision_list_title"
android:padding="4dip" android:background="#000000" android:textColor="#ffffff" android:textStyle="bold" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
<TextView android:textSize="18sp" android:textStyle="bold"
android:id="#id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"
android:text="There are no rules to consider." />
</LinearLayout>
Is there any way I can at least reduce the font size so that it fits to two lines?
There are several options.
You can define your custom layout for item in the list. This allows you to fully customize your item UI. More details can be found in List View tutorial.
The other approach can be used if you still want to use standard item layout but only fix one small thing in it. You can extend Adapter and modify the view in getView function. Here is an example:
class MySimpleCursorAdapter extends SimpleCursorAdapter
{
public MySimpleCursorAdapter(Context ctx, int layout,
Cursor cursor, String [] from, int [] to)
{
super(ctx, layout, cursor, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView textView = (TextView)view.findViewById(android.R.id.text1);
//You can increase number of lines in the text view
textView.setMaxLines(5);
LayoutParams params = textView.getLayoutParams();
if(params.height > 0)
{
int height = params.height;
params.height = LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
textView.setMinHeight(height);
}
//Or you can make your font smaller
textView.setTextSize(customTextSize);
//or whatever you like, even apply new custom style
//...
return view;
}
}
You should probably choose the first approach (custom layout), but sometimes the second approach is feasible too.
You don't need to implement a custom Adapter. Just define a custom layout for the list items and pass it to the adapter instead of android.R.layout.simple_list_item_multiple_choice.
You can search the Android sources (come with the SDK on you computer) to how the original simple_list_item_multiple_choice layout looks like and adapt it to your needs.
I'm having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the items. No other phones seem to have this problem. I was going to try to force the font to be black to see if that helps. How can I do that?
Here's how I'm currently populating the spinner. It seems like the simple_spinner_item is broken on Droid X's.
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,android.R.layout.simple_spinner_item, spin_arry);
I'm going to use Spinner project sample from Android SDK for next code examples.
Code:
First, you need to create you custom adapter which will intercept the creation of views in drop down list:
static class CustomArrayAdapter<T> extends ArrayAdapter<T>
{
public CustomArrayAdapter(Context ctx, T [] objects)
{
super(ctx, android.R.layout.simple_spinner_item, objects);
}
//other constructors
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
//we know that simple_spinner_item has android.R.id.text1 TextView:
/* if(isDroidX) {*/
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.RED);//choose your color :)
/*}*/
return view;
}
}
Then you create adapter in your code like this:
String [] spin_arry = getResources().getStringArray(R.array.Planets);
this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);
Explanation:
Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.
Screenshot:
Simple and Crisp ...
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
write a R.layout.simplespinneritem:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The ID is android:id="#android:id/text1", set the color of font and background.
ArrayAdapter adapter =
new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);
public class ee extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ww);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
Spinner spinner1;
ArrayAdapter<String> mAdapter;
spinner1= (Spinner) findViewById(R.id.spinner2);
spinner1= new ArrayAdapter<String>(this, R.layout.spinner_item, array);
spinner1.setAdapter(mAdapter);
}
}
and in xml res/layout add new xml file: type layout, spinner
(in spinner_item.xml)
<?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="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#00f0ff" />
To add to sasad's reply, make a copy of that file, which you can find in your Android folder, in your project, change the text color of the TextView in that file, and use that layout while initializing the Adapter instead of android's.
You could try this approach too wherein you add 2 new Layout Resource Files
Custom_spinner_list_item
Custom_spinner_dropdown_item
and use them in the code .
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,R.layout.custom_simple_spinner_item, spin_arry);
adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
custom_spinner_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fontFamily="#font/roman"
android:singleLine="true"
android:textAlignment="inherit"
android:textColor="#color/black"
android:textSize="14sp">
</TextView>
custom_spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:fontFamily="#font/roman"
android:singleLine="true"
android:textAlignment="textStart"
android:textColor="#color/black"
android:textSize="14sp">
</TextView>
Happy Coding !! :)
make your own layout xml file, and give a android:textColor="#000" for black text
Here is more appropriate way guys,
First find the "simple_spinner_item.xml" file in your system,
Follow the below path,
C:\Users[username]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout
Now copy the content of "simple_spinner_item.xml" file
Second create the custom_spinner.xml file in your project res\layout folder
and paste the copied content in recently created file
Here is the sample:
res\layout\custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:textAlignment="inherit"
android:ellipsize="marquee"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:singleLine="true"
android:textColor="#color/dark_gray"
style="?android:attr/spinnerItemStyle"
android:id="#android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android"/>
Here is the set adapter code:
Spinner ddlArea = (Spinner) findViewById(R.id.ddlArea);
ddlArea.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_spinner, areaList));
Where areaList is the List
Thanks,
Ejaz Waquif