How do I specify for a spinner in Android an extra padding on top/bottom of the first/last item? See sample of Goolge below (8 dp spacing).
Spinner does not support multiple view types.Issue.
I suggested you using dialog for showing your list.
Edited
according to your comment resolved your problem with this question
You need to create a custom adapter for you Spinner using your custom layout. Thus, you just need to provide a layout with the proper margin/paddins.
Spinner spinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.data, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
You can find a valid example here.
Just create a separate layout for the items you want to display in the spinner.
Something like this
spinner_text_view.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="#string/sample_list_item"
android:textColor="#android:color/black"
android:textSize="20sp" />
Then in your activity/fragment, you can create an adapter (an array adapter to be simple in this case) and pass this layout to that adapter reference. Finally set that adapter to the spinner and you can get the desired result.
Sample code.
private void setupSpinner() {
String[] sampleSpinnerItems = {"One", "Two", "Three", "Four", "Five"};
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_text_view, sampleSpinnerItems);
sampleSpinner.setAdapter(spinnerAdapter);
}
Hope this helps.
Note : spinner_text_view is the layout file you have created earlier.
You need custom adapter for this and custom layouts .
You can use 2 layouts . Supposing you have n elements to show , in your spinner you ll have n+2. iF the position is 0 (the first element) or the last element you will show the empty layout otherwise you will show the other layout .
The empty layout you will use for creating this space
You can accomplish this using custom Adapter for Spinner. It will look like this:
class YourAdapter extends BaseArrayAdapter<YourObject> {
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.your_item, parent, false);
DropDownViewHolder holder = new DropDownViewHolder();
holder.root = convertView.findViewById(R.id.drop_down_root);
// other assignments etc.
convertView.setTag(holder);
}
DropDownViewHolder holder = (DropDownViewHolder) convertView.getTag();
int bottomPadding = 0;
int topPadding = 0;
if (position == 0) {
topPadding = getContext().getResources().getDimensionPixelSize(R.dimen.margin_8);
} else if (position == (getCount() - 1)) {
bottomPadding = getContext().getResources().getDimensionPixelSize(R.dimen.margin_8);
}
holder.root.setPadding(0, topPadding, 0, bottomPadding);
// other UI related logic etc.
return convertView;
}
// ...
public static class DropDownViewHolder {
View root;
// other views
}
}
Actually logic with setting extra padding can be changed to changing some stub view visibility to VISIBLE or inflating some other layout depending on position, but the solution with padding seems more natural to me.
You don't actually need a custom adapter for this. The following solution works perfectly.
Create a drawable bg_spinner.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<padding
android:bottom="8dp"
android:top="8dp" />
<solid android:color="yourPopupColor" />
</shape>
Then set android:popupBackground="#drawable/bg_spinner_popup" property for your Spinner in XML.
This works like a charm without anything being cut off etc.
Related
I want to write an activity that is similar to the about screen of android phones. I want it to display some information in the style of the about screen of android phones.
Like this
title1
info
-----------------
title2
info
-----------------
etc.
Is there a special view that I can use or is it just a result of multiple views placed in a specific way? Or is there an activity template in android studio that I can use?
Use ListView. You can create a custom layout for cells and then use an array or a cursor to fill the data.
ListView: A view that shows items in a vertically scrolling list. The
items come from the ListAdapter associated with this view.
ListAdapter can receive data as input. The adapter would inflate the layout for each cell in its getView() method and assign the data to the individual views in the cell.
Read more about ListView here: http://developer.android.com/reference/android/widget/ListView.html
See PreferenceActivity or PreferenceFragment. They are special list views populated either from code or from a xml file. There are many different preference types to choose from (checkbox, switch, list etc)
An example preference fragment:
You can use ListView and a custom ArrayAdapter to create a screen like that. If you need any help about how to create a custom ArrayAdapter check this useful tutorial here.
If you want to create a simple list, then ListView is probably the simplest option. You may also want to look into ListActivity and/or ListFragment as well to further simplify the process.
If you intend to use complex animations, or have the list update dynamically with animations, you may be better served with RecyclerView, although using it is more complex.
An straightforward implementation of ListActivity could look something like this:
public class MainActivity extends ListActivity {
String[] titles = { "title one", "title two" };
String[] descriptions = { "desc 1", "desc 2" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListAdapter() {
leave everything the same, except for getCount() and getView()
#Override
public int getCount() {
return titles.length;
}
This will ensure you list is always the correct length as your array.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row, parent, false);
} else {
view = convertView;
}
TextView title = (TextView) view.findViewById(R.id.title);
TextView description = (TextView) view.findViewById(R.id.description);
title.setText(titles[position]);
description.setText(descriptions[position]);
return view;
}
And row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="match_parent"
android:layout_height="wrap_content"
android:id="#+id/title"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/description"
android:textSize="20sp"/>
</LinearLayout>
I am trying to figure out how I can change the text color of android.R.layout.simple_list_item_multiple_choice
I know you can create your own listview with checkboxes but I really do not want to have to do that as the android.R.layout.simple_list_item_multiple_choice works perfectly, I would just like to know how to change the Text color.
I have come accross this question here but I do not understand how I can use the most voted up answer.
How can I override android.R.layout.simple_list_item_multiple_choice textview so I can change the color of it. Thank you.
You can change color easily.
You just have to override the getView() method.
And inflate the android.R.layout.simple_list_item_multiple_choice in particular view. now you can access the whole template.
So that you can get the TextView used in this template by finding using id android.R.id.text1. after that you can change the behavior of textview.
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, your_data_container) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(InvitationFlowActivity.this).inflate(android.R.layout.simple_list_item_multiple_choice, null);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setTextColor("Your_color");
}
};
And if you are using custome adapter then you can also override the same getView() method and you can customize it as per your requirement.
Just make custom layout - simple_list_item_multiple_choice.xml like this -
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
and use it in place of android.R.layout.simple_list_item_multiple_choice.xml
There are two ways to do this.
1. Override the getView method
2. Use custom layout instead of standard layout.
No. 2 is better as you have more control on the same and you can edit at your ease.
Alt+Click will clone the required layout to your res/layout folder.
Make changes to the new layout and use it in the adapter.
ArrayAdapter aa = new ArrayAdapter(getApplicationContext(),R.layout.text_change,listItems);
listView.setAdapter(aa);
Here listItems is the String array of list.
text_change.xml is the updated layout xml
listView is your listview in the main layout.
In order to dynamically update color of content use the following code
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0)
{
//Intent intent = new Intent(CoffeeShop.this,DrinkCategoryActivity.class);
//startActivity(intent);
TextView txtV = (TextView)view;
txtV.setTextColor(Color.BLUE);
}
else
{
TextView txtV = (TextView)view;
txtV.setTextColor(Color.YELLOW);
Toast toast = Toast.makeText(getApplicationContext(),(CharSequence)"These Methods are yet to be made public", Toast.LENGTH_SHORT);
toast.show();
}
}
};
ListView listView = (ListView)findViewById(R.id.list_opt);
listView.setOnItemClickListener(itemClickListener);
Adding onclicklistner for the view will allow updating the color of the text upon clicking. You can add respective logic as to which color should the text be displayed.
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"/>
Basicaly, my question is similar to this one:
Scrolling a ListView changes everything from White to Black
The only difference is that I'm dealing with a TextView that changes the color upon scrolling (the TextView is inside a ListView).
I looked up if there's a method similar to setCacheColorHint(Color.WHITE) for a TextView - I didn't find it.
Perhaps I should dynamically set the default TextColor? Because currently, it's being set in XML and then changed in code.
How can I handle this?
Code for changing the color to blue:
private void highlightSelectedFile(View vw)
{
TextView fileName = (TextView) vw.findViewById(R.id.file_name);
//Log.v("color: ", Integer.toString(fileName.getCurrentTextColor()));
if(fileName.getCurrentTextColor() == Color.BLACK) {
fileName.setTextColor(Color.BLUE);
} else {
fileName.setTextColor(Color.BLACK);
removeFromSelectedFiles(new File(fileName.getText().toString()));
}
}
These TextView's go back to BLACK after I scroll the ListView they're inside in:
ListView lv = (ListView) ac.findViewById(android.R.id.list);
Only put this statement in your list in XML file. android:scrollingCache="false"
It will solve your problem.
In your adapter you might be using a data item that applies data to your views in Adapter. When you scroll, your Views are recycled and your getView() is called with a different View object. Your problem can easily be fixed. Just add one more int color; variable in you data item and use it like this.
public View getView(int position, View convertView, ViewGroup parent) {
// efficient stuff here
// after applying your data
fileName.setTextColor(data.color);
}
also change this method:
private void highlightSelectedFile(DataItem data) {
if(data.color == Color.BLACK) {
data.color = Color.BLUE;
} else {
data.color = Color.BLACK;
removeFromSelectedFiles(new File(fileName.getText().toString()));
}
// to update ListView
myAdapter.notifyDataSetChanged();
}
Below is code of my girdview. i need to change font size of the data. i aslo need to change default selection colour which is orange. please help i am new to android development.
String[] tes={"AAA","BBB","CCC"};
ArrayAdapter<String> aa = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
tes );
gv.setAdapter(aa);
gv.setOnItemClickListener(this);
You have to change the font size in the baseadapter class for the gridview.
Like this:
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(100, 80));
tv.setTextSize(20); //text size in gridview
}
The gridview just show whatever the
gridview.setAdapter(new MyAdapter (thi));" //MyAdapter class is where the getView method run
sets the setAdapter to contain.
You can try to achieve this by changing styles of your widgets. Take a look a this
post to learn to apply styles and themes. Also you'll find description of all android styles and themes there. Hope this helps.
You can change it in the XML file, if you write android:textColor="#000000" where 000000 is the code of your color hexcode what you want.
a little bit too late but for others who are looking for it will work :
first you create this a simple_textview.xml(put it a name)
<?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:textSize="10dp"
android:shadowColor="#color/grisActionBar"
/>
then in yout activity or fragment you set the adapter with the created textview.xml
gvMain = (GridView)v.findViewById(R.id.gridView);
gvMain.setAdapter(new ArrayAdapter<String
(getActivity(),R.layout.simple_textview,data));