Android LinearLayout distribution of children - android

I am trying to create a sliding side-panel that evenly distributes children. The children can be dynamically added to the parent. For mdpi screens, each child is a 32x32 dp ImageButton. I want to achieve the following effect.
Case 1: Few Children.
When the layout has few children, I would like the image buttons to be evenly distributed.
Illustration of the panel with few children:
XML Layout:
<LinearLayout
android:id="#+id/toolbar"
style="#style/TabbedPanelToolbar"
android:layout_toRightOf="#id/pager"
android:orientation="vertical" >
<ImageButton
android:src="#drawable/ic_tab_info"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_tag"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_chat"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_meta"
style="#style/TabbedPanelToolbarControl" />
</LinearLayout>
Styles:
<style name="TabbedPanelToolbar" parent="android:style/Widget.ListView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#null</item>
</style>
<style name="TabbedPanelToolbarControl" parent="android:style/Widget.ImageButton">
<item name="android:background">#null</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:layout_weight">1</item>
</style>
Case 2: Many Children.
When there are a significant number of children, I would like the layout to condense to a certain point and then start to scroll (no longer distribute children but start overflowing the parent). For this, I had to add ScrollView. The problem is that the LinearLayout with few children stops distributing them evenly.
Illustration of the panel with many children:
XML Layout:
<ScrollView
android:id="#+id/toolbar_container"
style="#style/TabbedPanelToolbarContainer" >
<LinearLayout
android:id="#+id/toolbar"
style="#style/TabbedPanelToolbar"
android:layout_toRightOf="#id/pager"
android:orientation="vertical" >
<ImageButton
android:src="#drawable/ic_tab_info"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_tag"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_chat"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_meta"
style="#style/TabbedPanelToolbarControl" />
...
</LinearLayout>
</ScrollView>
Styles:
<style name="TabbedPanelToolbarContainer" parent="android:style/Widget.ScrollView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:scrollbars">none</item>
<item name="android:scrollbarSize">0dp</item>
<item name="android:paddingRight">0dp</item>
<item name="android:scrollbarAlwaysDrawVerticalTrack">false</item>
<item name="android:scrollbarStyle">insideOverlay</item>
<item name="android:background">#null</item>
<item name="android:divider">#null</item>
</style>
<style name="TabbedPanelToolbar" parent="android:style/Widget.ListView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">#null</item>
</style>
<style name="TabbedPanelToolbarControl" parent="android:style/Widget.ImageButton">
<item name="android:background">#null</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:layout_weight">1</item>
</style>
My question is whether it is possible to do the above but with one xml layout declaration - even distribution for few children and scroll for many. If not, is there a non-hacky way to do it with code (considering small and large screen sizes... children grow accordingly from 24dp to 96dp).

Omg, just shows how new I am to Android. So I started to play around with Graphical Layout tool in ADT and clicking random stuff (not really but more like experimenting). Turns out this is achievable by adding android:fillViewport="true" to ScrollView. The final xml layout is as follows:
<ScrollView
android:id="#+id/toolbar_container"
style="#style/TabbedPanelToolbarContainer"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/toolbar"
style="#style/TabbedPanelToolbar"
android:layout_toRightOf="#id/pager"
android:orientation="vertical" >
<ImageButton
android:src="#drawable/ic_tab_info"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_tag"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_chat"
style="#style/TabbedPanelToolbarControl" />
<ImageButton
android:src="#drawable/ic_tab_meta"
style="#style/TabbedPanelToolbarControl" />
...
</LinearLayout>
</ScrollView>
I'm glad I found it and sure others will find it useful too :)

Related

Content is not wrapping when layout_height=wrap_content in table row

I have created a simple table with varies length of text. I'm using layout_height=wrap_content in my table row. If you see the picture, it has a wide spaces between the next element and the previous one. If I set the height, then the spaces will remained when viewed in landscape. I don't know where to fixed it. I mostly used styles... so it's weird that some are working and some not.
sampleLayout.xml
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:background="#color/form_background"
android:shrinkColumns="*"
android:stretchColumns="*">
<!--Table header-->
<TableRow
style="#style/table_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/th_component"
style="#style/table_header"
android:layout_height="match_parent"
android:layout_weight="2.8"
android:text="#string/tableH_komponen"/>
<TextView
android:id="#+id/th_marks"
style="#style/table_header"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tableH_markah"/>
<TextView
android:id="#+id/th_demerits"
style="#style/table_header"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:text="#string/tableH_demerit"/>
<TextView
android:id="#+id/th_notes"
style="#style/table_header"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:text="#string/tableH_catatan"/>
</TableRow>
<!--Component C1-->
<TableRow
style="#style/table_row"
android:layout_height="wrap_content">
<TextView
style="#style/table_attr"
android:text="#string/c1"/>
<TextView
android:id="#+id/tv_cC1"
style="#style/table_component"
android:text="#string/cC1"
/>
<TextView
android:id="#+id/tv_mc1"
style="#style/table_marks"
android:layout_height="wrap_content"
android:text="#string/_6"/>
<CheckBox
android:id="#+id/checkBox_c1"
style="#style/table_demerit"
android:layout_height="wrap_content"/>
<ImageButton
android:id="#+id/btn_c1"
style="#style/table_notes"
android:layout_height="wrap_content"
android:text="#string/catatan"/>
</TableRow>
<!--Component C2-->
<TableRow
style="#style/table_row"
android:layout_height="wrap_content">
<TextView
style="#style/table_attr"
android:text="#string/c2"/>
<TextView
android:id="#+id/tv_cC2"
style="#style/table_headComponent"
android:text="#string/cC2"/>
<ImageButton
android:id="#+id/btn_c2"
style="#style/table_notes"
android:layout_height="wrap_content"
android:text="#string/catatan"/>
</TableRow>
<TableRow
style="#style/table_row"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_cC2_1"
style="#style/table_subComponent"
android:text="#string/cC2_1"/>
<TextView
android:id="#+id/tv_mC2_1"
style="#style/table_marks"
android:layout_height="match_parent"
android:text="#string/_1"/>
<CheckBox
android:id="#+id/checkBox_c2_1"
style="#style/table_subDemerit"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
style="#style/table_row"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_cC2_2"
style="#style/table_subComponent"
android:layout_height="wrap_content"
android:text="#string/cC2_2"/>
<TextView
android:id="#+id/tv_mC2_2"
style="#style/table_marks"
android:layout_height="wrap_content"
android:text="#string/_1"/>
<CheckBox
android:id="#+id/checkBox_c2_2"
style="#style/table_subDemerit"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</FrameLayout>
strings.xml
<resources>
<!--Case C-->
<string name="cC1">Pemeriksaan kesihatan ke atas semua pengendali makanan:
\n - Mendapat suntikan pelalian anti-tifoid
\n - Menghadiri Latihan Pengendali Makanan </string>
<string name="cC2">Tahap kebersihan diri yang baik:</string>
<string name="cC2_1">- Berpakaian bersih dan bersesuaian</string>
<string name="cC2_2">- Memakai apron yang bersih dan berpenutup kepala</string>
</resources>
styles.xml
<resources>
<!--kpkt table header-->
<style name="table_header">
<item name="android:textSize">#dimen/textSize_subheading</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/table_textcolor</item>
<item name="android:background">#color/table_background</item>
<item name="android:padding">#dimen/text_padding</item>
<item name="android:layout_width">0dp</item>
<item name="android:gravity">center</item>
</style>
<!--table style-->
<style name="table_row">
<item name="android:gravity">end</item>
<item name="android:layout_width">0dp</item>
<item name="android:weightSum">5</item>
</style>
<style name="table_attr">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">0.3</item>
<item name="android:gravity">center</item>
</style>
<style name="table_component">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">2.5</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
</style>
<style name="table_marks">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:padding">8dp</item>
</style>
<style name="table_demerit">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">0.7</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:visibility">visible</item>
</style>
<style name="table_notes">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">0.5</item>
<item name="srcCompat">#drawable/ic_edit</item>
<item name="android:contentDescription">catatan</item>
<!--<item name="android:drawableBottom">#drawable/ic_edit</item>-->
<!--<item name="android:drawableTop">#drawable/ic_edit</item>-->
</style>
<!--kpkt table head component-->
<style name="table_headComponent">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:padding">#dimen/text_padding</item>
<item name="android:layout_weight">4.2</item>
</style>
<!--kpkt table subcomponent-->
<style name="table_subDemerit">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1.2</item>
<item name="android:visibility">visible</item>
</style>
<style name="table_subComponent">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:padding">8dp</item>
<item name="android:layout_weight">2.5</item>
</style>
</resources>
I can suggest you to understand the concept of wrap_content and match_parent, to get better solution.
I think you can use linear layout with horizontal and vertical orientation.
Because linear layout is more flexible and gives you better performance than table layout and table row.
Q: When we should use table layout and table row?
Ans: when we have a similar type of data, and we want to represent in rows and columns view then we should use table layout and table row.
But if we have non linear data, then we should not use table layout and table row...and I think you have non linear data.
For better understanding of showing data in non linear form, you should know the weight_sum and layout_weight property of linear layouts.

Align all columns in list view

I was wondering how I can manage to align all columns in rows. I am using a ListView, where I display a list of drugs for my game. The columns are different sizes each row. I would like to see that each column has the same width all down to the end of the list, but not have same width overall.
As you can see from the picture, the columns are not aligned. I draw the lines to make it even more obvious what I would like. You can also see that the buttons are to close to the other column. With the arrow on the picture I wanted to make it clear that those buttons should be placed more to the end of the row.
My implementation uses the following styles:
<!-- List style for the drug sell list -->
<style name="traderY.List">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentLeft">true</item>
<item name="android:layout_alignParentTop">true</item>
</style>
<!-- Header style for quantity column -->
<style name="traderY.HeaderQuantityText">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.15</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:padding">4dip</item>
<item name="android:layout_margin">4dip</item>
<item name="android:textColor">#EDEFF0</item>
</style>
<!-- Header style for cost column -->
<style name="traderY.HeaderCostText">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.20</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:padding">4dip</item>
<item name="android:layout_margin">4dip</item>
<item name="android:textColor">#EDEFF0</item>
</style>
<!-- Header style for name column -->
<style name="traderY.HeaderNameText">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.45</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:padding">4dip</item>
<item name="android:layout_margin">4dip</item>
<item name="android:textColor">#EDEFF0</item>
</style>
<!-- Row style for quantity column -->
<style name="traderY.QuantityListText">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.15</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:padding">4dip</item>
<item name="android:layout_margin">4dip</item>
<item name="android:textColor">#000000</item>
</style>
<!-- Row style for cost column -->
<style name="traderY.CostListText">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.20</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:padding">4dip</item>
<item name="android:layout_margin">4dip</item>
<item name="android:textColor">#000000</item>
</style>
<!-- Row style for name column -->
<style name="traderY.NameListText">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.45</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:padding">4dip</item>
<item name="android:layout_margin">4dip</item>
<item name="android:textColor">#000000</item>
</style>
<!-- Row style for button column -->
<style name="traderY.ListButton" parent="#android:style/Widget.Button">
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.20</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:gravity">center</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">0.6</item>
<item name="android:background">#drawable/custom_btn_black_pearl</item>
<item name="android:padding">4dip</item>
</style>
As you can see I set or I try to set the column width by using those two items:
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.20</item>
I was reading that you can set the width by using weight. I basically used the same values I would use to set percentages. If I understand correctly, the weight value represents the ratio between other components. I found this as a solution on a Stack Overflow question and I found it in several tutorials. One of the tutorials I found is this one here.
To share more light, here are also the layouts of the of all the components, starting with the fragment which contains the list view.
Fragment layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="#+id/money"
style="#style/traderY.TextCenter" />
<TextView
android:id="#+id/location"
style="#style/traderY.TextCenter" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/junkie_icon"/>
<ListView
android:id="#+id/drug_list"
style="#style/traderY.List"
android:layout_weight="1"/>
<TableRow
style="#style/traderY.ButtonRow">
<Button
android:id="#+id/nothing"
style="#style/traderY.ButtonCenter"
android:text="#string/exit"/>
</TableRow>
</LinearLayout>
Here is the header layout. As you can see I omit the last column for buttons, because there is no need to display a header for it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000">
<TextView
android:text="#string/header_quantity"
style="#style/traderY.HeaderQuantityText" />
<TextView
android:text="#string/header_cost"
style="#style/traderY.HeaderCostText" />
<TextView
android:text="#string/header_name"
style="#style/traderY.HeaderNameText" />
</LinearLayout>
And here is the layout for each row I use:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/drug_quantity"
style="#style/traderY.QuantityListText" />
<TextView
android:id="#+id/drug_cost"
style="#style/traderY.CostListText" />
<TextView
android:id="#+id/drug_name"
style="#style/traderY.NameListText" />
<Button
android:id="#+id/drug_sell_btn"
android:text="#string/sell_drugs_action"
style="#style/traderY.ListButton"/>
</LinearLayout>
Those are the style/layouts I use and I do not change any of those properties in the code. So the problem can only be in my XML files.
Does anyone know what am I doing wrong here? Maybe my investigation is wrong and I can not do it. I would appreciate any help or comments to solve this problem. If this is not even possible, then please let me know.
I found out that my solution was correct. The weight property did the job. For some unknown reason gradle did not see the changes when it was building the app. Or maybe it is the emulator. This was not the first time something like this happened. I will have to investigate why this even occur.
Increase weight of style="#style/traderY.QuantityListText". Make 0.2 or 0.3
Wrap your text views in a LinearLayout and set it to fill the parentView. In this way all the buttons will be aligned to the right.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/drug_quantity"
style="#style/traderY.QuantityListText" />
<TextView
android:id="#+id/drug_cost"
style="#style/traderY.CostListText" />
<TextView
android:id="#+id/drug_name"
style="#style/traderY.NameListText" />
</LinearLayout>
<Button
android:id="#+id/drug_sell_btn"
android:text="#string/sell_drugs_action"
style="#style/traderY.ListButton"/>
</LinearLayout>

TextView repositions when text is entered in EditText [XML]

I have a simple table layout that looks like this:
http://imgur.com/beU9LbR
My problem is that whenever I type text into the Artist, Venue or Comments EditText boxes, once the text string is in line vertically with the start of the Date TextView, the TextView start position moves position to stay in line with the string of characters; like this:
http://imgur.com/7f7eal8
I'm not sure if the Button is trying to match its end point to the string of characters or if the TextView is trying to match its start point to the string of characters but either way somethings not right.
Can anyone give me some insight as to what is happening here?
Thanks in advance!
Here is the XML file for the page shown:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
style="#style/titleBar"
android:id="#+id/addTitle"
android:text="#string/add" />
<TableLayout
style="#style/my_table"
android:id="#+id/addTable"
android:layout_below="#+id/addTitle">
<TableRow>
<TextView
style="#style/largeText"
android:text="#string/artistName"/>
</TableRow>
<TableRow>
<EditText
style="#style/tableEditText"
android:id="#+id/editName"/>
</TableRow>
<TableRow>
<TextView
style="#style/largeText"
android:text="#string/venue"/>
</TableRow>
<TableRow>
<EditText
style="#style/tableEditText"
android:id="#+id/editVenue"/>
</TableRow>
<TableRow>
<TextView
style="#style/largeText"
android:text="#string/date"/>
</TableRow>
<TableRow>
<Button
android:background="#android:color/transparent"
android:drawableRight="#drawable/event_1"
android:id="#+id/setDateButton"/>
<TextView
style="#style/tableEditText"
android:layout_weight="3"
android:layout_width="200dp"
android:id="#+id/editDate"/>
</TableRow>
<TableRow>
<TextView
style="#style/largeText"
android:text="#string/comments"/>
</TableRow>
<TableRow>
<EditText
style="#style/tableEditText"
android:inputType="textNoSuggestions|textCapSentences|textMultiLine"
android:lines="3"
android:id="#+id/editComments"/>
</TableRow>
</TableLayout>
<Button
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/add"
android:background="#android:color/transparent"
android:textColor="#color/grey"
android:padding="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Here is the styles.xml file:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="largeText">
<item name="android:textColor">#color/grey</item>
<item name="android:textSize">25sp</item>
<item name="android:padding">3dp</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">left</item>
<item name="android:layout_marginRight">20sp</item>
</style>
<style name="largeText2">
<item name="android:textSize">25sp</item>
<item name="android:padding">3dp</item>
<item name="android:background">#color/bgOrange</item>
<item name="android:textColor">#color/nearBlack</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">left</item>
<item name="android:layout_marginRight">20sp</item>
</style>
<style name="titleBar">
<item name="android:textColor">#fff</item>
<item name="android:textSize">25sp</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentTop">true</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:gravity">left</item>
<item name="android:padding">20sp</item>
<item name="android:background">#color/headingOrange</item>
</style>
<style name="my_table">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">15sp</item>
</style>
<style name="tableEditText">
<item name="android:inputType">textCapSentences|textNoSuggestions|textAutoComplete</item>
<item name="android:background">#color/bgOrange</item>
<item name="android:textColor">#color/nearBlack</item>
<item name="android:gravity">left</item>
<item name="android:padding">3dip</item>
<item name="android:textSize">25sp</item>
<item name="android:layout_weight">1</item>
</style>
<style name="smallIcon">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_gravity">left</item>
<item name="android:padding">2dp</item>
</style>
</resources>
This is happening because of your use of the TableLayout while not fully appreciating how it works. What it does is it goes through and figures out how many columns it will have. For each TableRow it will put the views in each column starting from the first defined view to the last.
What is happening is that your first TextView for artistName is in column 1. The next few TableRow only have 1 column. Then you get to the TableRow with setDateButton which creates two columns. Now all the other views previously assigned will be in column 1 with a column 2 after them which would be as wide as editDate if they had another view.
As a result setDateButton is now as wide as your artistName TextView and since you are using android:drawableRight the image is being placed to the right side of the Button.
You're going to need to adjust layout_column and layout_span values to adjust the layout. Alternatively, I would suggest removing the TableLayout and using the RelativeLayout appropriately.
Try wrapping the Button and TextView with a LinearLayout

Issues in abstracting ImageView style into themes.xml

Here's my original layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:src="#drawable/ic_action_refresh"/>
</RelativeLayout>
And I have extracted all but one of the ImageView layout into themes.xml:
<style name="MenuButton" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerVertical">true</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:paddingRight">10dp</item>
</style>
I then replaced the attributes of the ImageView above to be:
<ImageView
style="#style/MenuButton"
android:src="#drawable/ic_action_refresh"/>
My problems are:
The design preview in Idea complained it couldn't resolve the MenuButton style.
The app deployed and ran just fine on my device, but the android:layout_centerVertical attribute was not applied. The item floated to the top of the container.
I'm new to Android so I'm likely to have missed something fundamental. Any ideas please?
Dont use these attributes in style
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
It may create problems.

Layout weight is not fit, it change every input

I have a spinner, an edittext and a button in a horizontal linearlayout. When I have enter something into edittext, it increase every character and others decrease. There is an eanough blank area input but it does not change, every time it increase how long character I input.
How can I fix it?
layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/ly"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="10" >
<Spinner
android:id="#+id/spinner1"
style="#style/submitspinner"
android:layout_weight="2"
android:entries="#array/a_code"
android:prompt="#string/p_code" />
<EditText
android:id="#+id/editText1"
style="#style/submitstyle"
android:layout_weight="5"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
style="#style/submitstyle"
android:layout_weight="3"
android:text="#string/btn_submit" />
</LinearLayout>
style
<style name="submitstyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_margin">10sp</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
<item name="android:textSize">#dimen/pt</item>
</style>
<style name="submitspinner" parent="#android:TextAppearance.Widget.TextView.SpinnerItem">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_margin">10sp</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
<item name="android:textSize">#dimen/pt</item>
</style>
I have changed style as below and it worked well...
submitstyle and submitspinner style
from
<item name="android:layout_width">wrap_content</item>
to
<item name="android:layout_width">0dp</item>
I found good tuts for this but I have closed the tab, I cannot share it...
//change you style from
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
//to
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>

Categories

Resources