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.
Related
I'm using Chip component from material library in my android app. It is an action chip which has an icon and a text inside. the point is that I need to add some padding to the top of the text in order to bring it down a little a bit.
I have tried to add textAppearance style to the chip, but can not find any style item to do what i need. how to do that?!
<com.google.android.material.chip.Chip
android:id="#+id/chip"
android:layout_width="wrap_content"
app:chipIcon="#drawable/satisfied"
app:chipIconTint="#444"
android:layout_height="wrap_content"
android:text="my text!"
android:textAppearance="#style/Widget.App.Chip" />
<style name="Widget.App.Chip" parent="Widget.MaterialComponents.Chip.Action">
<item name="android:textSize">14dp</item>
<item name="android:fontFamily">#font/xyz</item>
<item name="android:textStyle">bold</item>
// what item is needed to achieve top padding?!
</style>
simply: you can't. Chip is designed as is and it extends AppCompatCheckBox and further TextView (doc HERE). so these icons on the left and/or right are compound drawables, not separated Views (e.g. ImageViews), so every padding you will set to Chip will add padding to text and all icons (as this is one View). for your purposes you must create own custom View, easiest way would be probably to create horizontal LinearLayout with childs: TextView and ImageViews on left and right to it, and then you can set additional padding only for text
I'm new to android and I have been trying to find out an answer to this. I have a layout file .xml for listview that has two textviews, one is for heading with a greater size and other below is for description. What I want is that heading textview's text stays white and description textview's text stays grey in my current theme. That is dark theme. But user has the option to change the theme, so when user selects Light theme I want my heading textview's text (that is within layout that I have for listview's rows) to become black and also the description textview's text to become black.
Please help and thanks in advance.
you can set the text color programmatically using the method textView.setTextColor(int color), or via XML using the attribute android:textColor="#yourhexcolor"
You can change theme programmatically. There is one guy asked this hours ago. Can you check this: how-to-change-colour-when-user-wants-another-colour-in-the-app
Also if you want to use diferent themes for different TextViews you can try like :
textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
How can i create custom themes for TextViews
in your styles.xml create different styles :
<style name="MyBlueTextTheme" parent="#android:style/TextAppearance.Medium">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#123456</item>
<item name="android:textStyle">bold</item>
</style>
Then use it like :
<TextView
android:id="#+id/textBlue"
android:text="This is a blue styled text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/MyBlueTextTheme" />
Or use it like: textView.setTextAppearance(context,R.style.MyBlueTextTheme);
but its deprecated and you can use textView.setTextAppearance(R.style.MyBlueTextTheme); but this can be used Api23 or higher
So I am trying to change my app color to blue and some of most the views I have are not willing to cooperate with me.
Here is the image:
Here I want to change the color of the green parts on the spinner, edittext and checkbox views (which are green) to black or blue.
I've looked all over Stack Overflow and I can't find the solution!
Thank you very much, If possible I would like to have a XML solution but I wouldn't mind a programmatic solution!
Add these to your base theme in styles.xml
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
Note: The above change will affect the EditTexts and other views probably throughout the application.
If not, and if you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="#style/Theme.App.Base.
This will ensure the style won't also affect other views in your layouts that you don't want to change
Also if you want to change the above solution, just add another Theme specific to EditTexts and Spinners and apply it to all Spinners if you want
<style name="MyWidgetTheme">
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
</style>
and in your EditText, Spinner or any other View, just assign this theme:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Demo"
android:lines="1"
android:theme="#style/MyWidgetTheme"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyWidgetTheme"></Spinner>
Take a look to this resource generator.
Choose your color, the widgets you want to generate and voila! You copy them to your project and reference them in your xmls.
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>
When doing web development, you can inspect an element and see which classes provide which css rules. Is there an equivalent for Android development?
TLDR; Here's an example of a style inheritance problem that I had and solved:
I had a dialog has the Holo theme, but the text color was dark, even when I tried to set the text color to white.
This is the dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Theme.Dialog"/>
</RelativeLayout>
In the style.xml resource:
<style name="Theme.Dialog" parent="android:style/Theme.Holo.Dialog">
<item name="android:windowTitleStyle">#android:style/TextAppearance.Large</item>
<item name="android:textColor">#color/solid_white</item>
<item name="android:windowNoTitle">true</item>
</style>
It turned out that I was using a Fragment whose Activity was a ListActivity, and it defined getView, which created the view from an xml style that set the text color to be dark. It would have liked to see what was setting the text color.
I agree with the comments on your question, but for future, if you want to get an idea of the view hierarchy (not styles) for your app - you can do so in Eclipse.
Run the app.
Window > Open Perspective > DDMS.
In the DDMS pane, click the icon to the left that looks like a stack of phones (next to the camera icon)
You can mouse around the screen and get a visual representation of the view hierarchy.
Might help in some way.