Here is The Code :
spinner.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:gravity="center"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="12sp" />
Spinner :
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
Java :
array = new ArrayAdapter<String>(Activity.this,
R.layout.spinner, ArrayList);
array .setDropDownViewResource(R.layout.spinner);
Spinner.setAdapter(array );
Spinner.setOnItemSelectedListener(this);
Problem : Text is still Aligned to the Left
Use following tag on spinner
android:textAlignment="center"
Simply add in your default app theme style this two lines :
<item name="android:spinnerItemStyle">#style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">
#style/spinnerDropDownItemStyle
</item>
create the new style :
<style name="spinnerItemStyle">
<item name="android:gravity">center</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:gravity">center</item>
</style>
Thats it !
Note that the base theme used here is : Theme.AppCompat.Light and this will be applied as the default spinner styles in your app.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ellipsize="marquee"
android:textAlignment="center"
android:textSize="17sp"
tools:ignore="MissingPrefix" />
Use this Textview as the layout passed to your adapter.
android:textAlignment="center" this line is the one who makes the magic
Add this line in your Spinner,
android:textAlignment="center"
Done!
I've followed this : http://nevescheng.blogspot.fr/2013/05/spinner-with-item-text-aligned-to-center.html & this worked fine
I've noticed that i had another spinner xml in value-v11 Folder & this one was Not Modified, i've Modified it & its worked
In your layout add "theme" attribute in Spinner like -
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/_10sdp"
android:theme="#style/CustomSpinnerTheme"
android:entries="#array/entries"
android:gravity="center"
/>
and add style (in values/style file) -
<!-- Spinner style -->
<style name="CustomSpinnerTheme">
<item name="android:textSize">#dimen/_15ssp</item>
<item name="android:textColor">#color/textMainColor</item>
<item name="android:gravity">center</item>
<item name="android:textAlignment">center</item>
</style>
Enjoy :)
The best answer and was tested is by Mouloud, but in Marshmallow I had to add textAlignment to make the drop down list items centered. So, the full code based in Mouloud code is:
<!-- Spinner style -->
<style name="spinnerItemStyle">
<item name="android:textColor">#color/colorAccent</item>
<item name="android:gravity">center</item>
<item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
</style>
<!-- Spinner style drop down style-->
<style name="spinnerDropDownItemStyle">
<item name="android:textColor">#color/colorPrimaryDark</item>
<item name="android:gravity">center</item>
<item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
</style>
And add the style now to your theme style:
<!-- Change the spinner style-->
<item name="android:spinnerItemStyle">#style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">#style/spinnerDropDownItemStyle</item>
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setText(myObjs[position].getText());
label.setGravity(Gravity.CENTER);
return label;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = new TextView(context);
label.setText(myObjs[position].getText());
label.setGravity(Gravity.CENTER);
return label;
}
Related
CircularProgressDrawable
This is the library i'm using in my project.
MY APPROACH IS :
<com.rey.material.widget.ProgressView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/progressviewmain"
app:pv_progressStyle="#style/Material.Widget.ProgressView.Circular"
app:pv_circular="true"
android:background="#ffffff"
app:pv_progressMode="indeterminate"
app:pv_autostart="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
and Code is :
progressView = (ProgressView)findViewById(R.id.progressviewmain);
functions are :
private void startprogress(ListView listView, ProgressView progressView){
progressView.setVisibility(View.VISIBLE);
}
private void stopprogress(ListView listView, ProgressView progressView){
listView.setVisibility(View.VISIBLE);
progressView.setVisibility(View.GONE);
}
all of this is working fine but as there in documentation we can see CircularProgressDrawable with 2 different forms i'm not able to get 1st one in which colors are changing . How to give input the array of colors to that ?? how to use attr?
As it said in doc you can give the view array of colors with cpd_strokeColors
cpd_strokeColors - The array of colors will be used as stroke's color (for indeterminate mode).
Update 1:
To change the attributes you have to create a style in res/values/styles.xml like this:
<style name="CircularProgress">
<item name="cpd_padding">0dp</item>
<item name="cpd_initialAngle">0</item>
<item name="cpd_maxSweepAngle">270</item>
<item name="cpd_minSweepAngle">1</item>
<item name="cpd_strokeSize">4dp</item>
<item name="cpd_strokeColor">#color/colorAccent</item>
<item name="cpd_strokeSecondaryColor">#android:color/transparent</item>
<item name="cpd_reverse">false</item>
<item name="cpd_strokeColors">#array/rainbow</item>
<item name="cpd_rotateDuration">1000</item>
<item name="cpd_transformDuration">600</item>
<item name="cpd_keepDuration">200</item>
<item name="cpd_transformInterpolator">#android:anim/decelerate_interpolator</item>
<item name="pv_progressMode">buffer</item>
<item name="cpd_inAnimDuration">0</item>
<item name="cpd_outAnimDuration">#android:integer/config_mediumAnimTime</item>
</style>
Then assign it to your view:
<com.rey.material.widget.ProgressView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/progressviewmain"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:pv_autostart="true"
app:pv_circular="true"
app:pv_progressMode="indeterminate"
app:pv_progressStyle="#style/CircularProgress"/>
And it will work.
I know there is a lot of questions, with answers, but none of them works for me.
My styles and themes:
<style name="AnnaTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--
Colors,
and stuff
-->
<item name="android:dropDownItemStyle">#style/DropDownItemStyle</item>
<item name="android:spinnerDropDownItemStyle">#style/DropDownItemStyle</item>
<item name="spinnerDropDownItemStyle">#style/DropDownItemStyle</item>
</style>
<style name="DropDownItemStyle" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:textColor">#android:color/black</item>
</style>
But the result is still this:
The layout is this:
<android.support.design.widget.TextInputLayout
android:id="#+id/til_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/et_email"
android:layout_width="#dimen/join_field_width"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"/>
</android.support.design.widget.TextInputLayout>
And the adapter:
emailView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Utils.getUniqueEmailsFromAccount(this)));
Can you guys help me how to make the text black?
Try this advice then...
First try to change your Adapter to this (if it didn't work try with the second option :
emailView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, Utils.getUniqueEmailsFromAccount(this)));
1# Create a style as follows :
<style name="CustomAuto">
<item name="android:paddingTop">3dp</item>
<item name="android:paddingRight">5dp</item>
<item name="android:paddingBottom">3dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:textColor">#000</item>
</style>
2# Create a Layout as follows :
<?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/any_id"
style="#style/CustomAuto"
android:singleLine="true" />
3# Change your Adapter to :
emailView.setAdapter(new ArrayAdapter<>(this,R.layout.text_custom_view, Utils.getUniqueEmailsFromAccount(this)));
A simple solution would be creating your own list item. So, create a layout XML file and write the following codes there.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:textColor="#android:color/black"
android:padding="5dp"
android:layout_width="match_parent"
android:singleLine="true"
android:layout_height="wrap_content"/>
And pass it to the adapter's constructor.
emailView.setAdapter(new ArrayAdapter<>(this, R.layout.that_layout, Utils.getUniqueEmailsFromAccount(this)));
You can also override the Adapter:
emailView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Utils.getUniqueEmailsFromAccount(this))){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
}};
edit:
for anyone coming across this, the way I got it to work is to either:
Set the layout directly for each preference item, as in:
<CheckboxPreference
android:layout="#layout/checkbox_preference_item"/>
OR set the style via a theme as I was doing before, but define a widgetlayout and set it instead of an actual layout, like so, where preferenceStyle is set to #style/Preference and checkBoxPreferenceStyle is set to #style/Preference.Checkbox:
<Style name="Preference">
<item name="android:layout">#layout/checkbox_preference_item</item>
</Style>
<Style name="Preference.Checkbox">
<item name="android:widgetLayout">#layout/your_custom_widget</item>
</Style>
I'm trying to customize the layout of a Checkbox Preference item and the checkbox itself is not showing up. I'm modeling after android code and not sure where I'm going wrong. The text views are working fine as well as the rest of the layout but the checkbox is not showing up. Would appreciate any help with this.
Relevant code:
theme.xml:
<style name="Theme.Settings" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/SettingsActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:preferenceCategoryStyle">#style/PreferenceCategory</item>
<item name="android:preferenceStyle">#style/Preference</item>
<item name="android:checkBoxPreferenceStyle">#style/Checkbox</item>
</style>
style.xml
<style name="Checkbox">
<item name="android:layout">#layout/checkbox_preference_item</item>
</style>
<style name="Preference.Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">#dimen/preferences_item_font</item>
<item name="android:textColor">#color/preference_text</item>
</style>
<style name="Preference.Text.Extra" parent="Preference.Text">
<item name="android:textSize">#dimen/preferences_header_font</item>
</style>
checkbox_preference_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/preferences_item_height"
android:background="#color/white"
android:paddingLeft="#dimen/preferences_item_left">
<LinearLayout android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<TextView android:id="#android:id/title"
style="#style/Preference.Text"/>
<TextView android:id="#android:id/summary"
style="#style/Preference.Text.Extra"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:id="#android:id/widget_frame"
android:layout_alignParentRight="true"/>
</RelativeLayout>
I am not seeing a CheckBox in the layout xml. Am I missing something? You need to specify something like:
<CheckBox android:id="..." ... />
Then you may need to specify something like
<CheckBoxPreference android:key="..." ... />
in the preference xml file. Setting should be android:widgetLayout to the custom CheckBox.
See this post may help you more customizing checkbox preference
I using listviews and expandedviews that has dividers and I can set them but on spinner its looks like it is no divider between items.
Someone that has a idea of how to fix this?
This worked for me:
<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#d1d1d1</item>
<item name="android:dividerHeight">0.5dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
The advantage of using this is that it doesn't remove the ripple effect on hover.
I managed to find a more proper solution for this issue (without including the divider in the single item layout).
What you have to do is define in your activity's theme
<item name="android:dropDownListViewStyle">#style/App.Style.Spinner</item>
and then create the proper style with
<style name="App.Style.Spinner" parent="#style/Widget.Sherlock.Light.ListView.DropDown">
<item name="android:dividerHeight">10dip</item>
<item name="android:divider">#drawable/mydivider</item>
</style>
Based on #Talihawk answer, I made it work for specific spinner only. Instead of setting your activity theme, set the theme directly for the spinner view:
<style name="MatchSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
<item name="android:divider">#123456</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="MatchSpinnerTheme" parent="AppTheme">
<item name="android:dropDownListViewStyle">#style/MatchSpinnerStyle</item>
</style>
and
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MatchSpinnerTheme"/>
Sorry, I am answering years after the question was asked but the solution is very simple, you just have to do a simple thing.
Go to your style.xml file and add this item in your active theme
<item name="android:dropDownListViewStyle">#style/MySpinner</item>
after this add another theme with the name MySpinner and same parent of your active theme
<style name="MySpinner" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:dividerHeight">2dp</item>
<item name="android:divider">#000</item>
</style>
this will separate your single item and will not show the separator while we hover on a single item
but be sure while doing this, we are applying this theme on all the spinner in the activity. Now after every spinner is will work on this same spinner theme.
For people with same problem i after almost gived up i got an idea of how to get the divider.
I added the divider line at the bottom of my custom layout for each item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" style="#style/ListItem2">
<TextView android:id="#+id/Text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
style="#style/SpinnerView_Text" android:paddingLeft="10dip" />
<ImageView android:id="#+id/icon" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/arrowright"
android:layout_alignParentRight="true" android:layout_centerInParent="true"
android:layout_marginRight="20dip" />
</RelativeLayout>
<ImageView android:id="#+id/Divider1" android:layout_width="fill_parent"
android:layout_height="1dip" style="#style/Divider"></ImageView>
None of the other solutions worked for me, so I used a drawable as the background of the Spinner items to produce the desired effect.
I have created a new Drawable dropdown_divider.xml and a custom SpinnerAdapter class where I adapted the getDropDownView() method to set the background to the Spinner items.
The android:bottom="-56dp" in the drawable is what centers the line perfectly between two items for me, but that depends on the exact margins and paddings that you've applied in your layout.
dropdown_divider.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="16dp"
android:right="16dp"
android:bottom="-56dp">
<shape android:shape="line" >
<stroke
android:width=".2dp"
android:color="#FF666666" />
</shape>
</item>
</layer-list>
SpinnerAdapter:
#Override
public View getDropDownView(int position, View convertView,
#NonNull ViewGroup parent) {
TextView text = (TextView) super.getDropDownView(position, convertView, parent);
text.setBackground(context.getDrawable(R.drawable.dropdown_divider));
label.setTextColor(Color.BLACK);
label.setText(lists.get(position).getTitle());
return text;
}
The result looks like this:
How to I tell my own custom Spinner Layout to use my Theme?
Style:
<style name="SpinnerText" parent="#android:style/Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/AnswerTextElement</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>
Theme:
<style name="ApplicationTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">#style/Button</item>
<item name="android:spinnerStyle">#style/Spinner</item>
<item name="android:spinnerItemStyle">#style/SpinnerText</item>
</style>
This works for default Spinners, however does not work with my custom layout:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:id="#+id/text1"
android:text="label name" android:layout_width="fill_parent"
android:layout_toLeftOf="#+id/check1"></TextView>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/check1"
android:layout_alignParentRight="true" android:clickable="false"
android:focusable="false" android:focusableInTouchMode="false"
style="#style/CheckBoxPlainBackground"></CheckBox>
</RelativeLayout>
SOLVED:
You need to add
?android:attr/spinnerItemStyle
to the style attribute of the layout. Now it will act like the actual Spinner Item and all changed to the Theme will apply.
By the way - I found this in the SDK under YOUR_SDK\platforms\android-1.6\data\res\layout\simple_spinner_item.xml
You will be able to find other styles for other controls in this directory.
You want your TextViews in the RelativeLayout to use the same text style?
Add "style=#style/SpinnerText" attribute to the TextView.