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>
Related
I have a TextView and this is the xml code for it
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="#+id/sidelist_item_count"
style="#style/HomeTextViewItemCount"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
And this is my Style
<style name="HomeTextViewItemCount">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/home_textview_count</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">10sp</item>
<item name="android:gravity">center</item>
<item name="android:layout_marginRight">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:textColor">#android:color/white</item>
</style>
I want to make my TextView circular. This doesn't expand when there are 3 digit numbers entered. But if I remove the style attribute from the TextView and set android:background="#drawable/home_textview_count" in my text view and added rest of the attributes inside Style into my Text view. then it works as expected. Can I know why cannot we set the style and what's the mistake I have done?
Please help me.
Thank you!
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.
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>
I have check box in my contacts list to select that. But check box is in white color,it is not visible...So i need to change that border to black color...Please help me to change the code
This is my layout code
<LinearLayout 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:divider="?android:dividerVertical"
android:showDividers="middle"
android:dividerPadding="5dp"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/image"
android:layout_width="#dimen/contact_image"
android:layout_height="50dp"
android:src="#drawable/contacts_image" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="1dp"
android:textColor="#color/black"
android:textSize="18sp"
/>
<TextView
android:id="#+id/mobile_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="15sp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
/>
</LinearLayout>
<CheckBox
android:id="#+id/check_box"
android:layout_width="#dimen/check_box_width"
android:layout_height="50dp"
android:visibility="visible"
android:clickable="true"/>
</LinearLayout>
This is my style.xml code
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/scandal</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<style name="MyActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
<style name="ActionBarCompat">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">50dp</item>
<item name="android:orientation">horizontal</item>
</style>
<style name="DashboardButton">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:drawablePadding">2dp</item>
<item name="android:textSize">16dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ff29549f</item>
<item name="android:background">#null</item>
</style>
<style name="FooterBar">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">#dedede</item>
</style>
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item>
</style>
</resources>
instead changing the default in style xml file. do this.
create this your_custom.xml file and and save it in drawable folder
your_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/unselected" android:state_checked="false"><color android:textColor="#your_color" />
</item>
<item android:drawable="#drawable/selected" android:state_checked="true"><color android:textColor="#your_color" />
</item>
<item android:drawable="#drawable/uncheck"></item>
<!-- default -->
</selector>
you will have to create three images for checkbox display
1.selected
2.unselect
3.uncheck
after all this done set attribute to your checkbox in layout
android:button="#drawable/your_custom"
okey guys, i've a button like the pict below
i want to change the distance between the text with the drawable . how to trick this one ?
this is my xml code for a button
<Button
android:id="#+id/ButtonCancel"
style="#style/CAGButton"
android:drawableLeft="#drawable/selector_ic_cancel"
android:text="#string/layer_button_cancel" />
and this is the CAGButton in style.xml
<style name="CAGButton" parent="#android:style/Widget.Button">
<item name="android:drawablePadding">3dip</item>
<item name="android:textColor">#color/cag_brown</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:background">#drawable/selector_button"</item>
</style>
okey, thanks in adv. :)
Btw i solved my problem with made some changes in my button style. so it become like this :
<style name="CAGButton" parent="#android:style/Widget.Button">
<item name="android:gravity">center</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:textColor">#color/cag_brown</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:background">#drawable/selector_button</item>
</style>
I think we just need to set the padding left &/ right to the best point that fit our design.
Here is the solution.
<Button
android:id="#+id/xyz"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:background="#drawable/button_shape"
android:drawableLeft="#drawable/location"
android:drawablePadding="2dip"
android:gravity="center"
android:paddingLeft="20dip"
android:paddingRight="16dip"
android:singleLine="true"
android:text="XYZ"
android:textColor="#FFF" />