I have the following XML file for a View on Android.
As I am trying to integrate the Google Maps, I need to declare the namespace map, in order to set it's properties. Using like this, it looks like I cant declare the namespace inside the fragment.
I've also tried to declare the namespace after the namespace "android" (in the beggining of RelativeLayout) but the namespace is still not recognized on the fragment.
How can I solve this problem?
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/venues_map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="13" />
For what I know, there is some bug that doesn't allow to put the map namespace properties inside the fragment if it not the only thing on the screen ( Meaning there are more views surround it).
Why wont you set your properties using code?
If you want to set zoom level do this:
cameraPosition = new CameraPosition.Builder().target(latlng).zoom(14.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
map.moveCamera(cameraUpdate);
Related
I have a separate layout that I want to call onClick and update a field on callback
<include
android:onClick="#{() -> viewModel.changeItem(2)}"
layout="#layout/item"
app:attr="#{viewModel.title}"
app:desc="#{viewModel.description}"
app:active="#{viewModel.isSelected}"
/>
But it returns the following binding error:
Cannot find the setter for attribute 'android:onClick' with parameter
type lambda on com.X.databinding.ItemBinding.
But I can binding on other views
<TextView
android:onClick="#{() -> viewModel.changeItem(1)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
What I suggest you is to call the onClick directly from code.
Setup an ID for your view in your .xml, then set something like this :
val item = findViewById(R.id.your_id) etc.
item.onClick { functionYouWantToCall() }
However, I do not know if this is possible to set an ID or an onClickListener on an include layout.
If you can not do it, simply put your include inside a LinearLayout, then set your onClick on it.
I used to set onClicks in the .xml too, but I think it is much more efficient to set it in the code.
Moreover, I do not know if you can set arguments in a function you call from the xml.
In this Android Navigation Icon - Profile picture instead of hamburger icon stackoverfolw post
ImageView profileImage = (ImageView)
binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);
How an Where to define R.id.iv_profile_image in which xml file ?
I couldn't find the XML code in the example you linked. However, I noticed the following line in the Activity's onCreate() method. This would indicate that the XML file being used is called activity_maps.xml. Most likely this is where the ImageView was placed.
binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
Assuming this assumption is correct, the ImageView can be declared something like this. The answer to "where" within the XML depends as XML is a declarative language.
<ImageView android:id="#+id/iv_profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
...
android:contentDescription="#string/to_replace" />
i just come trough a doubt
We had rexource XML and Layout
we can call another layout.xml by this way
<include layout="#layout/content_main" />
in same way can we call #xml ?
Just a doubt
and I know we can so it from code java by (R.XML.ook);
Is there any other way from layout ?
Thank you
It's impossible. I'm wondering what is your purpose when you want to call #xml in layout file?
Layout file can only include View and ViewGroup. So only view tag (TextView, Button,...) or layout tag (include, merge tag) or ViewStub are available for using in layout file. xml file can have many invalid tags, so it can't be use in layout file.
In a relative layout I see a pattern as:
<TextView
android:id=“#+id/txt_id”
etc
android:layout_alignBottom="#+id/some_other_txt”
etc
/>
<TextView
android:id="#+id/some_other_txt"
etc
/>
I thought +id is used only when creating an id for a widget. Is this a kind of “trick” to layout a widget relative to another widget declared later in the file?
UPDATE:
This question is specifically about the RelativeLayout possitioning. Not about the difference in syntax in general as the linked question
Its just a reference point for placing the different elements in the layout builder.
From this SO answer
The first time you reference an ID, use the #+ prefix, which tells the resource builder to add the ID,
Here you're making a forward reference to a widget & id that's not already defined:
<TextView
android:id=“#+id/txt_id”
etc
android:layout_alignBottom="#+id/some_other_txt”
etc
/>
Here you are defining it:
<TextView
android:id="#+id/some_other_txt"
More context here
I want to learn - if there is a way - how to define a custom view type, and use it in XML layouts.
For example, I have a custom button that looks like this:
<LinearLayout>
<ImageView />
<TextView />
</LinearLayout>
I know I can save this as mybutton.xml, and do <include layout="#layout/mybutton" />, but is there a way that I can use this like this:
<MyButton />
?
I would also like to be able to instantiate this new, custom class in Java code. Like this :
MyButton mb = new MyButton();
Should I define MyButton as a Java class? If so, what should I extend, and how ?
Thanks for any help !
You will need to define a custom class for your object.
MyButton.java
public class MyButton extends View{}
You will have to create the constructor to inflate your XML document, and then call the object in your XML by the fully qualified package name.
<com.example.MyButton/>