Is there a way to reuse a single activity layout-file (one orientation) for another activity with two orientations.
for example I have the MainActivity with one layout-file that just shows everything fullscreen:
activity_generic.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame">
</FrameLayout>
MainActivity.java
setContentView(R.layout.activity_generic);
And then I have SettingsActivity which should show everything fullscreen in portrait-mode and everything splitscreen in landscape-mode.
activity_settings.xml
//use activity_generic.xml somehow
activity_settings.xml (land)
<?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="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<FrameLayout
android:id="#+id/content_left"
android:layout_width="0dp"
android:layout_weight="40"
android:layout_height="match_parent"/>
<FrameLayout
android:id="#+id/content_right"
android:layout_width="0dp"
android:layout_weight="60"
android:layout_height="match_parent"/>
</LinearLayout>
SettingsActivity.java
setContentView(R.layout.activity_settings);
Of course there is a way to fix this problem within code by checking the orientation and inflating another layout based on that, but it feels kind of hacky. I tried to use an include tag but it didn't work. Does anybody else know the solution to this problem?
Edit
I managed to edit the activity_settings.xml so that it uses an include tag.
However it is a bit overkill because it uses almost as much code as the code from activity_generic.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/activity_generic"/>
</LinearLayout>
At this point i'm not sure if its even possible to use activity_generic.xml instead of activity_settings.xml (portrait).
You cannot use the exact same layout and achieve the same.
However there are some built in features that android has and are a common practice.
By default android shall pick the layout files from the /res/layout folder,
for both landscape and portrait orientations.
However, you can make a /res/layout-land folder and place a layout file with the same name and make landscape related UI changes there.
Android shall automatically pick this layout file for the landscape orientation.
You can check this link for further details: Android: alternate layout xml for landscape mode
Related
I was planning on having two layout versions for the profile fragment, one for new user and one for logged in user.
How would I dynamically switch/choose the necessary layout in the Fragment's onCreateView() method?
The most straight-forward idea that comes to mind is to use two <include> layouts and hiding one of them in onCreateView() depending on variables. But I was wondering if there is a smarter approach.
Current layout xml:
The main content is inside the second FrameLayout and I'd like to have two versions to choose from.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<ImageView
android:id="#+id/profile_monk_head_imageview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="#string/profile_monk_image_content_desc"
android:src="#drawable/monk_head" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This is what I want to switch out -->
<FrameLayout ... >
</androidx.core.widget.NestedScrollView>
</FrameLayout>
Use DataBindingUtil version inflate if layoutId is unknown in advance ( a.k.a dynamic binding). Otherwise, use the generated Binding's inflate method to ensure type-safe inflation.
Crosslink reference
I have a huge layout file with one flat constraint layout within.
I have android.support.constraint.Group elements that are identical. I want to move these to a separate file and then include them like <include layout="#layout/selection_group"/>
The problem I'm facing is that the group that I have in the file selection_group.xml is not previewed correctly in Android studio. Is there a way to make Android Studio preview this directly in the file or include them in a different way?
selection_group.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.Group
android:id="#+id/top_bar_config_one_background_group"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/top_bar_background_with_border_fx"
android:clickable="true"
app:constraint_referenced_ids="top_bar_config_one,top_bar_tooth_one"
app:layout_constraintBottom_toBottomOf="#+id/top_bar_container_background"
app:layout_constraintLeft_toLeftOf="#+id/top_bar_container_background"
app:layout_constraintRight_toLeftOf="#+id/top_bar_config_two"
app:layout_constraintTop_toTopOf="#+id/top_bar_container_background" />
<ImageView
android:id="#+id/top_bar_config_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:scaleType="center"
android:src="#drawable/ic_height_over_sea_100x26"
app:layout_constraintBottom_toTopOf="#+id/top_bar_tooth_one"
app:layout_constraintLeft_toLeftOf="#+id/top_bar_container_background"
app:layout_constraintRight_toLeftOf="#+id/top_bar_config_two"
app:layout_constraintTop_toTopOf="#+id/top_bar_config_one_background_group" />
<ImageView
android:id="#+id/top_bar_tooth_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="#drawable/ic_tooth_auto_40x40"
app:layout_constraintBottom_toBottomOf="#+id/top_bar_config_one_background_group"
app:layout_constraintLeft_toLeftOf="#+id/top_bar_container_background"
app:layout_constraintRight_toLeftOf="#+id/top_bar_config_two" />
<?xml version="1.0" encoding="utf-8"?>
</layout>
I don't want to nest multiple constraint layouts together. I think the root <layout> view in selection_group.xml is optimized and does not affect the performance? My goal is to reduce redundant code and not affect performance
Try added a merge tag to the included layout:
<layout>
<merge>
<group>
... etc. ...
</merge>
</layout>
In order to make that layout file ready to be included you need to replace <layout> with <merge>
In case you need to use DataBinding into your layout you need to have <layout first and than <merge> tags on top of the layout file
I order to have proper preview in your inner layout which is going to be included - add on top level into merge tag:
tools:parentTag="ContraintLayout"
Something like this:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
I'm using Eclipse ADT and I'm having a problem with a layout file. I can open it the internal xml editor, but when I open the Graphical Layout tool, it doesn't show anything, as if there was no xml file there.
This is the xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hermes.view.activities.SubTaskActivity" >
<fragment
android:id="#+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="35"
android:layout_marginBottom="15dp"
android:name="com.hermes.view.fragments.MiniMapFragment"/>
<ListView
android:id="#+id/list_subtaks"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60" />
</LinearLayout>
Note: I wanted to show a screenshot of the Graphical Layout tool, but StackOverflow doesn't allow it for reputation stuff :/
Is this an eclipse bug or am I doing something from with the xml file. I must also say that this only happens if I put a fragment in the xml file; if I remove the fragment tag, the problem disappear, but I need to see the design with the fragment in it !.
Thanks for the help
In my android app i want to put a common footer for all the activities. I created a custom view which extends Relative Layout, which contains a ImageView. It'll change the source for every 10 sec depends upon the data from server. I had put my custom view class in every activity's XML file also.
Now my problem is, i'm unable to find the way how it will instantiated in every activity means how the result view will added to every activity.
Simply, i want Admob Advertisement like feature in my application.
This is One of my Activities XML layout:
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
>
<RelativeLayout
android:id="____"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
----
----
</RelativeLayout>
<com.ninepstudio.movieme.activities.Banner
android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/activity_movies_layout" />
</RelativeLayout>
I'm trying to include the following layout twice:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
like the following
<include
android:id="#+id/include1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/view_pager" />
<include
android:id="#+id/include2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/view_pager" />
Actually, the second view pager does not work if I do it like that... The second view pager always stays empty... If I copy my view_pager layout and change the ids in this layout (pager to pager2 and indicator to indicator2) everything works fine. Is there a better way to do that? Copying layouts to achieve that seems to make the include useless for multiple includes of the same layout....
I'm getting the references correctly I think, but though it just does not work if I include the same layout...
pager1= (ViewPager)(findViewById(R.id.include1).findViewById(R.id.pager));
pager2= (ViewPager)(findViewById(R.id.include2).findViewById(R.id.pager));
Everything works perfectly if I copy the layout...
Edit:
I think it has to do with the FragmentManager, because the view pagers have the same id... But I don't know how to solve that correctly...
Yes it can be done. You can inflate the layout many times, but you have to make the inclusion programmatically. See the answer to same kind of question.