How to decrease padding in NumberPicker - android

How to decrease padding in NumberPicker
I want something like it:

It's surprisingly easy to archive:
(scaleX and scaleY equals 2.5)
(without scaleX and scaleY)
String[] values = {"Public", "Shared", "Private",....};
NumberPicker np=
(NumberPicker) findViewById(R.id.numberPicker);
np.setMaxValue(values.length-1);
np.setMinValue(0);
np.setDisplayedValues(values);
And simply set small layout_height and scaleX, scaleX:
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:scaleX="2.5"
android:scaleY="2.5"/>
I do agree, that standard NumberPicker is hardly customizable, though.
I hope, it helps

Unfortunately, number picker is not style-able.
I advise on using a library such as the one by SimonTV

This is probably a bit late but you can set the explicit height on the NumberPicker it then follows the given height and adjusts the space between the items.

As Grebulon pointed its very simple to customize the picker if you are using the libray by .
These are the code and the results-
<net.simonvt.numberpicker.NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:internalMaxHeight="100dp"
app:selectionDividersDistance="30dp"/>
I was forked it once to increase the number of selector wheels.
Here is the output of above code.

Try to customize your NumberPicker Theme like below:
<style name="Widget.Holo.NumberPicker" parent="Widget.NumberPicker">
<!-- Customize your theme here -->
<item name="android:selectionDivider">#android:drawable/numberpicker_selection_divider</item>
<item name="android:selectionDividerHeight">2dp</item>
<item name="android:selectionDividersDistance">25dp</item>
<item name="android:internalMinWidth">50dp</item>
<item name="android:internalMaxHeight">100dp</item>
</style>
Hope it's help your.

A simple solution is to decrease the height of the NumberPicker that will result in decrease of internal spacing between the numbers as well, in my case I have set it to 120dp and that just does the job
android:layout_height="120dp"
Here is the complete code for my NumberPicker
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_marginEnd="16dp"
android:theme="#style/myNumberPicker" />
For styling I have used the following theme in my style file
<!-- Custom style for Number Picker -->
<style name="myNumberPicker" parent="Theme.MyApplication">
<item name="android:textSize">26sp</item>
<item name="android:textColorPrimary">#color/textColorBlue</item>
<item name="colorControlNormal">#android:color/transparent</item>
<item name="android:fontFamily">#font/montserrat_semi_bold</item>
</style>

Related

Spinner DatePicker does not change text color

I am trying to change the text color from the DatePicker using an XML style with no luck. My app theme is Theme.MaterialComponents.Light and the style code that I am using is the following.
<style name="MyDatePicker" >
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="colorControlNormal">#color/colorAccent</item>
</style>
<DatePicker
android:id="#+id/datePicker"
style="#style/MyDatePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:datePickerMode="spinner" />
I have seen many posts tackling this issue, but unfortunately none of the answers worked for me. I am using version 1.2.0 of the material library.
Is there any other way to solve this issue? Thanks!
Since DatePicker is a ViewGroup, you apply the defined style to it (and its children) using the theme attribute.
style, in contrast, is applied to the parent DatePicker only and its children remain unaffected.
So change to:
android:theme="#style/MyDatePicker"

Android textview shadow doesn't work using style.xml

I have a textview that I set its style to a style I made with a shadow. I declared the settings I want in the style.xml InfoTextstyle and set the textview style to the style but it doesn't work.
This is the style.xml:
<style name="InfoTextStyle" parent="AppBaseTheme">
<item name="android:textColor">#fff</item> <- works
<item name="android:textSize">18sp</item> <- works
<item name="android:shadowColor">#ff0000</item> <- don't works*
<item name="android:shadowRadius">5.0</item> <- *
<item name="android:shadowDx">2.0</item> <- *
<item name="android:shadowDy">2.0</item> <- *
</style>
& this is the activity_main.xml:
<TextView
android:id="#+id/brightness"
style="#style/InfoTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:text="#string/brightness"
android:textAppearance="?android:attr/textAppearanceMedium" />
I'm new to android, so I'm not sure what's the problem.
A few things to try:
Look on a real device, not in Eclipse "Graphical layout" which does not support text shadow.
Decrease the shadow radius to 1. The larger the radius, the more blurred the shadow is.
Check if the style file you wrote in in the main "values" directory or under values-?dpi. Maybe your device dpi does not target your style file
Use this XML code in your TextView declaration instead of using Styles
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A light blue shadow."
android:shadowColor="#00ccff"
android:shadowRadius="1.5"
android:shadowDx="1"
android:shadowDy="1"
/>
-android:shadowColor Shadow color in the same format as textColor.
-android:shadowRadius Radius of the shadow specified as a floating point number.
-android:shadowDx The shadow’s horizontal offset specified as a floating point number.
-android:shadowDy The shadow’s vertical offset specified as a floating point number.
Also use this link to pick your Color code
http://www.w3schools.com/tags/ref_colorpicker.asp
EDIT:
TextView textv = (TextView) findViewById(R.id.textview1);
textv.setShadowLayer(1, 0, 0, Color.BLACK);
Also take a look at this link for Style way
https://stackoverflow.com/a/2487340/1364896

How to set the custom size of the stars in RatingBar

I have already added a style
<style name="myRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:minHeight">32dp</item>
<item name="android:maxHeight">32dp</item>
</style>
However instead of scaled stars I'm receiving cropped stars:
Is there a way to change the size of the stars?
Another approach would be scaling the widget:
android:scaleX="0.5"
android:scaleY="0.5"
Android won't scale a rating bar icons. When you decrease the minHeight and maxHeight android will always crop the icons as shown on the picture. The workaround for it and seems to be the only one solution is to provide your own icons for stars with the desired dimensions and set the minHeight and maxHeight to the size of the icons.
//you can also try
<style name="myRatingBar" parent="#android:style/Widget.RatingBar.Indicator">
<style name="myRatingBar" parent="#android:style/Widget.RatingBar.Small">
Ref: /android-sdk/platforms/android-10/data/res/values/styles.xml
<style name="Widget.RatingBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#android:drawable/ratingbar_full</item>
<item name="android:indeterminateDrawable">#android:drawable/ratingbar_full</item>
<item name="android:minHeight">57dip</item>
<item name="android:maxHeight">57dip</item>
<item name="android:thumb">#null</item>
</style>
<style name="Widget.RatingBar.Indicator">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#android:drawable/ratingbar</item>
<item name="android:indeterminateDrawable">#android:drawable/ratingbar</item>
<item name="android:minHeight">38dip</item>
<item name="android:maxHeight">38dip</item>
<item name="android:thumb">#null</item>
<item name="android:isIndicator">true</item>
</style>
<style name="Widget.RatingBar.Small">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#android:drawable/ratingbar_small</item>
<item name="android:indeterminateDrawable">#android:drawable/ratingbar_small</item>
<item name="android:minHeight">14dip</item>
<item name="android:maxHeight">14dip</item>
<item name="android:thumb">#null</item>
<item name="android:isIndicator">true</item>
</style>
You solved your problem. But this may help others.
I have same issue.All solutions I got does not work for multiple screen sizes. After spending hours I ended with following solution.
Just get drawable height which you want to use at runtime and set it to RatingBar. Here is sample code.
Drawable starDrawable = getResources().getDrawable(R.drawable.YOUR_IMAGE);
int height = starDrawable.getMinimumHeight();
LayoutParams params = (LayoutParams) ratingBar.getLayoutParams();
params.height = height;
ratingBar.setLayoutParams(params);
Maybe we can use the another alternative that android provides small size of rating stars.
Adding style="#style/Widget.AppCompat.RatingBar.Small" to the RatingBar.
<RatingBar
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/list_item_margin"
android:layout_marginLeft="#dimen/list_item_margin"
android:numStars="5"
android:isIndicator="true"
style="#style/Widget.AppCompat.RatingBar.Small"/>
Look at this Custom Rating Bar in Small Style .
I think you have to use the Custom Style for Rating Bar . Here is the Example.
<RatingBar
android:id="#+id/AVL_rating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ASD_cardlayout"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="#dimen/_10sdp"
android:isIndicator="true"
android:numStars="5"
android:rating="5.0"
android:secondaryProgressTint="#color/clr_red"
android:stepSize="1.0" />
You can set it in the XML code for the RatingBar, use scaleX and scaleY to adjust accordingly. "1.0" would be the normal size, and anything in the ".0" will reduce it, also anything greater than "1.0" will increase it.
You can also refer the link for the using the lib. for the custom rating bar, Click this link and get custom ratingbar and Enjoy the Rating bar.
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.5"
android:scaleY="0.5" />
This is not exactly an answer but I would like to show an alternative. Using a library called Iconify that allow you to use scalable vector icons on android apps and with a simple method you can create awesome ratingbar with icons.
The method
public static String montarEstrellasValoracion(float valoration,int starSize) {
String result="";
float cont=1f;
for(float i=1f;i<=valoration;i++){
if(valoracion==0){
break;
}
cont=i;
if((cont+0.5f)>=(valoration) && valoracion!=5f){
result+="{fa-star "+starSize+"dp} ";
result+="{fa-star-half-o "+starSize+"dp} ";
cont++;
break;
}else if((cont+0.5f)<(valoration) && (cont+1f)>(valoration) && valoration!=5f){
result+="{fa-star "+starSize+"dp} ";
result+="{fa-star "+starSize+"dp} ";
cont++;
break;
}
if(cont<=5){
result+="{fa-star "+starSize+"dp} ";
}
}
for(float j=cont;j<5f;j++){
result+="{fa-star-o "+starSize+"dp} ";
}
if(valoration==0){
result+="{fa-star-o "+starSize+"dp}";
}
return result;
}
And then on the onCreate method:
IconTextView valoracion = (IconTextView)item.findViewById(R.id.id_icon_textview);
valoracion.setText(montarEstrellasValoracion(valoration,15));
valoracion.setTextColor(Color.parseColor("#000");
The icons are from another awesome site called Font Awesome
Hope this help anybody!
In addition to scaling shown by xleon, set transformPivotX="0dp" and then set a custom height.
android:scaleX="0.7"
android:scaleY="0.7"
android:transformPivotX="0dp"
android:height="40dp"
just add font-size ppt to in style : style='font-size: 1.7vw'

How to change textsize in datepicker?

The default textsize in datepicker is too big for my app. I've seen a way suggested to change it here, but fishing around for the textviews nested inside the datepicker class seems clunky and error prone.
Is there a better/cleaner way to do it?
Setting a theme to DatePicker layout and adding android:textSize to it works for me.
In your layout's xml add a DatePicker applying a theme as shown below -
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/NumberPickerStyle"
android:datePickerMode="spinner"
android:calendarViewShown="false"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
and then define the NumberPickerStyle in styles.xml specifying android:textSize like this -
<style name="NumberPickerStyle">
<item name="android:textSize">#dimen/number_picker_text_size</item>
</style>
The easiest way to change the font size of datepicker/timepicker/numberpicker is customizing the theme.
In styles file, set the default font size in the following way.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textSize">22sp</item>
</style>
Sean's approach made some glitches in the UI of picker itself in case there're a number of pickers and I think it's not perfect to apply text size to all the components under the picker.
Below is what I've used and it works perfect without any UI glitches.
<style name="PickerTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:editTextStyle">#style/PickerEditText</item>
</style>
<style name="PickerEditText" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textSize">24sp</item>
</style>
Sean's code works. but it is changing font size for all the other components also(textViews, buttons, etc...) So Here is the solution.
Create different style for time picker and use it on time picker theme.
<style name="my_time_picker_style">
<item name="android:textSize">24sp</item>
</style>
and use it on your timepicker
android:theme="#style/my_time_picker_style"
Look at video, how I did https://youtu.be/JMJ2ujhk9c0
use below code:
ViewGroup childpicker = (ViewGroup)datePicker.findViewById(Resources.getSystem().getIdentifier("month", "id", "android"));
EditText mornthEt = (EditText)childpicker.getChildAt(1);// month widget
//change textsize and textcolor for mornthEt
mornthEt.setTextSize(30);
mornthEt.setTextColor(Color.GREEN);
use below code
DatePicker picker = (DatePicker)findViewById(R.id.dp_date);
ViewGroup childpicker
= (ViewGroup)
findViewById(Resources.getSystem().getIdentifier("month" /*rest is:
day, year*/, "id", "android"));
EditText textview = (EditText)
picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input",
"id", "android"));
textview.setTextSize(30);
textview.setTextColor(Color.GREEN);

Styles, minHeight/maxHeight: I cannot get the maxHeight to override the theme minHeight

Im thinking that more "global" styles are always overridden by more "local" Styles. For example, if I redefine all Buttons to have textSize=40dip (apply that Style as a Theme for the Application) and then apply another Style to a specific Button that says textSize=10dip, then that specific Button should get 10dip textSize.
And that is how it works, usually. But not when it comes to maxHeight. Here is the scenario:
In my styles.xml I have one Style where I inherit the default Button and change textSize and minHeight, and then another Style that sets some other values (but also inherits from Button), like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Button" parent="#android:style/Widget.Button">
<item name="android:textSize">26dip</item>
<item name="android:minHeight">60dip</item>
</style>
<style name="ButtonHeader" parent="#android:style/Widget.Button">
<item name="android:textSize">18dip</item>
<item name="android:minWidth">70dip</item>
<item name="android:maxHeight">10dip</item>
</style>
</resources>
I apply the first Style as a theme for my Activity which makes all the buttons larger (minHeight=60dip). But I have a "header" (where I have some other buttons) that I do not want to have a minHeight of 60dip, and for those buttons I want to use the ButtonHeader, setting the maxHeight to 10dip.
In my header.xml it looks like this:
<Button style="#style/ButtonHeader" android:text="UPP" android:id="#+id/Header_Button_UPP" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button style="#style/ButtonHeader" android:text="ALT" android:id="#+id/Header_Button_ALT" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button style="#style/ButtonHeader" android:text="NAV" android:id="#+id/Header_Button_NAV" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button style="#style/ButtonHeader" android:text="HIS" android:id="#+id/Header_Button_HIS" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
I am specificly styling the buttons, overriding the "global" theme. It works in some parts; the textSize for these header-buttons is correctly set to 18dip, but the maxHeight is ignored - these buttons also increase in height to 60dip.
If I, in the style for ButtonHeader, set android:minHeight="100dip" the buttons in the header will increase in size to 100dip, overriding the Theme.
But, as stated above, when I have android:maxHeight instead, nothing happens.
What am I missing?
Shouldn't your ButtonHeader style override the minHeight property? It seems like you end up with minHeight=60dp and maxHeight=10dp, and minHeight wins. If you explictly set the minHeight to something else, you shouldn't have that problem.
as I underestand you have 2 different style for Button (Button - HeaderButton) and in HeaderButton Styles you set minHieght and MaxHeight (max height isworking) and now MinHeight face with an issue,
I implement your code and find that your MinHeight is not suitable for 18dip textsize, so you should either decrease your tex size or increase minheight size
let me know, whats happen to you

Categories

Resources