image height doesn't wraping content inside listview - android

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>

Related

Is there any possible way to style Relative Layouts using styles.xml in Android Studio?

Instead of setting the layout_toRightof, layout_toLeftof, layout_above and layout_below parameters everytime a view is created, is there a way to include it in the styles.xml file such that it becomes easier to implement instead of typing it every single time? This is the intended layout.
I've included my activityMain.xml and styles.xml files below.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:padding="24dp"
tools:context="com.example.android.musiclibrary.MainActivity"
android:background="#fcfcfc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
style="#style/song_group">
<ImageView
android:src="#drawable/n30_Seconds_to_Mars"
android:id="#+id/thumbnail1"
style="#style/icon"
/>
<TextView
android:layout_toRightOf="#id/thumbnail1"
android:text="Up In The Air"
android:id="#+id/song1"
style="#style/song"
/>
<TextView
android:layout_toRightOf="#id/thumbnail1"
android:text="30 seconds to Mars"
style="#style/artist"
/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Here's my styles.xml file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="icon">
<item name="android:scaleType">fitCenter</item>
<item name="android:maxWidth">42dp</item>
<item name="android:maxHeight">42dp</item>
<item name = "android:adjustViewBounds">true</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="song">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">6dp</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">17sp</item>
</style>
<style name="artist">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:layout_marginLeft">6dp</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#000000</item>
<item name="android:textStyle">italic</item>
</style>
<style name="song_group">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">0dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_weight">1</item>
<item name="android:padding">10dp</item>
</style>
</resources>

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>

How I set the width of RelativeLayout to screen width?

I have this RelativeLayout component and I want to set it to the entire screen width. How can I do that?
atendente.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
style="#style/layoutAtendenteBalao"
android:id="#+atendente/rltAtendenteBalao">
styles.xml
<style name="layoutAtendenteBalao">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">210px</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:layout_marginRight">176px</item>
<item name="android:layout_marginBottom">140px</item>
<item name="android:background">#drawable/borda_balao</item>
<item name="android:paddingLeft">15dp</item>
<item name="android:paddingRight">15dp</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
I have tried to put fill_parent to try to fill the entire width. But it does not worked. What can I do?
You should set it when you including this layout to other, like this:
<include android:id=”#+id/your_id”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”#layout/your_layout”/>
For this to work, you have to add android:layout_width , and android:layout_height in your layout node like:
<RelativeLayout
style="#style/layoutAtendenteBalao"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+atendente/rltAtendenteBalao">
use -1 and -2 for fill_parrent and wrap_content
Reference: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT
<style name="layoutAtendenteBalao">
<item name="android:layout_width">-1</item>
<item name="android:layout_height">210px</item>
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:layout_marginRight">176px</item>
<item name="android:layout_marginBottom">140px</item>
<item name="android:background">#drawable/borda_balao</item>
<item name="android:paddingLeft">15dp</item>
<item name="android:paddingRight">15dp</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
</style>

applying style to textview drawable

I have a layout for a TextView and drawable as such...
<LinearLayout style="#style/dashboard_content_horizontal" >
<ImageView
style="#style/dashboard_imageview"
android:src="#drawable/menu6" />
<TextView
style="#style/dashboard_textview"
android:text="#string/menu6_text />
</LinearLayout>
as you can see the image uses a style attribute which looks like this
<style name="dashboard_imageview">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:scaleType">fitCenter</item>
</style>
the linear layout's style surrounding it looks like this
<style name="dashboard_content_horizontal">
<item name="android:layout_width">0px</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">3</item>
<item name="android:orientation">vertical</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
</style>
and TextView's style
<style name="dashboard_textview">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/black</item>
</style>
this all works and looks exactly how I want it to look. however, because of this eclipse warning
"This tag and its children can be replaced by one
and a compound drawable" I decide to try to do this with just a TextView and drawable
like such..
<TextView
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Menu6"
android:drawableBottom="#drawable/my_drawable"
android:padding="5dp" />
this renders the image too small.. I don't see a way to apply a style to the image to get it like I want it.. anyone know the answer? is even possible?

problem changing style in a Spinner

i need to implement an application in android using styles. I know that, difining an style for each object in styles.xml, it's possible to stablish diferent styles for: Textview, Buttons, editviews..but in the special case of spinner it doesn't work. this is my example:
this is the layout main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
style="#style/TextviewVerde"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/escribenombre"
/>
<EditText
style="#style/EditviewAzul"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TxtNombre"
/>
<Spinner
style="#style/SpinnerRojo"
android:textColor="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TxtSpinner"
android:prompt="#string/seleccionetexto"
/>
<Button
style="#style/BotonBlanco"
android:id="#+id/BtnHola"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pulse aqui"
/>
</LinearLayout>
and this is the styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TextviewVerde" >
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">5pt</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TextviewRosa" >
<item name="android:textColor">#5F04B4</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">15pt</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="EditviewAzul" >
<item name="android:textColor">#0000FF</item>
<item name="android:textSize">8pt</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:shadowColor">#0000FF</item>
</style>
<style name="SpinnerRojo" >
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#DF0101</item>
<item name="android:gravity">center</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">11pt</item>
<item name="android:drawSelectorOnTop">true</item>
</style>
<style name="BotonBlanco">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">12pt</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:clickable">true</item>
</style>
</resources>
every object works well, but the spinner doesn't change its appearance. My boss told me that i have to implement styles without changing the *.java file, what can i do?? i need to change the colour of the spinner options, the backgound, add some pics.. etc. thanks a lot for the help
http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/
To make custom styled dropdown when the spinner is clicked, you should create a custom Adapter with a reference to your layout xml defining the rows of the list.
For example:
String[] elements = {"optionA", "optionB"};
Spinner s = (Spinner) findViewById(R.id.your_spinner_id);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.id.textViewId, elements);
adapter.setDropDownViewResource(R.layout.your_dropdown_layout);
Where textViewId should be a child view in your_dropdown_layout.
Hope that helped.
Ripityom

Categories

Resources