What View subclass has include? - java

When I use include in a layout, I have to set width and height both to include tag and to the layout it is linked to. Otherwards it won't work correctly. So, it seems, that include is not a link, but a subclass of View, if it has its own width, height, id and so on. But on developer's site there is not such class as include. Who are you, Mr. include?

The <include> tag can be thought of as a copy/paste of the internal layout. It's part of the xml parsing that's in the View Inflater classes. It effectively takes the layout ID, inflates the xml object you pass in, then adds it to the main layout that it's inflating based on the attributes of the included layout. You can use it on anything that inherits from View.
You can overwrite any of the attributes that have layout_ in them. You don't have to overwrite the width and height attributes if they are included in the root of the layout your including. However, you do have to overwrite those parameters if you are overwriting any other attribute. It's part of a bug in how <include> is parsed. If you want to change, say, layout_marginLeft, you would have to overwrite layout_width and layout_height to the same values that are in the layout in order for the parser to overwrite the left margin attribute.

include doesn't actually need a width and height if you specify the width and height of the root element of the included layout. In other words, this is entirely valid:
<include layout="#layout/my_included_layout" />
So long as my_included_layout has a root element with a width and height it will be happy.

Related

Enclose layout as an inner content of a bigger layout

I have a basic container layout in which inside it I have a FrameLayout with an id #android:id/content. This FrameLayout meant for other layouts to fill it.
Now my question is this:
Lets say I have a layout file content.xml which I want it's content to fill the said FrameLayout.Is there a way to do all this in XML?
I want something like the <include> tag, but the exact opposite. Call it <included-in>.
Is there such a thing or way to achieve it, or do I have to inflate them both, and insert the content in code?
Thanks!

Overriding: same onDraw for many Views (Button, TextView) elements

I need to draw custom borders in android to all of the views on my screen. Every view will have different parameters. To do this, I thought about making new CustomBotton, CustomTextView etc. classes and redefine their onDraw() methods. But the methods will contain the same code, so it's not nice to
make new classes for those Views and
rewrite the same onDraw() method with the same code.
Is there a more elegant/faster way to do this?
Do something like this for simple and fast solution (Doing things quick always has a performance trade off).
Dont deal with onDraw for anyview.
Make a single class extending LinearLayout.
2.1 Set Background of this Linearlayout as color of your border.
2.1.1 - More better read border color attribute from xml at runtime.
2.2 Set its padding as width of your border.
2.3 Set its width and height as wrap_content and wrap_content respectively.
Add single view to this LinearLayout. Either programatically or via xml.
<com.example.BorderLinearLayout >
<ImageView /> // or whatever. But a single view or viewgroup only
</com.example.BorderLinearLayout>
Hope this helps

Writing text to certain positions in a TextView

My application has a TextView, used for showing lots of data for the user. The data would be best represented in a structured, "semi-spreadsheet way", ie. some data would need to be always shown on topmost line of the view, some data on bottom-left corner, some on bottom-right corner, and the "middle" of my view would need to be reserved for other stuff.
As a minimum, I would thus need to reserve:
topmost row for certain data
bottom left corner for some
bottom right corner for some
the rest of my view to a rapidly changing data stream
Is there any UI component which would allow me to position my txt freely around the component, to achieve my goal? I need to keep this view (currently TextView) as one, since I need the view to be clickable for other purposes, and dividing the area into millions of small TextViews really is not the answer I'm looking for.
You can use RelativeLayout creatively to achieve what you need. There is no specific layout that will satisfy your needs. But RelativeLayout is something i can see that would serve your purpose if used creatively.
You can use this in your xml file for alinment.
android:gravity="left"
OR in java you can do
TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);
For aligning view inside relative layout you can do any of this
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
You have to use Relative layout in your layout xml file and position different textviews according to your need using align properties as described above.
Note: Android does not provide facility to write text in textview at particular position, what at most android provide is align text within textView to left/right/middle position using layout_gravity parameter, but this applies to whole text in textview.

Android ViewAnimator height changing base on child

By default, the max size of all children is used as the VeiwAnimator's layout size.
How to create ViewAnimator, which height changes depending on the size of displaying child?
Either by specifying the android:measureAllChildren="false" as a ViewAnimator XML attribute or viewAnimator.setMeasureAllChildren(false) in code.

Android: ListView fixed height of child view

Please advice how to set fixed heigh (in dip) for the child view of ListView component?
I am using relative layout as root layout for the child view
when I set backgoround image to relative layout it becomes very height (maybe because backgoround picture is large) and I want to set precisely the height in dp.
Stumbled on this problem while looking for an answer to a somewhat related question.
Anyway, the problem is with inflating the view. In your getView (or newView if you're using CursorAdapter), when inflating a new layout, instead of doing
inflater.inflate(R.layout.your_layout, null)
do this instead:
inflater.inflate(R.layout.your_layout, parent, false)
Passing the parent view will make the child honor it's parents bounds, and false means you're instructing the inflater to NOT attach it to the parent. Setting a fixed height in dip will work after you do this.
For specifying the child height in dip via java code see following discussions:
1. setWidth in dip
2. how to specify padding in dip
3. Correct way to specify dimensions in Java code

Categories