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>
Related
I want to create a style 100% equal to this button:
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:paddingStart="30dp"
android:paddingEnd="30dp"
android:backgroundTint="#color/red"
android:text="In XML"
android:textStyle="bold"
app:cornerRadius="25dp"
app:strokeColor="#color/black"
app:strokeWidth="2dp"/>
This is the style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="btn_xml" value="btn_xml" />
<style name="ProgrammaticallyStyle">
<item name="android:id">#id/btn_xml</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">5dp</item>
<item name="android:backgroundTint">#color/red</item>
<item name="android:paddingStart">30dp</item>
<item name="android:paddingEnd">30dp</item>
<item name="android:text">In XML</item>
<item name="android:textStyle">bold</item>
<item name="app:cornerRadius">25dp</item>
<item name="app:strokeColor">#color/black</item>
<item name="app:strokeWidth">2dp</item>
</style>
</resources>
But in style, the last 3 attributes start with app:xxx and cannot be assigned. I assumed that is because in xml I have the following sentence in button parent view:
xmlns:app="http://schemas.android.com/apk/res-auto"
Using sttributes without app: doesn't work, do I maybe need a specific parent for the style?
How can I assign these app:xxx attributes in style?
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
I have a problem while working with List view and cardslib library.
I created a card list with the same structure of list_card_thumbnail_layout
and each card contains a photo and text
I used for card layout card_thumbnail_layout
But the source of all thumbnails is an already loaded bitmap
What I want is make the thumbnail match the width of the card and scale itself to fit inside the thumbnail
What I did is:
For card List:
<it.gmariotti.cardslib.library.view.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_cardId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/list_card.thumbnail"
card:card_layout_resourceID="#layout/card_thumbnail_layout"
/>
For indivisual Card:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<it.gmariotti.cardslib.library.view.component.CardThumbnailView
style="#style/card_thumbnail_outer_layout"
android:id="#+id/card_thumbnail_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- Main Content View -->
<FrameLayout
android:id="#+id/card_main_content_layout"
style="#style/card.content_outer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
and in styles:
<style name="card_thumbnail_image">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
<item name="android:scaleType">centerCrop</item>
</style>
<style name="card_thumbnail_outer_layout">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
I don't know where the problem is. :(
I think the attribute 'adjustViewBounds' is the answer in this case
give this a try:
<style name="card_thumbnail_image">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
<item name="android:scaleType">centerInside</item>
<item name="android:adjustViewBounds">true</item>
</style>
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 :)
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>