I have the XML layout code:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="info" type="com.mycompany.orm.binders.MyBinderObject"/>
</data>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:gravity="end">
...
And this works great. However, I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end. I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.
I.e., android:gravity="#{info.mygravity}" (However, this line does not work. The compile-time databinding code fails).
Does anyone have an idea how to dynamically set the android:gravity based on a Data-bound objects property?
I have a property inside MyBinderObject called mygravity, and it is set to one of two strings: start or end.
Therein lies the problem: you're doing something reasonable and treating the layout XML like XML.
The layout XML attribute values can be of a variety of types. Sometimes, this is obvious, such as android:enabled="false" or android:layout_width="30sp". And, sometimes, the type really is a string or string resource reference (e.g., android:text="Foo"). But sometimes the attribute value looks like an English-language string, but it is really one of a set of enumerated values (e.g., gravity values).
I didn't realize that gravity was actually an enumerated attribute
A rough-cut rule of thumb: if the build would fail if you translated the value into another language, then it is an enumerated attribute (e.g., android:gravity="fin" instead of android:gravity="end", assuming that Google Translate gave me reasonable Spanish there...). If the translated value would work just fine, then it's any valid string or (usually) string resource.
I am trying to figure out how to read the Data Binding property mygravity to assign the gravity to the layout.
You will have to transmogrify the string values into equivalent Gravity constants like Gravity.START.
you can definitely use android:gravity="#{info.mygravity}" in layout file but make sure that mygravity should be int value.
you should refer this
like if you want center gravity, value of info.mygravity should be Gravity.CENTER
Related
<GridLayout
<ImageView
android:id="#+id/freeParking"
android:layout_width="52dp"
android:layout_height="58dp"
android:background="#57979F"
app:layout_column="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_row="0"
app:srcCompat="#drawable/ic_launcher_foreground" />
This is one of the child in my gridLayout. I want to change its layout_column and layout_row from MainActivity.
I am answering the question considering that you are working with the latest version of Android Studio.
Programmatically you can define the define the number of rows and columns as:
android:rowCount="number of rows"
android:columnCount="number of columns"
There are a few points you must always keep in mind:
1.First of all you can't directly modify the layout from Main Activity.
2.You can change them by accessing the plentiful options available in the Pallete.
3.Instead for layout_column and layout_row you can access the Linear Layout(horizontal and vertical) respectively.
4.Guideline Layout is also a secondary option available)
5.If you don't wanna use them, then you can manually modify them using the various constraints present in the attributes section (hoping you are familiar with them).
What you are presenting here is just a part of the basic code present in the activity_main.xml portion of the project file. This portion of code has no area that can modify your desired layouts.
In short if you want to define the number of rows and columns in the project file, then there are two attributes present in gridLayout. These are columncount and rowcount present in the attributes. One can use these to define the numbers of columns and rows respectively.
I found a library which allows an app to wrap text around an image - however once implemented it changed the size of my text - how can the text size be increase when using this library?
android:textSize= has no impact on the text size.
Neither does:
FlowTextView titleTv = (FlowTextView) findViewById(R.id.titleTv);
((FlowTextView) findViewById(R.id.titleTv)).setTextSize(20);
https://code.google.com/p/android-flowtextview/
Example:
<com.pagesuite.flowtext.FlowTextView
android:id="#+id/titleTv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text=""
android:textSize="20sp" >
In the short term a call to invalidate will probably get it working:
FlowTextView titleTv = (FlowTextView) findViewById(R.id.titleTv);
titleTv.setTextSize(20);
titleTv.invalidate();
However, I suspect you are using the JAR file right? It is quite out of date so I would recommend checking the source code out and using it as an android library project - setTextSize() should work properly then without needing a call to invalidate() (plus various other bug fixes etc).
Also - I never added the ability to set the text size via XML - wouldn't be too hard to add this though.
I checked the code of android-flowtextview and they have the text size hardcoded (check line 131 here). You have to change their source code or use the public method setTextSize(int).
Also, this link might help you, as seems that someone already did something as you are trying to do.
https://code.google.com/p/android-flowtextview/source/browse/trunk/src/com/pagesuite/flowtext/FlowTextView.java
There's a 'setTextSize(int)' method that should do exactly what you're looking for.
If you want to set it from XML, it's a little more involved. Since FlowTextView's constructors ignore the AttributeSet that gets passed in, you'll have to code this yourself. Follow guides like this: http://kotikan.com/blog/posts/2012/09/android-attributes to figure out how to add custom attributes to your views.
I just came across some tutorial code example which are using declare-styleable in XML and Context.obtainStyledAttributes in Java code.
My understanding is, it is trying to obtain the attribute (Like background color) of a GUI component.
I was wondering, why do we need to go through these cumbersome steps just to obtain a simple attribute? If I want to retrieve a GUI component attributes each time, I have to create a new XML file, and add in the attribute I want to retrieve to the XML file itself. Can we have something simpler?
Currently, here is what I need to do when I try to obtain attribute of a GUI component.
http://developer.android.com/resources/tutorials/views/hello-gallery.html
Create an XML file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
Have the following Java code.
TypedArray a = context.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
Can we have something simple like
helloGalleryInstance.getBackground();
As in Swing, here is what I usually do to obtain an attribute of a GUI component. It is fun and easy. Can we have something similar in Android?
instaceOfAComponent.getBackground();
Actually, there is a lot more to it than that. The technique of using styles and getting the attributes is a way to allow consumers of the custom component to use XML attributes that you define for the control. For example, you might create a TitleWithSubtitle control that has a subtitle text size attribute. This technique would let you get the value from the layout XML. You could still add a getSubtitleTextSize() method to the control itself, but this would serve a different purpose.
To get the background of a View, call getBackground().
I'm parsing some XML with XPath.
The XML code follows:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<teldir>
<contact>
<nameDept>D'ADAMO, Piergiorgio</nameDept>
</contact>
</teldir>
</response>
Using the expression /response/teldir/contact/nameDept/text() I put the result in a Java String with Node.getNodeValue().
This string is shown in a ListView using a custom but simple layout for each item.
<TextView
android:id="#+id/contact_name_dept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="20sp"
android:textColor="#FFFFFF"
/>
I have a unit test asserting the string is "D'ADAMO, Piergiorgio".
The problem is that only when the code runs in the emulator the item in the ListView shows "D'".
It seems that Node.getNodeValue() is truncating the string when the apostrophe occurs.
Maybe the Node DOM implementation in Android has issues?
The XPath specification requires that adjacent text nodes are concatenated, so using /text() should never give you half a text node. Unfortunately there are some careless implementations around, and if they run on a DOM that has multiple adjacent text nodes (as can often happen when entities are involved) they don't go to the trouble of merging them. It's a non-conformance and you should complain about it, but you'll be lucky to get it fixed. Meanwhile, try to get out of the habit of using /text() in your XPath expressions - it's nearly always bad practice. Instead, get the string value of the containing element using string(/response/teldir/contact/nameDept). It's very unlikely that any XPath implementation will get that one wrong (I hope!).
I am going through the android hello world tutorial, and have been stuck when trying to create an XML UI. For some reason, even on a new program, in which I have made no changes to the default build, it gives the error java.lang.NullPointerException after every character I type. I can't figure out why it is doing this, as I am just trying to edit the text between the Text I want to set it to say something other than what is set by default. However, even with a fresh build, no changes, and I just try to change the text within the xml tags, it still gives the error. What do I need to do to allow it to let me type? I am using the eclips IDE and the android sdk. I was able to do the first part of the tutorial that doesn't involve XML.
Just guessing, but I suspect you are doing something like the following:
<TextView android:text="#string/hello" />
and you are editing it to
<TextView android:text="#string/helloWorld" />
without creating a reference in res/values/strings.xml .
If this is the case, go to strings.xml and edit the proper string there, for example
<string name="hello">Hello World!</string>
becomes
<string name="hello">Hello everybody!</string>
Ok, I finally found the answer somewhere else, it was something wrong with how the file was created by default. I have to add the element xmlns:android as follows <resources xmlns:android="http://schemas.android.com/apk/res/android" />
Interestingly enough, the file will work if I type it one character at a time, dismissing the pop up with each keystroke. However the new element eliminates the pop ups. I am not sure why the error would pop up, yet the program still compile and run correctly on my avd. Oh well, if you have this error add the element and it goes away
I think in your typing contain UTF-8 not pure ASCII. You can change in eclipse by
in Ecipse IDE Window> preference> Under General tab, select workspace.
In text file encoding choose other, in these choose UTF-8 . It will be ok