Does anybody know How to set the size of an AutoCompleteTextView Result ?
I try android:textSize="12sp" But it only modify the size of the text in the TextView, not in the result.
The best way is to create your own xml file with a customized design, such as "dropdown.xml"
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textAutoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F3F5E1"
android:gravity="center_vertical"
android:padding="5dp" />
and in your code use this
AutoCompleteTextView etChoose= (AutoCompleteTextView) findViewById(R.id.etChoose);
ArrayAdapter<String> adapterChoose = new ArrayAdapter<String>(this,R.layout.dropdown, ChooseList);
Just add android:dropDownHeight="size" for AutoCompleteTextView in xml file.
private ArrayAdapter<String> autoCompleteAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, yourList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(14);
Typeface Type = getFont () ; // custom method to get a font from "assets" folder
((TextView) v).setTypeface(Type);
((TextView) v).setTextColor(YourColor);
((TextView) v) .setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
return v;
}
};
YourAutoCompleteView.setAdapter(autoCompleteAdapter);
Assuming someone is facing the same issue now
Instead of using simple_spinner_item you can also use simple_spinner_dropdown_item to get better results as it will format the data as it would appear in a normal dropdown
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, subjects1);
Related
How to set style on spinner ? I have style with font size and family for custom edit text class and I need same for spinner. I tried to add style tag in spinner xml but it is ignored, I tried in adapter but it doesn't work.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tempList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/CANDARA.TTF");
((TextView) v).setTypeface(externalFont);
return v;
}
}
Instead of using the simple spinner layout android.R.layout.simple_spinner_item, create your own layout file containing a TextView and style it the way you'd like. Then, pass a reference to that layout when you instantiate the ArrayAdapter.
You are passing the default android layout for the spinner items, instead create your own layout like:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
stlye="#style/txtViewStyleHere"/>
</RelativeLayout>
Pass this layout to the adapter, in that case, you will have to create your own adapter class to access the textView.
you want to add customize spinner
for that see these two link it might be help you .
Link 1 normal as you want just changing text and all that,
and for hover effect on your spinner look this link . Link2
Having trouble getting my spinner to populate. Well, I was getting it to populate just fine with a string array from strings.xml but now I want to add a custom font to the array. I'm using a Custom ArrayAdapter for the first time and it is overwriting my "android:entries=" and "android:prompt=" inside its layout xml.
So, I need to populate this spinner with an array from my strings.xml
(Inside onCreate)
spinner1 = (Spinner) findViewById(R.id.distancespinner);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
spinner1.setAdapter(ma);
Then:
private class MyArrayAdapter extends ArrayAdapter {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");
public MyArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(font);
return v;
}
public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(font);
return v;
}
}
I tried adding the entries and prompt again inside the my_spinner_style.xml...
<?xml version="1.0" encoding="utf-8"?>
<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:entries="#array/location_arrays"
android:prompt="#string/location_prompt"
android:singleLine="True" />
But, that is not working either.
Maybe this is a little late. After I implement your code and some additional test, I think I finally get the answer. The KEY problem the spinner not populating is:
You set the entries of the spinner within the xml file but replace it programmatically with an EMPTY adapter.
I have tried the following ways to populate the spinner with different results:
Set the entries in xml without any adapter.
<Spinner
android:id="#+id/simple_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/simpleStringArray"
android:prompt="#string/simplePrompt"/>
It populates the spinner with right content without any additional style.
Set the entries in xml with another adapter set to the spinner (as you did in the question)
in .xml
<Spinner
android:id="#+id/simple_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/simpleStringArray"
android:prompt="#string/simplePrompt"/>
in .java:
Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
It displays an empty spinner.
set the data programmatically to adapter
in .xml
in .java
setContentView(R.layout.activity_spinner);
Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
spinnerAdapter2.addAll(items2);
private class MySpinnerAdapter2 extends ArrayAdapter {
public MySpinnerAdapter2(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTextColor(Color.BLUE);
return v;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/Igualb.ttf");
TextView v = (TextView) super.getDropDownView(position, convertView, parent);
v.setTextColor(Color.RED);
v.setTypeface(myFont);
return v;
}
It displays the content correctly with cumtom-set font.
You can check for the complete project in this github repository if you like to.
Wish this helps.
That's the code I rewrote and it's working fine:
Spinner spinner = (Spinner) findViewById(R.id.distancespinner);
spinner.setPromptId(R.string.location_prompt);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
ma.addAll(Arrays.asList(getResources().getStringArray(
R.array.location_arrays)));
spinner.setAdapter(ma);
And the XML
<?xml version="1.0" encoding="utf-8"?>
<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:singleLine="True" />
Adapter is the same.
May be the below line is incorrect
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");
m01.TTF is wrong change to m01.ttf
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.ttf");
The text on my spinners is white, and I have no idea why.
This is my xml, nothing special
<Spinner
android:id="#+id/spinner_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
And my code
dateSpinner = (Spinner) findViewById(R.id.spinner_date);
selectedDate = calendar.getTime();
List<String> list = new ArrayList<String>();
list.add(formatter.format(selectedDate));
dateAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, list);
dateSpinner.setAdapter(dateAdapter);
What could be the reason that my text is displayed in white?
EDIT:
I've found the reason, I replaced the mContext parameter which was set in my onCreate.
mContext = getApplicationContext();
Now I use d
ateAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
and it works.
I have same problem and have found the answer. You dont use application context, instead, just use getActivity() (if you are in fragment) or this (if you are in activity), it will work
dateAdapter = new ArrayAdapter<String>(**this**,
android.R.layout.simple_spinner_item, list);
I solved this problem using
getBaseContext()
instead of
getApplicationContext()
i change it from
new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, some_list);
to
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
it's fixed, although i don't want to use "this"
I've changed the text color of that spinner's textview without creating a new layout. I know it's been a long time but it's what worked for me, just thought of sharing it. Best part is you can use it on any default adapter.
Here's the code : (this for Activity & requireActivity for Fragments)
1) Java
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,groups){
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView listItem = view.findViewById(android.R.id.text1);
listItem.setTextColor(Color.WHITE);
listItem.setTextSize(20);
listItem.setElevation(18);
return view;
}
};
2) Kotlin
arrayAdapter = object : ArrayAdapter<String>(requireActivity(), android.R.layout.simple_spinner_item, spinnerCategoriesList) {
override fun getView(position: Int, #Nullable convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val listItem = view.findViewById<TextView>(android.R.id.text1)
listItem.setTextColor(Color.BLACK)
listItem.textSize = 16f
return view
}
}
I also had same problem it was because of my application theme. Which I solved by replacing the
android.R.layout.simple_spinner_item
with
android.R.layout.simple_list_item_1
in my ArrayAdapter. I hope this may solve your problem
Maybe you have a white android:textColor="#android:color/white" attribute in your simple_spinner_item.xml in the layout folder of your project.
Better use a custom spinner item layout with a good android:textColor="#android:color/COLOR_YOU_WANT_TO_USE" attribute.
I assume you have created your own TextView for your Spinner, e.g.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/white"
android:padding="5dip"
/>
and glued it to your Adapter via a call like that
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
This should result in having the selection-text of your spinner including its items within the dropdown view painted in white. Now the background of your dropdown view will be influenced by your app theme, which in most cases, will lead your white text to be rendered on a white background. To avoid this android allows you to set a resource for your spinner dropdown view. You can set your own view or simply use the default drop-down view, which will overwrite your custom textview within the dropdown menu through the call
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
The complete code could look like this
this.spinner = findViewById(R.id.spinnerView);
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(adapter);
You can easily style the Spinner. Use this in style.xml :
<style name="SpinnerThemeLight" >
<item name="android:colorBackground">#color/black</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:textColorSecondary">#color/black</item>
<item name="android:textColorTertiary">#color/black</item>
<item name="android:textColorPrimaryDisableOnly">#color/black</item>
</style>
In the above xml file, I have simply given black color for time sake. Just play with it and sort out your preferred color.
Define the Spinner in the activity.xml as follows :
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="#array/countrys"
android:spinnerMode="dropdown"
android:theme="#style/SpinnerThemeLight"/>
Sorry if this is obvious to everyone else but I am having a minor difficulty understanding how to display html inside my listview.
My list view is declared.
ListView lv1 = (ListView) findViewById(R.id.ListView01);
I populate it (not shown) then set my listview here with an ArrayAdapter.
lv1.setAdapter(new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, foods));
Further down I create a new array of strings that I want to have bold tags in. I then add this new array (called arr_sort) to the arrayadapter insdie a onTextChanged() method.
lv1.setAdapter(new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort));
So now that my new Array of Strings has < b > tags in it. How do I make my listview display the bold text?
Here is my new_list_view
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/grey2"
android:textSize="20sp"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="40dip"
/>
And here is my ListView part in my main layout.
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners_green"
android:cacheColorHint="#00000000"
android:divider="#color/green6"
android:dividerHeight="1px"
android:fastScrollEnabled="true" >
</ListView>
Any help would be much appreciated.
Ok, so Jitendra Sharma was had the right idea for my scenario, but I needed to override the getView method. Or at least that is what worked for me. Then in the getView method I was able to set my text to render in html.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (null == convertView) {
row = mInflater.inflate(R.layout.new_list_view, null);
} else {
row = convertView;
}
TextView tv = (TextView) row.findViewById(android.R.id.text1);
tv.setText(Html.fromHtml(getItem(position)));
//tv.setText(getItem(position));
return row;
}
};
lv1.setAdapter(adapter);
override getItem method of the Adapter and do the following:
ArrayAdapter<String> adapter= ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort){
public Object getItem(int position)
{
return Html.fromHtml(arr_sort.get(position));
}
};
If you are using a SimpleAdapter, here is the code that enables HTML on a TextView.
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (data instanceof Spanned && view instanceof TextView) {
((TextView) view).setText((Spanned) data);
} else {
((TextView) view).setText(Html.fromHtml(String.valueOf(data)));
}
return true;
}
}
);
Ref: [Link] (http://android.jreactor.com/2012/07/17/simpleadapter-spanned-html-fromhtml/)
If all you wanted is to display some text where parts of the text should be bold, all you need is one TextView, and properly formatted text (with <b> added) and do the following:
textview.setText(Html.fromHtml(text));
For more information on what TextView+Html can support, see here
If you have the possibility of loading your texts from strings.xml, adding the tag there will automatically bold your text.
If however your texts are dynamic, you will have to create a custom adapter, and in it to set the text using textView.setText(Html.fromHtml(yourText));
ArrayAdapter<Spanned> listAdapter = new ArrayAdapter<Spanned>(MyActivity.this, R.layout.row);
listAdapter.add(Html.fromHtml(htmlText));
listAdapter.add(Html.fromHtml(htmlText));
...
if you use ksoap for html data from any database engine
yourVariable=String.valueOf(Html.fromHtml(ic.getProperty(0).toString()))
adapter = new ArrayAdapter<String>(rr.getContext(),android.R.layout.simple_list_item_1,titulos)
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (null == convertView) {
row = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null);
} else {
row = convertView;
}
TextView tv = (TextView) row.findViewById(android.R.id.text1);
tv.setText(Html.fromHtml(getItem(position)));
return row;
}
};
lista.setAdapter(adapter);
This also works and is perphaps a lot simpler. First, pass your data from String[] to Spanned[]
Spanned[] myhtmldata = new Spanned[mydata.length];
for(int i = 0 ; i < mydata.length; i++) {
myhtmldata[i] = Html.fromHtml(mydata[i]);
}
Then declare the ArrayAdapter using the CharSequence parameter
ArrayAdapter<CharSequence> linksadapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.list_item, R.id.textview, myhtmldata);
setListAdapter(linksadapter);
Borrowed from here
I have the layout:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:textColor="#android:color/white"
android:dividerHeight="1px"
android:listSelector="#drawable/highlight_sel"
/>
</LinearLayout>
And the code:
private ListView lv1;
private String lv_arr[]={"Item 1","Item 2","Item 3","Item 4"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsmenu);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
lv_arr));
}
I want the text color of Item 2 (or 1 or 3 or 4) to appear dynamically as red (denoting a new item) or white (default). Is there a way to do this?
I already have a selector present, which is why I used ListView. I've search the Internet and this site, and I have not seen this question broached.
So is it possible?
Yes everything is possible. you need to write your own adapter implementation basically overriding the getView Method in the adapter. search google and stack you will find many tutorials on how to write an adapter.
Writing a special adapter to override getView in simple adapter is the way to change the text color alternating on the lines of your choice in a listview. I took the example which has been repeated many times on this website and added a way to change the text color. position mod length to select the color position can be replaced with any scheme you like. The text view "business" can be the first line of your layout like mine--or use the android.R.id.text1.
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
//view.setBackgroundColor(colors[colorPos]); //old example
TextView tv1 = (TextView)view.findViewById(R.id.business); //new
tv1.setTextColor(colors[colorPos]); //new
return view;
}
}
Just use SpecialAdapter instead of SimpleAdapter in your app.
Here's an example of a getView method. Note that it's using a viewholder for efficiency. If you want to know more about that, let me know.
public View getView(int position, View convertView, ViewGroup parent) {
tempDeal = exampleBoxArrayList.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.list_item_example_box, null);
holder = new ViewHolder();
holder.divider = (RelativeLayout) convertView.findViewById(R.id.example_box_divider);
holder.merchantName = (TextView) convertView.findViewById(R.id.example_box_merchant_name);
holder.expireDate = (TextView) convertView.findViewById(R.id.example_box_expire_date);
holder.description = (TextView) convertView.findViewById(R.id.example_box_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (tempDeal.isDivider()) {
holder.divider.setVisibility(View.VISIBLE);
} else {
holder.divider.setVisibility(View.GONE);
}
holder.merchantName.setText(tempDeal.getMerchantName());
holder.expireDate.setText(tempDeal.getExpiryDateString());
holder.description.setText(tempDeal.getPriceOption().getDescription());
return convertView;
}
As you can see, I call the isDivider() method on my custom object (this method looks at a boolean set on data load). This method is used to turn the visibility of part of the layout on or off.
Alternatively, you could load a completely new layout based on this same concept.