How to set z index by using some integer values - android

I have been working in android for past few months. The problem for me is now related to Z index value. I am having a layout with a textview,edittext and imageview.
Suppose i have a xml file something like this..
<Layout1>
<edittext><zindex>3</zindex></edittext>
<textview><zindex>2</zindex></textview>
<imageview><zindex>1</zindex></imageview>
</Layout1>
So my question is that am reading this xml file by DOM parser and i want to set the z index value for all these by the values defined in the xml. Now is there any function or property that i can use to do it.
I have learnt about coding it with xml, but that will make it hardcoded. I want a dynamic display so how do i adjust layout with the zindex values.... HELP PLZ

there is no Z-index in android layouts. You'll need to use FrameLayout or RelativeLayout if you need to place elements on top of each other in reverse order.
see Placing/Overlapping(z-index) a view above another view in android

You can use view.setZ(float) starting from API level 21. Here you can find more info.

Related

Short Key for Generate String From Layout XML

I have seen that there some short key for generate new string, dimens value etc from Layout XML file...But I don't remember it.
Example I have input new dimes in Layout XML called time_padding. its showing red error that there no any value called time_padding in dimens file....Now I want generate its from xml so it can be handy for fast work. Let me know if someone know it.
Thanks
You can use Alt + Enter then "extract string" to create new string under resources.string.
You can do the same for extracting dimesions as well.
In android, the layout parameters depends on the type of layout.
For example:
Suppose you are using Relative layout then you can use
android:layout_alignParentBottom
android:layout_alignParentEnd
android:layout_alignParentLeft
android:layout_alignParentRight etc
But these parameters are not available for the Linear layout.
So, please mention which layout you are using.

What are insets in android?

I am beginner at android development. I was recently looking at someone else's code and found some functions view.onApplyWindowInsets(windowInsets), windowInsets.getSystemWindowInsetTop(). And this word was used frequently in the same app.
I tried googling it and found InsetDrwable class with explanation
A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds.
Can some one explain me what is the meaning on Insets and what those piece of code up there meant?
A explanation with an example will be appreciated.
Suppose you have a TextView and you need to add a background to the TextView. But, on the other hand, you don't want the background to scan the entire View (TextView), therefore, you need to use Insets.
A Drawable that insets another Drawable by a specified distance or
fraction of the content bounds. This is used when a View needs a
background that is smaller than the View's actual bounds.
The background can be a Drawable. Hence you need to use the <inset> attribute inside your xml file (activity_main.xml) "For Example".
Then, after using the <inset> tag, you can specify some attributes such as:
<inset
android:drawable="#drawable/(Enter the file name under drawable)"
android:insetBottom="4dp"
android:insetTop="4dp"/>
For more information please have a look at InsetDrawable on Android developer.com
Hope this helps!

How to change the below target in run time in android?

For example, I have a imageView 1 , imageView 2 and imageView3
In the XML I use a relative layout to group them
eg.
imageView1:
below of 2
imageView3:
visiblity: gone
after certain action (in run time)
eg.
imageView1:
below of 3
imageView3:
visiblity: visible
I think something similiar, but can not find the correct syntax
imageView1.setLayoutParams(new LayoutParams(Below));
Thanks for helping
What's your API level? If, by chance, you are using a min SDK of 17, you can just use removeRule(int verb) to remove the below of 2 and then add below of 3.
Otherwise, what I do is I keep two instances of RelativeLayout.LayoutParams for a given view. Try something like this:
belowTwoParams.addRule(RelativeLayout.BELOW, 2.getId());
belowThreeParams.addRule(RelativeLayout.BELOW, 3.getId());
Where belowTwoParams and belowThreeParams are both instances of RelativeLayout.LayoutParams. Keep in mind that your "2" and "3" views need to have valid IDs, set either in the XML or programatically before adding these rules.
Afterwards, you just call imageView1.setLayoutParams(belowTwoParams //or belowThreeParams accordingly);

Scrollable TextView in Android Widget

I would like to create an android widget with a scrollable textview.
The solutions given to this question
Making TextView scrollable on Android
cannot be applied because it is a widget:
1.
This
findViewById(R.id.textview).setMovementMethod(new MovementMethod());
does not work, since findViewById is not available in AppWidgetProvider but only in Activity.
2.Putting a ScrollView around the TextView also does not work, because I get an
InflateException:
android.view.InflateException: Binary XML file line #23: Error inflating class ScrollView
Can anybody give me a hint, how to make a TextView in a Widget scrollable?
I solved this problem by putting the text view inside ListView with single node, which is supported in App widget.
http://developer.android.com/guide/topics/appwidgets/index.html#collections
It looks like this is not possible.
More on this can be found here: http://code.google.com/p/android/issues/detail?id=9580
and here:
How to make a scrollable app widget?
So, probably it is possible to do make appwidgets scrollable in the HTC sense environment but not for normal android environments.
A way around this is to add buttons that go up and down in a list.
I have been working on this with two buttons which increment and decrement through an array. Just having trouble accessing the global variables. Did you make any headway on this?
Another solution is:
Add to textview any web link - for example: www.google.com.
Setting text value with HtmlCompat.fromHtml method:
textView.setText(HtmlCompat.fromHtml("some text" + "\nwww.google.com", HtmlCompat.FROM_HTML_MODE_COMPACT));
After that vertical scrollbar is appeared.
But it's not elegant and full solution. It's temporary workaround maybe...
The complete full solutiion is bat-el-g 's answer - with adding ListView.
Current marked solution (which just tell: "it's not possible") - is wrong.
In mainactivity.java:
after code text view:
TextView txt = (TextView) findViewById(R.id.textView id);
Enter this code:
txt.setMovementMethod(new ScrollingMovementMethod());
and you will be ok.

Defining Z order of views of RelativeLayout in Android

I would like to define the z order of the views of a RelativeLayout in Android.
I know one way of doing this is calling bringToFront.
Is there are better way of doing this? It would be great if I could define the z order in the layout xml.
The easiest way is simply to pay attention to the order in which the Views are added to your XML file. Lower down in the file means higher up in the Z-axis.
Edit:
This is documented here and here on the Android developer site. (Thanks #flightplanner)
If you want to do this in code
you can do
View.bringToFront();
see docs
Please note, buttons and other elements in API 21 and greater have a high elevation, and therefore ignore the xml order of elements regardless of parent layout. Took me a while to figure that one out.
In Android starting from API level 21, items in the layout file get their Z order both from how they are ordered within the file, as described in correct answer, and from their elevation, a higher elevation value means the item gets a higher Z order.
This can sometimes cause problems, especially with buttons that often appear on top of items that according to the order of the XML should be below them in Z order. To fix this just set the android:elevation of the the items in your layout XML to match the Z order you want to achieve.
I you set an elevation of an element in the layout it will start to cast a shadow. If you don't want this effect you can remove the shadow with code like so:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myView.setOutlineProvider(null);
}
I haven't found any way to remove the shadow of a elevated view through the layout xml.
I encountered the same issues: In a relative layout parentView, I have 2 children childView1 and childView2. At first, I put childView1 above childView2 and I want childView1 to be on top of childView2. Changing the order of children views did not solve the problem for me. What worked for me is to set android:clipChildren="false" on parentView and in the code I set:
childView1.bringToFront();
parentView.invalidate();
Please note that you can use view.setZ(float) starting from API level 21. Here you can find more info.
Thought I'd add an answer since the introduction of the
android:translationZ
XML field changed things a tad. The other answers that suggest running
childView1.bringToFront();
parentView.invalidate();
are totally spot on EXCEPT for that this code will NOT bring childView1 in front of any view with a hardcoded android:translationZ in the XML file. I was having problems with this, and once I removed this field from the other views, bringToFront() worked just fine.
API 21 has view.setElevation(float) build-in
Use ViewCompat.setElevation(view, float); for backward compatibility
More methods ViewCompat.setZ(v, pixels) and ViewCompat.setTranslationZ(v, pixels)
Another way collect buttons or view array and use addView to add to RelativeLayout
childView.bringToFront() didn't work for me, so I set the Z translation of the least recently added item (the one that was overlaying all other children) to a negative value like so:
lastView.setTranslationZ(-10);
see https://developer.android.com/reference/android/view/View.html#setTranslationZ(float) for more
Or put the overlapping button or views inside a FrameLayout. Then, the RelativeLayout in the xml file will respect the order of child layouts as they added.
You can use custom RelativeLayout with redefined
protected int getChildDrawingOrder (int childCount, int i)
Be aware - this method takes param i as "which view should I draw i'th".
This is how ViewPager works. It sets custom drawing order in conjuction with PageTransformer.
Check if you have any elevation on one of the Views in XML. If so, add elevation to the other item or remove the elevation to solve the issue. From there, it's the order of the views that dictates what comes above the other.
You can use below code sample also for achieving the same
ViewCompat.setElevation(sourceView, ViewCompat.getElevation(mCardView)+1);
This is backward compatible.
Here mCardView is a view which should be below sourceView.

Categories

Resources