Node can be replaced by a "TextView" with compound drawables - android

I am just starting on android development. Using the layout editor I added a Image View below the TextView.
By after adding the TextView the following warning was printed:
Node view can be replaced by a "TextView" with compound drawables
This tag and its children can be replaced by one <TextView/> and a compound drawable A LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable (a single TextView, using the drawableTop, drawableLeft, drawableRight and/or drawableBottom attributes to draw one or more images adjacent to the text). If the two widgets are offset from each other with margins, this can be replaced with a drawablePadding attribute. There's a lint quickfix to perform this conversion in the Eclipse plugin. Issue id: UseCompoundDrawables
Please help me resolve this. Thank you

The warning says "can be replaced by" not "should be replaced by". So you can ignore it. Not every ImageView and TextView combo can be replaced by CompoundDrawables. Even then let me if you want to know, how to do this, I have described it below.
TextView tag comes with 4 compound drawables, one for each of left, top, right and bottom.
Just add below line to your TextView.
android:drawableLeft="#drawable/up_count_big"
Or in java code, you can do it like this:-
mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
where left ,top ,right and bottom are all drawables.
See TextView#setCompoundDrawablesWithIntrinsicBounds for more info.

Related

How to set layout margin on Android Studio 3.4?

Trying to do this android tutorial,
Drag a Plain Text (EditText) element from the Palette pane to the
bottom of the layout, and add constraints to the left side of the
layout, the bottom of the layout, and the left side of the Send
Button. Set its attributes in the Attributes pane as follows:
Attribute
Value
id
editText_main
Right margin
8
Left margin
8
Bottom margin
16
But I do not see a way to set the layout margin. When I select the editText_main and input 8dp into the layout_marginLeft, and press Enter or Tab, the number 8dp disappears. It was swallowed without a burp.
It was not obvious, but I right clicked and then selected the Constrain menu and added constraints. Then I went into Attributes and it allowed me to modify the current value.
It seems like a design bug.
I'd advise you not to use the drag pane only, Some features are much easier with the xml editor
Goto you xml file add margins like this.
<EditText
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp" />
It is not a bug, a constraint layout child CANNOT have a child with margins because it has a set position (if you just drag and drop it from the palette), the position wont change no matter what, so adding margins is useless, when its constrained those margins become useful because lets say i constraint a button to the left but i don't want it completely to the left, so lets add a marginLeft of 8 dp, in that case it is relevant to have a margin thats why the editor wasn't letting you add them until you constrained it.
You can see this in the "Text" tab, the position is absolute, margins are useless when the position is absolute. So the editor prevents you from adding them.
Whenever you add a constraint to it the editor gets rid of the absolute position and lets you add margins.
Maybe your tutorial didn't specify what layout to use since it used to not be relevant until constraint layout was implemented.

How to add TextView at below in a RelativeLayout when there is no space at right?

I've seen many of questions for how to add TextView in a RelativeLayout programatically, but everybody adding it in LinearLayout with orientation vertical. Can any one suggest me or give me a link that how to add multiple TextView in RelativeLayout at right until it has space and then change the line.
I want a layout like this..
May be this works for you !
You can customize chips-edittext-library for your need. Customizing by setting background to transparent, and editable to false.
-> Or you can use any other library which is used for displaying emoticon in EditText and customize it according to your need. Like android-emoticon-edittext-spike
You can use the Flow Layout library that manages the view arrangement itself.
When there is no space left the added View is moved to the next line

Android XML: Good examples to use gravity rather than layout_gravity

I have done some basic xml layout from the Internet and I am confused as to what android:gravity would be used for vs android:layout-gravity. The UI designer seems to respond well when I change the layout_gravity, but I get no response to the regular gravity? What is up with this?
Is this similar to layout parameters with fill-parent and match-parent? one is gone now.
android:gravity is a statement from a parent to its content, indicating where the content should slide within the available parent space (if there is room for such sliding).
android:layout_gravity is a request from a child to its parent, asking that it be slid in a certain direction (if there is room for such sliding).
layout_gravity - It contains and it belongs to component's parent layout. Like if you set one TextView in LinearLayout, LinearLayout's width is fill_parent, but TextView basically will set at left-top. So now if you would give layout_gravity=right then your TextView will be shifted to top-right corner. But the text will not change its place. i.e. you had been wrote "Hello world" then "hello world" will not be changed to right, but its parent textView will be changed to right. So here in this case it will belongs to LinearLayout, means parent Layout.
gravity- It contains and it belongs to TextView as per above explanation. if you will give gravity="right" then "Hello-world" text will go to right-top corner of TextView not LinearLayout.

Bottom margin Android LinearLayout item in XML ignored, but works in Java code

I am developing an app with an activity with member reactions on a hike event. The reactions are the yellow "balloons" which are made using a LinearLayout. Each item is constructed from a XML file (listitem_deelnemerreactie.xml) which defines the layout for a reaction item. The top level of this layout file is a LinearLayout my itself.
I want some spacing between the separate elements, as well as some right margin. The most straightforward way to so this should be: setting a bottom and right margin on the top-level LinearLAyout element of the listitem_deelnemerreactie.xml layout file.
But setting the bottom margin on the LinearLayout has no effect on the vertical spacing, though the right margin does have an effect.
The only way to be able to set a vertical margin appears to be: setting is in the Java code, after attaching the inflated view to the container.
See the two images for the effect and the code.
Though setting the margins in the code is a working workaround, I still think it is strange this cannot be achieved in the XML. Why is the bottom margin attribute ignored while the right margin is not?
Any ideas?
Have you tried to set an android:padding="10dp" for example on your elements to spaced them ?

Imageview floating left

How can I create ImageView floating to the left of the textView, something like html:
<div>
<img src="src" style="float:left"> Text here.....
</div>
You have two options:
With LinearLayout, setting the orientation to horizontal so the image is first and then come the rest.
With RelativeLayout, there you can indicate the position of a element relative to another with the attributes of the style android:layout_toLeftOft, android:layout_toRightOf, android:layout_below or android:layout_alignParentTop, ...
However, it is not so flexible as CSS for some actions and, for example, wrapping text around an image is not so easy to achieve.
There's really no concept of floating elements in Android, but you can easily put an image to the left of some text using the drawableLeft attribute of the TextView. Example:
Otherwise, for more complicated layouts, parent views determine how their children are laid out. For example, instead of the concept of a div which simply wraps it children and uses the float and display attributes of the children to determine how things look, Android has more complex parent views (ViewGroups as they are called, since that's the super class) to control things.
Check out the docs for LinearLayout and RelativeLayout for some examples.
The positioning of views depend on the kind of layout you use. In case if you are using a RelativeLayout and you want to float your view(in your case the ImageView) which is within this Relativelayout, you can use the attribute of the ImageView (android:layout_toLeftOf="") specifying the view id of your textview between the double quote.

Categories

Resources