How can I automatically apply the style of the parent? - android

I have a layout that contains a custom layout. I would like to pass the style from one element to another. I have a class which inflates the layout, which I can post if required. This element of design is added by 2 different layouts, with different sizes depending on what is required.
Child layout
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<TableRow android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_width="fill_parent" android:id="#+id/tableRow1">
<Button android:text="1" android:id="#+id/nb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
<Button android:text="2" android:id="#+id/nb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
<Button android:text="3" android:id="#+id/nb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
</TableRow>
</TableLayout>
And here's the parent layout
Parent Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<pearsonartphoto.AJEG.number_viewer android:id="#+id/numberviewer" style="#style/bigViewer" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
</pearsonartphoto.AJEG.number_viewer>
</RelativeLayout>
What I would like to do is to pass in a style, and have the style be repeated through all of the children, or at least the textView elements. What do I need to do to make this happen?

As far as I know there is no way in android to inherit the style of parent to the children views, not by just nesting them !
the thing that you could to is to apply a style to the parent
let say styleA, and than apply a style to the children elements let say styleB
and the inheritance could be done in the styles, I mean styleB can inherit properties from styleA and also override some of his parent values
fill_parent
wrap_content
#00FF00
monospace
fill_parent
wrap_content
#00FF00
monospace
the bad news is that you must put style in every children element :-(
I think this is the only way to give a style to some element, if you want to give a style to group of elements than you must define theme and apply it to the activity or to the application this is done usually in code but it could be done pro programmaticlly also.

Related

How do I automatically `layout_below` sequential controls in an Android layout?

I often seem to make Android layouts that have a series of controls that are meant to sit one below the other. For example
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/a"/>
<TextView
android:id="#+id/b"
android:layout_below="#+id/a"/>
<TextView
android:id="#+id/c"
android:layout_below="#+id/b"/>
<TextView
android:id="#+id/d"
android:layout_below="#+id/c"/>
<TextView
android:id="#+id/e"
android:layout_below="#+id/d"/>
</RelativeLayout>
The android:layout_below attributes are necessary: without them the TextViews all bunch up together in the same place.
They are also, usually, redundant and a general source of bugs and tedium. As control IDs change, as controls are added and removed, all of these strings must be edited to match up properly. To illustrate the general redundancy of this scheme, note how it promotes this sort of spaghetti:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/e"
android:layout_below="#+id/d"/>
<TextView
android:id="#+id/b"
android:layout_below="#+id/a"/>
<TextView
android:id="#+id/d"
android:layout_below="#+id/c"/>
<TextView
android:id="#+id/a"/>
<TextView
android:id="#+id/c"
android:layout_below="#+id/b"/>
</RelativeLayout>
I can see how explicit layout_below directives (and friends such as layout_above) could be useful in some circumstances. But is there no way of configuring the layout (e.g. the RelativeLayout) to simply assume that the each control in the series that it contains should automatically layout_below the preceding control?
LinearLayout might be more suitable for this kind of UI structure. It does exactly what you need, and it does it automatically for you. All that you really have to specify is its android:orientation which can be either vertical or horizontal.
More information on LinearLayout can be found here.
All children of a LinearLayout
are stacked one after the other, so a vertical list will only have one
child per row, no matter how wide they are, and a horizontal list will
only be one row high (the height of the tallest child, plus padding).
Here's a quick example:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey, I'm TextView A!"/>
<TextView
android:id="#+id/text_view_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey, I'm TextView B!"/>
<TextView
android:id="#+id/text_view_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey, I'm TextView C!"/>
<!-- ..and so on. -->
</LinearLayout>
What you are looking for is LinearLayout with a vertical orientation.

Why do child views inherit the alpha value from parent layout

In my app I've set a background image of the top level linearlayout and then to fade the background I set its alpha to .2 but this creates an odd problem in that it also sets the alpha for all children of the layout as well, even if I explicitly define a different alpha value in the children.
Is it possible to set the alpha value of a parent and not affect that of the child?
What is there a proper way to set the alpha on top level view without affecting the alpha on that views children?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#drawable/bg"
android:alpha="0.2">
<Button android:id="#+id/btn1"
android:text="Set 1"
android:layout_width="300px"
android:layout_height="150px"
android:layout_gravity="center"
android:background="#drawable/button1"
android:tag="1"
android:alpha="1"/>
<Button android:id="#+id/btn2"
android:text="Set 2"
android:layout_width="300px"
android:layout_height="150px"
android:layout_gravity="center"
android:background="#drawable/button2"
android:tag="2"/>
</LinearLayout>
That is exactly how it is intended to work.
Why not simply change the alpha of your background drawable "#drawable/bg" to 0.2?
Alternatively, try a FrameLayout with this basic structure:
<FrameLayout>
<ImageView
android:background="#drawable/bg"
android:alpha="0.2" />
<LinearLayout>
<Button />
<Button />
</LinearLayout>
</FrameLayout>
One way is to set the alpha value within the hash-code for color i.e. instead of #RRGGBB use #AARRGGBB. This makes sure that the alpha value isn't inherited.
You will need to use a framelayout. other wise your button is enlosed by a linearlayout with aplha set which will also affect the buttons.

Uneven LinearLayout weight distribution

I have a LinearLayout that has four views layed out horizontally. The first and last component are a set size. For the inner two views I want to just share the available space 50:50. I set each to a weight of "1" but when the views are layed out, the views are different sizes depending on the content they hold.
Here is my layout xml for reference.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/status"
android:src="#drawable/white"
android:paddingRight="10dip"
android:layout_height="35dip"
android:layout_width="35dip">
</ImageView>
<TextView android:id="#+id/name"
android:text="Name"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/status"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textSize="25dip">
</TextView>
<TextView android:id="#+id/description"
android:text="Description"
android:layout_toRightOf="#id/name"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textSize="25dip">
</TextView>
<TextView android:id="#+id/time"
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/description"
android:textSize="25dip">
</TextView>
</LinearLayout>
Obviously these aren't the actual column names but I changed them for privacy purposes. This layout is used by a ListView which changes the text of each view to be whatever value its presented. The name and description fields should line up since they're both given 50% of the remaining screen but when the name is longer the description is shifted right. Why?
For the weight to be considered, the layout dimension needs to be 0 (zero)
<TextView android:id="#+id/name"
android:text="Name"
android:layout_height="fill_parent"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="25dip">
</TextView>
I also recommend making your weight add up to either 1 (and use fractions) or 100.
So instead of 1 you would use either 50 or .5 for each view. The LinearLayout code will work properly with any weight sum, but it gets difficult if you want to modify your view later with additional sections.
Also, if you are not using relative layout, get rid of the toRightOf attributes. Less is more.
Try to use android:layout_width="fill_parent" instead of "wrap_content" in all children of LinearLayout. Or better yet, make such a structure in your xml:
<RelativeLayout>
<ImageView /> # status, fixed width, alignParentLeft="true"
<TextView /> # time, fixed width, alignParentRight="true"
<LinearLayout> # layout_width="fill_parent", toLeftOf="time" toRightOf="status"
<TextView /> # name, use layout_weight="1"
<TextView /> # description, use layout_weight="1"
</LinearLayout>
</RelativeLayout>
This should do what you want. Using LinearLayout instead of RelativeLayout might work too, but you have to experiment a bit (I believe using nested Layout, as in my example, will do the work).

Meaning of layoutopt tool output

I use layoutopt like 'layoutopt layout.xml'
And I get this message:
9:18 This tag and its children can be replaced by one <TextView/> and a compound drawable
28:78 Use an android:layout_height of 0dip instead of wrap_content for better performance
But I do not understand the meaning, can someone clarify me the meaning of it
use 0dip on what? on the layout? I want my layout to wrap the content not to be zero size height
<LinearLayout android:id="#+id/linearLayout3"
android:layout_width="fill_parent" android:layout_height="0px"
android:layout_alignParentTop="true" android:background="#30000000"
android:padding="5dip">
<ImageView android:id="#+id/imageView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/bzz_icon"></ImageView>
<TextView android:layout_width="fill_parent" android:id="#+id/textView1"
android:textColor="#FFFFFF" android:layout_height="wrap_content"
android:layout_gravity="center" android:gravity="left"
android:layout_marginLeft="15dip" android:text="#string/title"
android:textSize="20sp"></TextView>
</LinearLayout>
The first message is saying that you can replace your linearLayout3 with only a TextView and use android:drawableLeft instead of the ImageView.
The second message is probably telling you that on whatever is at line 28 of your layout you can use layout_height of 0dp instead of wrap_content. This is usually used in conjunction with layout_weight when your element will be expanded anyway. However, this is just a guess and you should post your entire layout XML, including line 28, so that we can better figure out the meaning of the message.
TextView with the left image and the text is centered
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:drawableLeft="#drawable/ic_launcher"
android:text="#string/hello" />

How to change position of button in android?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" android:background="#drawable/robo">
<TextView android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max Time(Sec)">
</TextView>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:id="#+id/maximum"
android:inputType="number">
</EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/startbtn"
android:focusable="true">
</Button>
<ImageButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"
android:id="#+id/imageButton1">
</ImageButton>
</LinearLayout>
In LinearLayout you can't change positions a whole lot - because the layout is Linear (sequential). But you can use layout_margin to somewhat move the widgets.
I don't know what you want to do, but you should look into FrameLayout (which will let you put the image anywhere!). My personal favorite is RelativeLayout.
Position depends on position of it's parent container/component (Layout) and on it's layout_* properties.
And u did'n tell what u want. If u want change it position - switch it with another view in Layout.
put more buttons in layout and hide or show them according to your needs
views in android are in relationship, not only in RelativeLayout but in Others, so you should choose the best way to describe your app layout, using more than one is commonly used.
to change position of label, you 'd better using AbsoluteLayout then using android:layout_x="", android:layout_y=""
what is your exact requirement???
you can put two buttons and make one visible and other one invisible or vice verse.or you can put layout_margin for all direction.And try to put all your component in different layout.

Categories

Resources