Change several layouts for different densities at the same time - android

If I have three different layouts for a same view (lets say, one for phone, one for tablet and one for tablet in landscape) and I want to add/change a property that the three of them share (lets say, the android:background), is there a way of adding/changing it in one place and have it automatically in the other two layouts? Is there an alternative to copy-and-paste it?

Unfortunately not really. You could have separate layouts for each set of elements and use include to include them in each subsequent layout, then just updating the single layout would update all of them. But this would mean having a lot of stand alone layouts. A good example of this is using a seperate Toolbar layout like so:
Toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
style="#style/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Then have this line in all layouts which you want to use Toolbar.
<include layout="#layout/toolbar" />
This technique could be applied to anything you like, and would allow you to in future only edit each separate layout once and have it applied everywhere you use it. You can also edit dimensions inside these Layouts under the include, or in code.
The only other way to achieve what you want would be to handle it all explicitly in code but I wouldn't recommend this.

You can apply style for the widget(TextView or LinearLayout for example) that uses background property and determine background property in that style.
Changing background property in style will affect all the widgets this style was previously applied to.
http://developer.android.com/guide/topics/ui/themes.html

Related

How to group multiple Views without using nested Layouts

Consider this situation:
<LinearLayout style="#style/Basic_W_mp_H_wc_Vertical">
<!-- IMAGE-1 -->
<TextView style="#style/Copyright_TextView_1" />
<ImageView style="#style/Copyright_Image_1" />
<include layout="#layout/layout_copyright_info"/>
<View style="#style/HorizontalSeparatorGray"/>
<!-- IMAGE-2 -->
<TextView style="#style/Copyright_TextView_2" />
<ImageView style="#style/Copyright_Image_2" />
<include layout="#layout/layout_copyright_info"/>
<View style="#style/HorizontalSeparatorGray"/>
<!-- IMAGE-X -->
[...]
</LinearLayout>
Now I want to hide copyright information for IMAGE-1. I have two options:
Create a wrap-layout (LinearLayout) with an Id, so I can find and hide it
Give Id for every View find and hide them.
The first option will work, but I don't want to create an extra nested layout for this.
The second option will also work, but my layout will get unreadable if every view has an Id.
I know there is a third memory-optimized option to group multiple views, but I can't remember how it works and google search gives me always ViewGroup...
Any idea?
You could try to use what's called the merge tag and pair it with the include tag. What this does is it will allow you to use the same layouts in different files (since you seem to have multiple groups together). The <merge/> tag will merge the elements in to the top-level layout (in this case a LinearLayout). I have not used it with this scenario, so I imagine what will happen is you will not be able to retrieve them in a group. It is worth a shot though.
I think option 1 is not a bad choice either for these reasons:
It's easier to code. If you don't group them together than you'll have an extra lines and remember extra IDs. With just one Layout wrapped around it, then you can just hide one layout.
It's easier to maintain. What happens if you change the copyright look? What happens if you want to change the rest of the layout? With one wrapped, it's easier to just fiddle around with one view and everything inside stays intact.
The performance hit isn't that bad. Overall, this looks like the top-most root view. Inflating this Layout won't be as bad as if you were say, inflating a ListView item. This is a one-and-done operation and that's it. You don't want to fall in to the trap of doing premature optimization where you sacrifice code maintainability in favor of optimizations without any benefit.

PreferenceScreen Layout in sliding tabview/layout

How can i include a preference.xml in a fragment that is in a tab?
All i want is the layout i have my own logic for settings, i just want the standard preferencescreen look.
This is my latest attempt.
<include layout="#xml/preferences"
android:id="#+id/rtes"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
</include>
I thought this would be easier then to copy the look with a standart layout ...
android doesnt like to include layouts from the xml folder into layouts from the layoutfolder , i still think this should be possible but i couldnt find a way.
If anyone ses this question use this https://stackoverflow.com/a/30334930/6752994 and fake it... dumb i know ...

Total beginner; need help properly setting up my Android 5.0 Toolbar

I do have a toolbar right now, but I don't really understand a lot of the things that I needed to add and copypaste from tutorials and other questions to get it to work. Also, my toolbar doesn't display an elevation even though I set android:elevation to 9dp.
Similar to the Google I/O app, I've set up a resource in /layout for my toolbar. I copied the name and named it "toolbar_actionbar_with_headerbar". Please excuse that it doesn't make any sense.
This is the content:
toolbar_actionbar_with_headerbar.xml
<?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="128dp">
<Toolbar
android:id="#+id/toolbar"
android:elevation="9dp"
android:layout_height="128dp"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:gravity="bottom" />
</LinearLayout>
Now, as it's apparently supposed to be done, I am applying my toolbar to activities by including them in their /layout XML-file. Works fine.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
[...]
<include layout="#layout/toolbar_actionbar_with_headerbar" />
[...]
</RelativeLayout>
However, this is my first question. When I did that initially, my toolbar was cut off at the sides like all other content of the activity, according to the margins set at the beginning of the xml. Right now, I simply deleted those, and add margins for every text field and other things on the activity. There probably is a better way?
The second question is: Why doesn't my elevation work? I've even tried setting it through code onCreate, but that doesn't work either. And lastly, what would I have to do to properly support devices pre-Level21?
Thanks a lot.
You should read this post: appcompat-v21
Take care when you copy some code text, there are some ” that are wrong, and you should change to ".
About elevation (you can read it in the post too), it only works with lollipop.

Set visibility via bindings - dimens.xml

Is it possible to set the visibility via bindings? (dimens.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="#whatever/visibility_of_this_view"
android:orientation="horizontal">...
so that I can put something inside of the dimens.xml like:
<whatever name="visibility_of_this_view">visible</whatever>
I don't want to do it via code or different layouts, just with different dimens.xml.
How ever your different dimens are selected (layout, api, what ever) you should use the same mechanism to select another layout or even a different style which sets the visibility. Every other way seems to be a abusive way :)

Can I create a statich part of view

I`m wondering about one thing. If I decide to have a part of my View same in every Intent. For example 2 buttons at the bottom of screen and for example I have 3 diffrent views, List, Detail and a third one :) Do I need to put the buttons on every xml schem for each view or can I create other xml and attach it in each activity with all listener etc.
If I can attach in activity how can I do that ?
Create a XML buttons.xml with the common elements
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/myButton"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
<ImageButton
android:id="#+id/myButton2"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
</merge>
Include it in another xml:
<include layout="#layout/buttons"/>
where buttons is the name of the xml file to be included
You can use the <include /> tag in your XML files.
See Layout Tricks for an example.
Create a separate layout for your buttons then in the layout you wish to display them use the include tag.
This will allow you to reuse the same layout in multiple parent layouts.
The include works as stated above you could also consider using fragments
Fragments
You can use these in older versions of Android by including the compatibility library in your application. It's definitely more work than a simple include but if you need some reusable UI for more sophisticated features than simple buttons you might want to look at that as well
How to use compatibility API

Categories

Resources