Following question has kept me puzzled for a while and I thought maybe asking about this does no harm. I have the following layout.xml and style.xml files;
res/layout/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
style="#style/headerContainer" />
<LinearLayout
style="#style/footerContainer" />
<ScrollView
style="#style/contentContainer" />
</RelativeLayout>
res/values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="container">
<item name="android:layout_width">fill_parent</item>
</style>
<style name="headerContainer" parent="container">
<item name="android:layout_height">40dp</item>
<item name="android:layout_alignParentTop">true</item>
<item name="android:background">#80FF0000</item>
<item name="android:id">#+id/header</item>
</style>
<style name="footerContainer" parent="container">
<item name="android:layout_height">50dp</item>
<item name="android:layout_alignParentBottom">true</item>
<item name="android:background">#8000FF00</item>
<item name="android:id">#+id/footer</item>
</style>
<style name="contentContainer" parent="container">
<item name="android:layout_height">60dp</item>
<item name="android:layout_below">#id/header</item>
<item name="android:layout_above">#id/footer</item>
<item name="android:background">#800000FF</item>
</style>
</resources>
Now, the question is, is there a danger of overlapping IDs as I'm introducing them in style.xml? Funny thing is that this approach works, on the emulator I'm using at least, but the created IDs are not being added to the R class. And I'm a bit confused how they are defined once my layout is inflated.
Don't use #+id/... in styles.
#+id/... can only be used in layouts.
Otherwise you can get Error executing apt: return code 139 during build.
Use #id/... and generate ids with help resource file if needed:
res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="header" />
<item type="id" name="footer" />
</resources>
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Its not safe my friend. You should use different id for different files. Here the emulator will not act problem. It will understand because for every xml file there is unique code defined R.java file automatically. So emulator will understand from there very easily. But if you need to improve or edit the code for any up gradation surely you will get confused which id is belonging to which xml file's layout or wedged. So provide unique id to every widget of layout. It will be helpful if you provide the id including some tag of respective file name.
Example: If file name is filldetails.xml then you can use id=#+fd_name
It will be helpful to know the flow of application.
I am doing it this way and have had good luck:
Layout
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include layout="#layout/action_bar"/>
</RelativeLayout>
Common:
res/layout/action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_container"
android:layout_width="fill_parent"
android:layout_height="#dimen/action_bar_height"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="20dip"
android:textStyle="bold"
/>
</RelativeLayout>
Related
Is there any way in Android XML to set the same style (or style paramerers, like padding) for all rows in a TableLayout at once?
What I would like to avoid doing is:
<TableLayout>
<TableRow style="#style/MyStyle">...</TableRow>
<TableRow style="#style/MyStyle">...</TableRow>
<TableRow style="#style/MyStyle">...</TableRow>
<TableRow style="#style/MyStyle">...</TableRow>
...
</TableLayout>
You can define your own styles by creating XML files on the res/values directory. So, let's suppose you want to have red and bold text, then you create a file with this content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyRedTheme" parent="android:Theme.Light">
<item name="android:textAppearance">#style/MyRedTextAppearance</item>
</style>
<style name="MyRedTextAppearance" parent="#android:style/TextAppearance">
<item name="android:textColor">#F00</item>
<item name="android:textStyle">bold</item>
</style>
</resources>
You can name it like you want, for instance res/values/red.xml. Then, the only thing you have to do is using that view in the widgets that you want, for instance:
<?xml version="1.0" encoding="utf-8"?>
<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/MyRedTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is red, isn't it?"
/>
</LinearLayout>
For more referense click here
In my project I use special fonts for my buttons. So I've added the PixlUI library so I can set the font in xml.
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_login"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_broker_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile_broker"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
These are my buttons, since they all have the same typeface I want to include the typeface in the 'custom_button_style'
This my custom style:
<style name="custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
</style>
How do I include
pixlui:typeface
in this style?
It´s not clear if we can´t see the complete xml layout file, but things that often happens is that the poeple forget to add the xmlns attribute to the parent. You parent layout should look something like this. It´s part of this library and the important thing is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" <----thats the important attribute
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
You don´t need to add something in Your style.xml. So if this does not fix Your problem, come back.
EDIT
It is not tested yet, but for the style it could be:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui">
<style "custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
<item name="pixlui:typeface">AvenirNextCondensed-Regular.ttf</item>
</style>
before someone marked this question as duplicate, I must say I already know that this question is already asked several times, but every time I tried I always failed.
so I have a listview of date on the left and I want to show data on the right, I want to make a listview that can show which item is selected so I know which date that I picked.
So far I've tried this;
on listview I've added android:choiceMode="singleChoice";
<ListView
android:id="#+id/listTglRMPasien"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_above="#+id/btnObatAlkes"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:choiceMode="singleChoice">
</ListView>
on item of listview I've added android:background="#drawable/list_selector";
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtListRekamMedikTanggal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/list_selector"/>
<TextView
android:id="#+id/txtListRekamMedikTabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/emptyString"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="gone" />
</LinearLayout>
and on list_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_launcher" android:state_activated="false"/>
<item android:drawable="#drawable/ic_launcher" android:state_pressed="false"/>
<item android:drawable="#drawable/blue" android:state_pressed="true"/>
<item android:drawable="#drawable/blue" android:state_activated="true"/>
</selector>
note that ic_launcher and blue here are png file, it's supposed to be "#drawable/listitem_pressed" and "#drawable/solid_white", I find this answer from here
but somehow it got an error, it says No resource found that matches the given name and I think something is missing so I changed it to png file
anyway, when I tried to run this, it always show ic_launcher whether I select it or not, it never show blue.
apparently it happens because my API level is too low, I changed it from API level 8 to API level 14.
this is a good answer about how to change min. API level for your Android app in Eclipse
Here is what I did on my ListView. Create your own theme on res/values folder, you can name it for example mytheme.xml. Here what I added on mytheme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme" parent="android:Theme.Holo.Light">
<item name="android:textColor">#fff</item>
</style>
</resources>
Then in your Manifest add this android:theme="#style/mytheme" inside the activity of your ListView Class.
Hope it helps.
I want to create a layout with a repeated background image. I searched and fount this tutorial
but seems not working for me!
the following shows activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backrepeat"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
and this is the backrepeat.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="#drawable/bg"
android:tileMode="repeat" />
also put backrepeat.xml in drawable folder.
Should I add or remove some codes?
thanks
Ok, here's what I've got in my app. It includes a hack to prevent ListViews from going black while scrolling.
drawable/app_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/actual_pattern_image"
android:tileMode="repeat" />
values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="app_theme" parent="android:Theme">
<item name="android:windowBackground">#drawable/app_background</item>
<item name="android:listViewStyle">#style/TransparentListView</item>
<item name="android:expandableListViewStyle">#style/TransparentExpandableListView</item>
</style>
<style name="TransparentListView" parent="#android:style/Widget.ListView">
<item name="android:cacheColorHint">#android:color/transparent</item>
</style>
<style name="TransparentExpandableListView" parent="#android:style/Widget.ExpandableListView">
<item name="android:cacheColorHint">#android:color/transparent</item>
</style>
</resources>
AndroidManifest.xml:
<application android:theme="#style/app_theme">
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to set a row for grid-view in android?
Now i am displaying the data from XML file in to grid view in android.This is the exact xml's file link
"http://54.251.60.177/StudentWebService/StudentDetail.asmx/GetTMSOrders"
which i am trying to show.
I have done that concept successfully, but here the problem is,i am not getting the answer like the below image
i need to show like the below image, in android
but i am getting only like the below image.....
![enter image description here][3]
How to overcome this concept?can any one please make me clear?
thanks for your precious time!..
Try the below layout in your code has grid_layout.xml, it may help you a while.Better to use Listview for these kind of implementation.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
style="#style/gridViewStyle" />
under styles.xml place the below code
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- GridView Style -->
<style name="gridViewStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:columnWidth">60dp</item>
<item name="android:gravity">center</item>
<item name="android:horizontalSpacing">10dp</item>
<item name="android:numColumns">3</item>
<item name="android:padding">10dp</item>
<item name="android:stretchMode">columnWidth</item>
<item name="android:verticalSpacing">10dp</item>
</style>
</resources>
place your relevant Listitems in your grid_items.xml has below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/icon_image"
style="#style/wrapViewScale"/ >
<TextView
android:id="#+id/icon_text"
style="#style/wrapViewScale"
android:gravity="center_horizontal"
android:textColorHighlight="#color/text_color_highlight" />
</LinearLayout>
stick with gridview , and change the childview by inflating custom xml file