Populate LinearLayout by external layout file - android

I created a drawer layout with LinearLayout as main content layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<!-- The main content view -->
<ScrollView
android:id="#+id/scroll_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >
</LinearLayout>
</ScrollView>
<!-- The navigation drawer -->
<ListView
android:id="#+id/navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:background="#FFF"/>
</android.support.v4.widget.DrawerLayout>
Now as is there are no Views under LinearLayout is it possible to create a layout in external file main_layout.xml and then call that xml in this LinearLayout defined in my DrawerLayout above.
The reason for this is that I am building the UI programatically so when user selects an option from my DrawerNavigation menu I call content_container.removeAllViews(); then I read a JSON schema and populate the content_container with Views defined in JSON. However when user goes back to the home page again I need it to load the default static views from my main_layout.xml
This way I don't have to programatically build the home layout/views.

These links may be helpful depending on your goal, LayoutInflator, Fragments, Include

Now as is there are no Views under LinearLayout is it possible to create a layout in external file main_layout.xml and then call that xml in this LinearLayout defined in my DrawerLayout above.
Your question isn't 100% clear but I'm assuming you want to dynamically create an XML file in internal or external storage and then inflate it as a layout.
If that's the case then the answer is no, you can't do this. Even though there is an API method allowing the use of a path to an XML layout file to my knowledge it was never implemented and won't work.
Layout files are pre-processed at build time in order to make layout inflation efficient and reduce processor / GPU load. The only way you can create layouts at runtime is to do it programmatically in-memory.

Related

ViewBinding - How to switch/choose layout dynamically (<include>)?

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

How to add Buttons and textView inside the fragment of android libraries places widget

I am trying to achieve like the image below how can i do in fragment android.libraries.places.widget i tried using relative layout but its not showing up inside the places widget can anyone suggest some good source to do it or is there any other layout which can be used to do it.
The Xml which i tried:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Maps.MapsAddress" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="10dp"
android:id="#+id/autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.libraries.places.
widget.AutocompleteSup portFragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chetpet Chennai"
android:textSize="14sp"
android:id="#+id/toolbar_address">
</TextView>
</FrameLayout>
Root layout use : Relative
SearchBar layout can use searchview or floating searchview(external libraries)
Device Location can use relative layout so icon place at the start of parent and etc
Saved addresses you can use table layout.
Assign id to the different layout so that you can stack the layout inside of the root-relative layout

layout_weight not working with LinearLayout inside Drawerlayout

I'm struggling with LinearLayout when using DrawerLayout. This is using the Android Studio template for DrawerLayout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:background="#00f"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</LinearLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.test.testdrawerlayout.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
The preview in Android Studio shows this:
http://imgur.com/UlPGzJS
However, when running on my Nexus 5, the following shows:
http://imgur.com/w4QeGbB
As you can see, the layout_weight="1" part is not showing at all. When I create a blank project with the inner LinearLayout however, the layout works (with the blue layout taking most of the screen with the red layout at the bottom, just like the preview).
Any ideas would be really appreciated, as I'm completely stumped right now. Thanks in advance.
It appears I was using an older Android SDK platform. Once I went into SDK manager and installed API 19, it worked (with a new project).
Maybe it was a bug in an older implementation?
This is definitely an Android issue (or at least appcompat/design library issue). I've tested with different layouts inside the DrawerLayout, and concluded that the problem was "content_layout" file - the one that is displayed below the drawer.
Only using a clean, plain FrameLayout worked. No CoordinatorLayout, no fragments, no custom layouts nor 3rd-party layouts, just a good old FrameLayout with "match_parent" as width and height.
Inside the drawerlayout you should have a FrameLayout which contains the main content for your screen and you should also have a ListView in most cases to hold the contents of the navigation drawer.
It should be as such:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Now place your LinearLayout within the FrameLayout. Think of the FrameLayout as the "container" that holds your main screen.
Note that the official Android documentations state that
The main content view (the FrameLayout above) must be the first
child in the DrawerLayout because the XML order implies
z-ordering and the drawer must be on top of the content.
For more reference and information, I would advise you check this link by clicking here.
Please mark this as the answer if it solved your problem. Thank you.
UPDATE:
Okay first, remove "xmlns:android="http://schemas.android.com/apk/res/android"" from your linearlayout. That should only appear once and in the main root xml, in this cause the DrawerLayout.
Next, fix how you are using your weights. Declare a android:weightSum attribute. Think of weightSum as a pie. In this case we set it too 5, so we have 5 pieces of the pie. Anything inside this layout using the android:layout_weight attribute. This first one is set to 4, which will take 4/5ths of the screen. The last one is 1/5th of the screen at the bottom.
Make sure this LinearLayout is still wrapped within the FrameLayout. Your LinearLayout should appear as such:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5">
<LinearLayout
android:background="#00f"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"/>
<LinearLayout
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</LinearLayout>

DrawerLayout Not Scrolling

I notice that the drawers within the Google applications are scrollable, but I cannot for some reason come to the conclusion of how to achieve a scrollable DrawerLayout. I attempted to construct the layout file with the following design paradigm.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mainScreen">
<!-- Layout of Activity -->
</FrameLayout>
<!-- DrawerLayout segment -->
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/drawerLinearLayout"
android:orientation="vertical"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start|bottom"
android:layout_marginTop="?android:attr/actionBarSize"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#77000000">
<!-- Layout of Drawer -->
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
But, with or without the ScrollView, the drawer just cuts items off at the bottom when they go beyond the end of the screen. I can't get any form of scrolling enabled. Not sure what I am missing or need to enable. Thoughts would be appreciated.
The LinearLayout in the DrawerLayout segment contains different styled views. One view displays title only with a divider below it, one displays an imageview with text next to it and another displays a title with a switch built into the row. So, multiple styled views need to be accounted for if done outside of XML coded layout files.
While using a ListView is a valid option, it was useful for me to attempt and resolve the situation for which I already had such a large XML file constructed for my layout in between the ScrollView above. As it turned out, there were only a few small modifications needed in order to get it working as intended. The latest layout file constructed is the following:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mainScreen">
<!-- Layout of Activity -->
</FrameLayout>
<!-- DrawerLayout segment -->
<ScrollView
android:id="#+id/scrollView"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start|bottom"
android:layout_marginTop="?android:attr/actionBarSize">
<LinearLayout
android:id="#+id/drawerLinearLayout"
android:orientation="vertical"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#77000000">
<!-- Layout of Drawer -->
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
The main modifications required me to alter where the gravity and the margin existed. The gravity needed to be in the surrounding ScrollView otherwise it would cause odd behavior that didn't scroll or actually crashed in some instances apparently. Then the inner layout needed to be changed to 'wrap content'.
If the margin was not moved into the ScrollView as well, it apparently doesn't scroll down to the bottom. It left a margin of scroll unscrolled at the bottom. Once this was resolved, the DrawerLayout worked as was expected at this point. The ListView option also proposed is another approach to be used, but as mentioned at this point it was worth me analyzing it a bit further to re-use the code that I already had written; especially with several different custom views that would need to be handled inside the view.
It seems that it only works properly with a ListView. You will take less time to migrate to ListView than trying to make the ScrollView to work.
You should use a listview along with the frame layout. Something like this :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<ListView android:id="#+id/left_drawer"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
You can also refer to the Navigation Drawer documentation at developer.android.com. It has everything you will need.

dynamically changing custom view as common footer in android application

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>

Categories

Resources