How is +id valid inside a RelativeLayout? - java

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

Related

Is it possible to show a variable from java editing a XML activity?

I have a TextView which I would like to show how the time passes in a game (it depends on the steps followed, not on real time elapsed).
<TextView
android:id="#+id/time_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"/> //Editing this line
Is there a way that I could set it to show variables from java to string, instead of setting a constant value?
This can be achieved by data binding concept in Android. A view model has to be created and associated with the text view as below.
<TextView android:text="#{viewmodel.userName}" />
Below link in android developer website explains in detail on how to create a view model.
https://developer.android.com/topic/libraries/architecture/viewmodel?gclid=Cj0KCQiAuP-OBhDqARIsAD4XHpdBptAbmhtZUFWh0EpQhpvNui7pc9_Br-w8xbimKZHpBkC1K9VC6u8aAg-ZEALw_wcB&gclsrc=aw.ds
I think you want to do something like this:
String time = "....";
//...
time = "xyz";
TextView time_tv = findViewById(R.id.time_tv);
time_tv.setText(time);

Android onClick() on include view won't let binding

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.

Codename one latest build issue with fonts?

I am trying to do a build for android on codenameone , after the last update when I send a build for android it converts all buttons and label text to UPPERCASE
whatever the text [was written in]
any build hint or fix to this issue
Regards,
See this for the long version https://www.codenameone.com/blog/pixel-perfect-material-buttons.html
Here is the short version, you can disable this using:
myButton.setCapsText(false);
Button.setCapsTextDefault(false);
Define the theme constant capsButtonTextBool to false
According to the latest Material Design guidelines, buttons use ALL CAPS for text, regardless of the capitalization you pass in.
https://material.io/guidelines/components/buttons.html#buttons-style
To prevent this, set this attribute on your <Button> tag:
android:textAllCaps="false"
Try this ,
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="false"
android:text="#string/button" />

Android: Default layout for listitem containing title and subtitle

I have made a listitem, containing a title and a subtitle. Looking to the result, I think it doesn't look really professional. So that's why I'm asking.
The listitem I'm looking for is pretty common (it's used in a lot of apps i.e. Android's default settings menu and it's also shown when you add a listview in the graphical layout editor tab in eclipse).
So my question is: Where can I find a default layout for a listitem with a title and a subtitle?
Resource id is android.R.layout.simple_list_item_2
Upper text line has id android.R.id.text1 and lower one - android.R.id.text2
Layout is located in the <ANDROID_SDK_ROOT>/platforms/<any_api_level>/data/res/layout folder
OR
You can use TwoLineListItem from the default Android controls list(it is located under "Advanced" tab in Eclipse Layout Editor)
OR
You can build your own layout with anything you like(for example LinearLayout with orientation="vertical" and two TextEdits added
So The Best Way Is:- I took out the simple list item 2 and made an layout in my project and with little editing it took the same layout as you may give in android.R.simple_list_item_2
So the code is:-
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingLeft"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingEnd="?attr/listPreferredItemPaddingRight"
android:paddingRight="?attr/listPreferredItemPaddingRight">
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#+id/text1"
android:layout_alignLeft="#+id/text1"
android:textAppearance="?attr/textAppearanceListItemSmall" />

How to have a header in every layout page?

I want that this code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView android:id="#+id/logo" style="#style/logo"
android:layout_alignParentTop="true"
android:text="#string/logo" tools:context=".MainActivity" />
to start in every layout. How can I do that I wouldn't need to add this to every layout page? Like for example in PHP I would use <?=include("header.php");?> (just an example, actually it's bad practise, doesn't matter here). Thank you.
Save your header in XML file and then include this XML as child layout in other layouts:
<include layout="#layout/headerlayout" android:id="#+id/headerLayoutid" ... />
The headerlayout.xml is name of your above layout that should be defined in res/layout and you like that be shown as header in all layouts(layout="#layout/headerlayout"),also headerLayoutid is id of your headerlayout(in it's parent) and you can reference to it in your parent layouts or in your code.
You can override all the layout parameters. This means that any android:layout_* attribute can be used with the tag. Here is an example:
<include android:layout_width="fill_parent" layout="#layout/image_holder" />
You can see more details in about include in this page.
Edit:
If you have problems in about finding views in included layout,see this questions,I hope these help you:
findViewById not working for an include?
Finding an view by id?
The <include> tag is what you want. Read about it in Re-using Layouts with <include/>
You can use the <include .../> tag in your layout to reuse your header where is needed. See here for an example.

Categories

Resources