Including padding on activities with themes in android - android

I am trying to match the style of a website with my app and on the website there is always a 10 pixel border around every page that just shows the background. I am trying to set up my activities to include that with themes, so I don't need to pad every layout I write. Is there a way for me to theme this? I've been looking through all of the available theme items and haven't been able to find it, but this is my first time using themes.

I would approach this task by setting up a margin or padding in your top layer container in the hierarchy of views in activity.
First, define an attribute.
res/attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="desiredActivityBorder" format="dimension" />
</resources>
Then, in your layout for activty use this atribute:
layout/your_activiy.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:width="match_parent"
android:height="match_parent"
android:padding="?desiredActivityBorder">
... your views go in here ...
</LinearLayout>
Of course this assumes that your LinearLayout background is transparent so one can see through to background beneath.
All you have to do know is to set a value for this attribute in your theme.
<style name="YourTheme" parent="Holo.Theme ">
<item name=" desiredActivityBorder">10dp</item>
</style>

Related

How can i find the definition of ?android:attr/dividerVertical?

I want to see the definition of divider vertical, I'm not sure exactly what the meaning of an attribute is but when click through to the definition of the attribute in intellij I'm taken to attrs.xml and shown the following, which does not help.
<!-- Drawable to use for generic vertical dividers. -->
<attr name="dividerVertical" format="reference" />
My specific problem is I'm trying to achieve a list with an inset list divider with the dividerVertical style. In order to do this I have defined my own inset shape.
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="30dip"
android:insetRight="30dip">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="?android:attr/dividerVertical"/>
</shape>
</inset>
This does not work as android will not accept the "?android:attr/dividerVertical" as the colour. It would not work anyway as divider vertical has an alpha component, so what I need to know is what colour and opacity is divider vertical?? But ideally Id like to know how I can check the source of any resource components in android so that I never encounter this problem again.
Thanks
Piers
According to developer.android, ?[<package_name>:][<resource_type>/]<resource_name> is used for References To Theme Attributes.
So, for ?android:attr/dividerVertical, you can navigate to android-sdk\platforms\android-16\data\res\values. There, looking at attrs.xml, you can see
<!-- Drawable to use for generic vertical dividers. -->
<attr name="dividerVertical" format="reference" />
But this is just a "reference" for the theme. Looking at your target platform's themes.xml, you can find many lines for different themes something like
<item name="dividerVertical">#drawable/divider_vertical_dark</item>
The line you want is the line contained within the theme element for the theme you are using. It's easiest to copy the whole theme element to another text file and search there. Which brings up
<item name="dividerVertical">?android:attr/listDivider</item>
Searching again in the theme for "listDivider" you can find
<item name="listDivider">#drawable/list_divider_holo_dark</item>
So that is a drawable.
Searching for matching files you can find
./platforms/android-16/data/res/drawable-hdpi/list_divider_holo_dark.9.png
./platforms/android-16/data/res/drawable-mdpi/list_divider_holo_dark.9.png
./platforms/android-16/data/res/drawable-xhdpi/list_divider_holo_dark.9.png
These pngs happen to be 9 patches with the content consisting of a white square with an alpha value of 38.
dividerVertical is defined in theme you are using. For example this is divider image for Holo.Light theme. There is <item name="dividerVertical">?android:attr/listDivider</item> row in themes.xml. This line is referencing forward to <item name="listDivider">#drawable/list_divider_holo_light</item>, from this you can see that it is drawable and not a color.

Eclipse GUI shows newly styled button incorrectly

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>

Define my customized theme for dialogs

I finally understood what themes are and how to apply them to a dialog.
I would like to create a theme and I need help from you to achieve what I want. I basically want to create a theme to apply to alert dialogs and to dialogs throughout my application.
In my theme, I need to define the title background color, title text size, main body background color and main body text size. What items should I use?
Here is the starting code to prove that I put some work into it :)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myGeneralDialogStyle" parent="#android:style/Theme.Dialog">
<item name="?"></item>
</style>
</resources>
IF all you got is the XML you shown above, then if your questions is about this "?" sign in name="?" part, then only thing you can use here are defined attributes. In your case you can i.e. use android:* ones. If you need to define own attributes then see here: Declaring a custom android UI element using XML

How to style second level list of ExpandableListView?

How to change style of second level list of ExpandableListView? For example, add background color or header\footer view.
Note, that I don't need to set background to each child. I need to set style for a whole second level.
Here is an example of what I'm trying to achieve (brown layout - is second level list):
As I said, I don't need to apply style for each item individually. Because I have a repeatable image at background (not just color). I know how apply styles and how to set it to each child view, but this is not what I can use to achieve such background.
EDIT
What I'm trying to achieve:
add shadow at the top of "At the cafe". It can be done easily with ListView, but how to get ListView in such case? Is there any ListView at all?
set repeatable image as a background of second level list (not for each child view individually).
One way this can be achieved is with the following.
Create a style xml in your res/values/ directory.
Now define your style however you want it. When used this way it will change all the Views where the style is applied to this particular style.
Here is my example i tested with:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="backgroundTest" parent="#android:style/TextAppearance.Medium">
<item name="android:background">#00FF00</item>
</style>
</resources>
Now add the style to your top level layout in the xml that defines the children for your ExpandableListView. For example, here is my child (note the style in my LinearLayout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/backgroundTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/rightSideTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="25dp"
android:paddingLeft="50dp"
android:paddingTop="25dp" />
</LinearLayout>
This should change the color of all your children to green, i believe. You can change the style to whatever you want. There is some great documentation that i linked that should help you out.

Is it possible to have a Button with a background image with text on top?

I need button that has a replicated background pattern and normal button text on top - how to specify this in layout XML?
Override android:background on the Button in question. Or, for reuse you could declare your own style, overriding android:background from the base button style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyButtonStyle" parent="Widget.Button">
<item name="android:background">#drawable/fancy_button</item>
</style>
</resources>
In either case, your fancy_button drawable would be an image, or more likely a StateListDrawable.
For the Button instances you wish to apply this to, you'd do:
<Button style="#style/MyButtonStyle" />.
Consider using ImageButton instead of using regular one.

Categories

Resources