Android set different styles to Views in code / dynamically - android

I want to dynamically add a custom styles to textviews in code. I can use the following workaround to do this when there is only textview in question:
android set style in code
However, if I try this method and create a template.xml file with 2 textview, each with a different style applied, this no longer works.
i.e. my template file will look something like this instead:
<?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"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="#style/my_style" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is another template"
style="#style/my_style2" />
</LinearLayout>
How do I extend this solution so it works with more than one textview and more than one style?

Related

Android - Avoid layout duplication

I have a huge layout file called visit_registration.xml. In order to support different screen sizes, I've duplicated the file and created the files layout-sw600dp/visit_registration.xml and layout-sw720dp/visit_registration.xml.
There is a single small difference between the layouts:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/app_margin"
android:paddingRight="#dimen/app_margin">
<TextView
android:id="#+id/visitRegistrationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/visit_registration" />
<!-- TONS of UI elements here! -->
</LinearLayout>
The original layout file (above) shows the TextView with the id "visitRegistrationTextView".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/app_margin"
android:paddingRight="#dimen/app_margin">
<TextView
android:id="#+id/visitRegistrationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="#string/visit_registration" />
<!-- TONS of UI elements here! -->
</LinearLayout>
Each copy uses a different visibility. The example above uses "gone". Sometimes the changes are different, but small, like a UI element moved to another place in the layout. The rest of the code remains the same.
The main question is about layout duplication. I don't want to maintain three different layouts because of the classical duplication problems like "What would happen if I need to change a common UI element present in three files and forget to change it in a single place?"
Is it possible to have a single "base layout" with the common UI elements and include the small layout changes on it accordingly with the current screen size ?
Try to make resources file eg. values-sw600dp/strings.xml, values-sw720dp/strings.xml with
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="visibility">gone</string>
</resources>
or
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="visibility">visible</string>
</resources>
and your layout file (only one):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/app_margin"
android:paddingRight="#dimen/app_margin">
<TextView
android:id="#+id/visitRegistrationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="#string/visibility"
android:text="#string/visit_registration" />
<!-- TONS of UI elements here! -->
</LinearLayout>
You can define a Layout with a single text view, one for each of your screen size. Then you create a general layout in which you can include the previous.
Let's call it text_layout, one for each screen size.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/visitRegistrationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="#string/visit_registration" />
</LinearLayout>
Then you create the main layout, one for all screen size.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/app_margin"
android:paddingRight="#dimen/app_margin">
<include layout="#layout/text_layout"/>
<!-- TONS of UI elements here! -->
</LinearLayout>
That's it, less duplication, more maintenance.

Text on right of CheckBox in Android

I'm trying to imitate the CheckBoxs like in the Settings app on my phone.
It looks like that:
I've tried using separate TextViews, but that way only the checkmark is clickable, rather than the text and the checkmark.
Is there a way to accomplish that?
I also tried a CheckedTextView but couldn't find the right drawable to use.
As far as I know this should work
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="#null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>
With android:button="#null" you remove standard checkbox button / image, and after you just add checkbox image as right drawable (or use drawableEnd to support RTL)
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
Is there a way to accomplish that?
I also tried a CheckedTextView but couldn't find the right drawable to use.
CheckedTextView will solve your issue perfectly:
Please look for example on simple_list_item_checked.xml (it is android embedded layout)
CheckedTextView have next attribute:
<!-- Drawable used for the check mark graphic. -->
<attr name="checkMark" format="reference"/>
So you only need to set checkMark to proper selector (If you do not have resource you could use btn_check.xml. It is inside android):
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#drawable/your_selector_with_proper_checked_states"
/>
try the following Xml
<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:orientation="horizontal"
android:weightSum="10"
tools:context="com.example.pager.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Anything you want here" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="8" />
</LinearLayout>
It works fine

Modularising Layout/Views in Android

In my code i'm using and XML file for showing a view in Android. The file is as follows :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:text="Search Box"
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I'm a part of a multi member team, and the situation is that the code for the EditText and ListView will be written by 2 different developers in 2 different XML files. Then we will use import statements to import the code from these 2 XML files into the main XML file.
The problem is that we would like to give custom heights, widths and weights to the EditText and ListView in the main XML file only and not in their individual files. (Basically we want to set the layout of different elements in the main file, so that we can create any element like an EditText in a seperate XML file, import it into the main XML file, and then postion it at will on the screen)
Is this possible? If yes, then how can it be done?
Regards
I'd say you would want to define a style?
From that page:
For example, by using a style, you can take this layout XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />
And turn it into this:
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
You have one place where the style is (maybe main.xml isn't the best place for that, but I think your goal would be met even with a separate style file?)

Crashes whenever I try to use a custom view object in XML layout

I have made myself a custom LinearLayout by the name of com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget that hosts a couple of spinner widgets. This custom LinearLayout inflates the following XML layout (You might not need to look at this too carefully but it's here for completeness):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Min value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_min_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1" />
<TextView
android:id="#+id/to_text"
android:text="to"
android:textSize="14sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0">
</TextView>
<!-- Max value Spinner -->
<Spinner
android:id="#+id/discrete_numerical_range_selector_max_value_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1" />
I have placed one such object in the layout for one of my activities like so:
<?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:orientation="vertical">
<include layout="#layout/search_form_section_generic_top"/>
<include layout="#layout/search_form_section_car_specific"/>
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
<include layout="#layout/search_form_section_advanced_options" />
</LinearLayout>
The problem is that my app force closes immediately upon startup. I've checked by putting breakpoints in my custom LinearLayout that none of my custom code is even being run yet. Furthermore, if I copy-paste the layout code for my compound widget in place everything works, which indicates to me that I probably haven't left any important XML attributes out. What could be going wrong?
I fixed it by making the LinearLayout XML element in the widget layout into a merge instead, and moved all of the layout parameters out of the widget XML file and into the activity XML itself, thus replacing
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget/>
with
<com.theflyingnerd.DroidMe.DiscreteNumericalRangeSelectorWidget
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
If someone could tell me why this worked, it might help me and others doing it again, and you can take credit.
because you must specify the width and height of every view you use in you xml?

Listing different images along with text in android?

..i want to list different images..so
I dont fully understand your exact requirement. Anyways, you can use ListViews to make a list of images and textviews.
Assuming you want to have a ListView in which each row must contain an image and a text, you must define an xml inside /layout/ folder. Here is an example custom_row.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:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
/>
</LinearLayout>

Categories

Resources