Why do i can't place widgets on my activity ? (Android) - android

I have created a Google Maps activity called 'PechhulpActivity' and
i want to place a button over it. But if i want to edit the activity it gives me the error about fragment tags:
A <fragment> tag allows a layout file to dynamically include different
layouts at runtime. At layout editing time the specific layout to be
used is not known. You can choose which layout you would like previewed
while editing the layout.
Here is a screenshot of my layout folder structure, first i thought i was missing a file like content_pechhulp.xml. If so, how can i create the correct one? I want to customize the Google maps activity like the info window and i want a button inside of it etc. I hope you guys can help me out.
Edit: (activity_pechhulp.xml)
<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:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doppler.stackingcoder.pechhulp.PechhulpActivity" />

Try using RelativeLayout as Parent and then add the child with fragment and another view like TextView or Button. It will placed above the map.

use a Relativelayout or FrameLayout as parrent for this
<RelativeLayout 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_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doppler.stackingcoder.pechhulp.PechhulpActivity"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_string_value"/>
</RelativeLayout>

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

How to show fragment privew in FrameLayout

I want show a preview of my fragment inside FrameLayout.
In fact I want display it only in android studio, And I do not want to render it.
My solution:
In Xml I load home_fragment preview using include tag:
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/nav_view"
android:layout_below="#id/my_toolbar">
<include layout="#layout/home_fragment" />
</FrameLayout>
In onCreate method i remove all child view of FrameLayout:
FrameLayout frameLayout = findViewById(R.id.main_frame);
frameLayout.removeAllViews(); // remove static preview in activity_main.xml
Everything works right now.
But I think there's a better way.
You probably want the tools:showIn attribute.
So if you have your main_frame.xml that <include>s your home_fragment.xml, in home_fragment.xml just point to the container layout:
<FrameLayout
tools:showIn="#layout/main_frame"
android:id="#+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other stuff -->
</FrameLayout>
Hope that helps!

Layout xml to Java code

Does anybody know some tool which convert layout.xml into Java Code generated programmatically. Simple example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/redownload_of_assets"
android:layout_gravity="center"
android:id="#+id/btn_redownload_of_assets" />
Button button = New Button(this)
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Reason of that solution is that I need add some controls into FrameLayout.
<?xml version="1.0" encoding="utf-8"?>
<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/fr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/slider_menu"/>
</android.support.v4.widget.DrawerLayout>
Try this service http://www.xmltojava.com/ it does not support custom view's but still can be useful. Any way i strongly recommend use plain xml. If you need to change layout dynamically there is two options.
Use ViewStub
Add all view's you need to layout and hide/show it when you need.
All xml tags are actually classes which you just inflate. Just give the layout and id, access it via the java and add whatever methods you want.

Populate LinearLayout by external layout file

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.

Categories

Resources