In my application scroll bar is just too skinny to use. Does anyone know how to change the width of this?
Looking around in the platform resource files, I found this in res/values/themes.xml under the main Theme:
<item name="scrollbarSize">10dip</item>
<item name="scrollbarThumbHorizontal">#android:drawable/scrollbar_handle_horizontal</item>
<item name="scrollbarThumbVertical">#android:drawable/scrollbar_handle_vertical</item>
So, it looks like if you create your own Theme, as described in the documentation, you can provide your own drawables for the scrollbars.
I do wonder what you mean by they are "too skinny to use". They aren't meant to be touched/dragged. They are meant for display, to show you your current position relative to the entire ScrollView.
Instead of creating your own theme dedicated to your ScrollView, you can set the android:scrollbarSize attribute.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarSize="2dp" />
Related
I have a chip with 100dp of width but the text is not centered how I can center the text.
I use androidx with material library, I've tried put android:textAlignment="center" and android:gravity="center" but not work
<com.google.android.material.chip.Chip
android:id="#+id/chip"
style="#style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="7:00" />
I have this
I want this
just now I faced with the same problem, and I solved it by set a chip property: android: textAlignment = "center". I tested your example and it works fine too, here the code that I tested:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.chip.Chip
android:id="#+id/chip"
style="#style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="7:00"
android:textAlignment="center"/>
</FrameLayout>
Also make sure that you don't set or change a chip's text alignment somewhere in your code.
The short answer:
Chips aren't meant to be used the way you are trying to use them. They are supposed to wrap your content. Therefore there isn't a clean way to align the text in the center.
There is a workaround tho, you can use Chip_textEndPadding and Chip_textStartPadding attributes, which will be kinda awkward I guess.
I don't really know what you are trying to achieve, I mean, what is your why? Is it a button? Is it suppose just to show some text?
Please describe the feature, or at least, part of it.
Anyway:
According to the material design guidelines
Chips allow users to enter information, make selections, filter content, or trigger actions. Chips should appear dynamically as a group of multiple interactive elements. Unlike buttons, which should be a consistent and familiar call to action, one that a user expects to appear as the same action in the same general area.
Does your feature as anything to do with this?
In case you want a clickable, circular component you can simply use material button.
There is a similar question that was asked at github.
as others said you can use textAlignment ...but i wanted to tell you that if your using a custom font it wont be perfectly vertically aligned. you can check here for explanation.
so i would make a custom style that inherits from chip styles and set the font padding for usage like this:
<style name="customStyle" parent="#style/Widget.MaterialComponents.Chip.Choice">
<item name="chipBackgroundColor">#color/white</item>
******* <item name="android:includeFontPadding">true</item> *************
</style>
then for the text appearance you can make another style:
<style name="CustomChipTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
<item name="android:fontFamily">?attr/myFont</item>
<item name="android:textAlignment">center</item>
</style>
dont forget to force a bridge theme in xml:
<com.google.android.material.chip.Chip 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"
style="#style/customStyle"
**** android:theme="#style/Theme.MaterialComponents.Bridge" ****
***** android:textAppearance="#style/CustomChipTextAppearance" *******
app:chipMinHeight="38dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:chipStrokeWidth="2dp"
app:rippleColor="#android:color/transparent"
tools:chipText="my chip" />
Use isTextAlignmentResolved,
For example, chipname.isTextAlignmentResolved() to do this programmatically.
When building my app, I started just using the Theme.Light.NoTitleBar.Fullscreen theme. I built all my layouts for the whole app like this, and got things to look how I want them. Some drawables used in the layouts have their size specifically set, and others are set to wrap_content.
I then decided to switch to the Holo light theme. When I do this, all the drawables used in layouts that are set to wrap_content end up larger. Almost as if they are pulling from a larger bucket. In fact, some look like they've been stretched.
I know the background is black in the older theme one, but that's not an issue (this is actually a layout file that is included in another layout). Obviously there's quite a difference in size between the two.
Here is just my guess based on what I read in this thread.
It can be because you use those images as background property of Button views. This is not safe because depending on default margin values - which are defined in the Theme - Buttons can stretch background images as they need to. If this is the case, then you need to use ImageButton views instead and use setImage*() method to assign images. There you can use scaleType property as it was mentioned by Carlos Robeles.
The only thing that comes to my mind, is that the different themes has different values for the defaultandroid:scaleType attribute of the image views.
Please, try specifying the attribute as some that is good for you, and see what happens using the 2 different themes. For example you can use android:scaleType="center", so your ImageViews would be something like
<ImageView
android:scaleType="center"
android:width="wrap_content"
android:height="wrap_content"
android:src="...
Yo can take a look at the different scale types in the ImageView reference:
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
It is not easy to understand what's the meaning of every type, so the best is to take a minute to play with them
My guess is that for some reason, the Holo theme is rendering your images in a lower resolution than Light. I mean that for instance you have your drawables in the drawable-xhdpi and Holo is treating them as drawable-hdpi. In fact, I don't have any evidence of that, but recently I've been messing around with resolutions and the difference seems very familiar to me.
If you don't have your drawables in the drawable-xxhdpi (the biggest resolution) folder, you could try putting them into a higher lever resolution folder, to see what happens.
From android's source code, see https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
The style which your button will be used in Holo.Light is
<style name="Widget.Holo.Light.Button" parent="Widget.Button">
<item name="android:background">#android:drawable/btn_default_holo_light</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<item name="android:textColor">#android:color/primary_text_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
</style>
See the last two lines. It has default minHeight and minWeight. That's why your button is stretched.
Solutions
1. Set minHeight and minWidth of your Button to 0.
2. Use a custom style like this.
<style name="MyHoloLightButtonStyle">
<item name="android:background">#android:drawable/btn_default_holo_light</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<item name="android:textColor">#android:color/primary_text_holo_light</item>
</style>
3. Use a ImageButton, and set your images by setImage*(not setBackround*) method.
I'm starting with a fully working app, with all the buttons in the right locations and the right sizes... but now I wanted to try out using styles for the first time. In particular I wanted to have the text colour in my buttons a dark blue and the background white. So I wrote the following in styles.xml in res/values ->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="mybut" parent="#android:style/Widget.Button">
<item name="android:textColor">#color/dblue</item>
<item name="android:background">#ffffffff</item>
</style>
</resources>
I modified my button code as follows:
<Button
android:id="#+id/spec"
style="#style/mybut" <-- I added this line here
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:text="#string/spec" />
In eclipse's XML viewer, the new button looked right in every way. But then at run time, on my android device, the button's height had shrunk by about a third! Any ideas?
EDIT: I'm not very confident about the parent="#android:style/Widget.Button" bit. I'm suspicious that perhaps I'm somehow already using some other style?/theme? and perhaps the line should look something akin to parent="#android:otherstyle/Widget.Button" or parent="#android:style/other.Widget.Button"... or similar.
EDIT: FYI... I'm trying this out on a kind of "home screen" activity which just contains two big buttons. I added the style="#style/mybut" to just one of the two buttons. They are now clearly very different sizes.
EDIT: I noticed that in the manifest I have android:theme="#android:style/Theme.NoTitleBar.Fullscreen" ... does that mean I need to make my button's parent android:theme="#android:style/Theme.NoTitleBar.Fullscreen.Widget.Button" ?? or something like that?
By default, a Button has a 9-patch background, not a simple color. This image has padding and a content area which alters the actual size of the button.
When you change the background, you're stripping that padding, and it appears smaller. The correct way to do this is to create a new 9-patch, based on the old, but with the colors changed.
The problem must be here:
android:layout_weight="0.12"
If you have an action_bar or something like that when you run your app the button is going to shrink. In your preview the action_bar doesn't appear (I'm just guessing).
EDIT:
Here is a sample of a custom button that I use in my app, I put a min height and width.
Instead of using: parent="#android:style/Widget.Button" I use: parent="android:Widget.Button"
<style name="ButtonCustom" parent="android:Widget.Button">
<item name="android:background">#drawable/btn_default_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">#fff</item>
</style>
I am trying to create a layout (using eclipse) in which I need to vertically align various controls like TextView and Button. I am trying to keep all the widgets perfectly left aligned. Even if I specify the same left margins/paddings for the controls, still a difference of 1-2 pixels can be seen between different types of controls.
The problem is that the distance between widget’s border (blue rectangle in eclipse) and widget’s content/graphics varies across widgets (say TextView and Button).
I can apply workarounds by either specifying left padding for TextView or by reducing the left margin of the button container. But I am looking for cleaner solution. I am unable to find any attribute which controls the difference between widget's border and content.
Any pointers on how I can control this gap ?
Snapshot demonstrating the problem is below. Here is the layout XML that I am using for this problem:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Button" />
</LinearLayout>
The images below show the snapshot of the dummy application. Please note the difference between left side margin of "Hello World" TextView and "Dummy Button".
The second pic shows the button widget when selected in Eclipse. The blue rectangle indicates the widget boundary/border. Is the difference between button's border (blue rectangle) and content (greyed rectangle) controllable by some property ?
This is a tough one the TextView and other Android widgets may have some intrinsic styles of their own (android version dependant as well).
Therefore to get around this you would have to create your own style.
In creating your own style always reference the android source code on this subject:
https://github.com/android/platform_frameworks_base/tree/master/core/res/res/values
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
There are multiple ways to create your own style.
One way is to theme your activity in your manifest.
First you need a styles.xml file, in your /values/ folder.
In here you would declare your new style:
<style name="Theme.MyTheme.Dark" parent="#android:style/Theme.NoTitleBar">
<item name="android:textViewStyle">#style/Widget.TextView.Black</item>
</style>
<style name="Theme.MyTheme.Light" parent="#android:style/Theme.NoTitleBar">
<item name="android:textViewStyle">#style/Widget.TextView.White</item>
</style>
The style above it inheriting from the Android style that hides the title bar, you can inherit from something else.
In this theme we then override the textViewStyle, this allows us to set custom values for our TextView's and override some of the intrinsic values.
<style name="Widget.TextView.White" parent="#android:style/Widget.TextView">
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="Widget.TextView.Black" parent="#android:style/Widget.TextView">
<item name="android:textColor">#000000</item>
</style>
Finally you theme your activity in the AndroidManifest.xml:
<activity
android:name=".ui.phone.FirstActivity"
android:theme="#style/Theme.MyTheme.Dark" />
<activity
android:name=".ui.phone.SecondActivity"
android:theme="#style/Theme.MyTheme.Light" />
Now when you use a TextView in the FirstActivity it's text will be Black by default and in the second activity it will be white.
For your specific question:
You would have to go look in the source code files I linked at the top and see if there is any padding or minWidth or size attributes that are affecting your widgets and your layout.
I've been searching the solution for hours: how to apply a simple theme or style to an application, an activity or just a view? It seems super easy but my styles always get ignored.
Here is the code in style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="master" parent ="#android:style/Theme.NoTitleBar">
<item name="android:typeface">serif</item>
<item name="android:textColor">#8b8378</item>
</style>
</resources>
and here is the code in AndroidManifest.xml:
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/master">
and code in a ListView
<ListView android:layout_height="wrap_content"
style="#style/master"
android:id="#+id/list"
android:layout_width="fill_parent">
</ListView>
NOTHING ever happens. Font style, color all remain the default. Only by declare the attributes explicitly like
<TextView android:gravity="center" android:typeface="serif" android:textColor="#8b7d6b" android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/text_intro" android:layout_height="wrap_content" android:text="#string/welcome_text" android:layout_width="wrap_content" android:padding="20sp" android:layout_weight="0"></TextView>
will work. I know eclipse doesn't support preview of theme and style, but they don't work on emulator as well.
Help please! I can't believe I have been stuck with this tiny issue for a week... Thank you in advance!
There are a few things about Android styles and resources at work here.
Android styles are an extremely general facility, and the result is that some configurations are possible but invalid and will be ignored. You've run across a few. :)
When applied to a view, a style will be equivalent to setting those same attributes on that view. Styles do not cascade to child views. typeface and textColor aren't valid on a ListView, so they are ignored. They also aren't going to work on a theme that way, since themes provide default styles for different kinds of views. (Why are invalid attributes silently ignored instead of generating an error? Because as new attributes are added in later platform revisions, older devices should ignore extra attributes that they don't know how to parse for compatibility.)
The best way to accomplish what you're trying to do is likely to be:
Create a style for TextViews. (This shouldn't have a parent that is a theme like your pasted code does.)
Apply that style to the TextView in your list item layout using the style= syntax.