I have the following layout:
<LinearLayout width:fill height:fill >
<RelativeLayout width:fill height:wrap >
<ImageView width:fill height:wrap >
<LinearLayout width:wrap height:wrap />
</RelativeLayout>
</LinearLayout>
The innermost LinearLayout seems to have no idea that it is inside the RelativeLayout when it comes to vertical properties. Gravity works only horizontally. For all intents and purposes (vertically), the inner LinearLayout thinks that the outer LinearLayout is its parent. Aligning parent Top/Bottom stretches it and the RelativeLayout to fill the outer LinearLayout.
Ultimately what I want here is for the relative layout height to wrap the imageview almost as if it was its background (though it's meant to be an overlay on top of another view on top of the background), and the linear layout to simply work inside the constraints of the relative layout.
Furthermore, I want to be able to clip anything that goes outside the bounds of the relative layout wrapping the imageview. What's my best option here?
I think your issue here is that wrap_content does not bound the upper limit of the View (at least by the parent). As such, the height of your RelativeLayout can go on to the full size of it's parent. If it's parent wasn't wrap_content too, it would stretch out to it's parent and so forth until it reaches a parent that has an upper-bound.
In this case, your top parent is bound by the size of the phone, so it stops there. Instead of using gravity on your inner-most LinearLayout. Use the attribute layout_centerInParent="true" on your inner-most LinearLayout. Unless the layout becomes bigger than your ImageView it should conform to the center of your RelativeLayout.
EDIT:
You may also want to experiment with the different Scale Types of the ImageView. There are some that will scale the image as big as it can get without cropping or distorting the ratio. There are some that will always fill the entire view but may crop or distort the image.
There are a lot of things that are unclear from your post, probably you should post the complete version of your XML code.
From my understanding, I guess you're not using RelativeLayout constraints such as layout_alignParentBottom="true" or layout_centerVertical="true". Those will work, unlike the gravity properties.
Related
I got BottomNavigationActivity and fragment in it. My fragment seems like it has margin on it, but it doesn't. If I stretch it - top part disappears.
How can I solve it?
That's because your ConstraintLayout has a android:paddingTop . Remove that I can't see it being necessary?
Also your fragment should have:
android:layout_width="match_parent"
android:layout_height="match_parent"
But because of your constraints I don't think the layout_width and layout_height is respected, might even be able to be 0dp you would need to test if that works for you
You are using a ConstraintLayout as Parent. There is top padding in your layout. remove the padding line from ConstraintLayout.
You set a custom height and width for the Fragment. Replace the custom size with match_parent. That will solve your problem. You can also use 0dp instead of match_parent. 0dp will automatically resize your fragment window according to the screen size.
I have a ScrollView in which I have inserted a ConstraintLayout that contains a fullscreen ImageView and some other components below it.
What I want is for the image to shrink in height (to a certain limit) whenever I scroll down.
Here's an example of what I'm seeking: https://imgur.com/rlOr0HA
As for resizing, after some research I figured I'd have to create a LayoutParams object and then affect it to the ImageView. No problem there.
But as for detecting the scroll event, I've had some trouble. I tried the setOnScrollChangeListener on my ScrollView, but the problem is that it requires an API level of at least 23. So I wonder if there is another solution that works for lower levels as well.
Another problem I'm having is how to make the resizing proportionate to how much the user has scrolled.
You are looking to do the coordinator layout with a collapsing toolbar. This is built into android and you do not need any code changes. The inflater will inflate the layout and everything will work.
In your xml layout file you will need
<coordinatorlayout
<Appbar layout
<collapsingtoolbar
<ImageView> <-- your image goes here
/collapsingtoolbar
/Appbar layout
/coordinatorlayout
<nestedscrollview
<textview> <--Your content that moves up goes here
/nestedscrollview
<floating action button> <-- your example shows one of these buttons but its optional
What's the recommended approach for ensuring an ImageView button is the correct size according to its parent?
I have a VideoView set to "match_parent" (screen width) and an ImageView over the VideoView that is 60dp x 60dp, which is fine on the screen I'm testing.
Problem is, if I rotate the phone and the VideoView is much smaller, the ImageView is no longer proportional and in the correct position. Same issue is true for larger screen sizes such as tablets.
I want the ImageView to always be the same proportional size and appear in the same position relative to the VideoView (its parent).
You should consider using layout_sum in android:
Since I don't know how you are exactly coding this, I can only provide assumptions based code. I am assuming that you would want to proportionately align an VideoView and ImageView in a parent. (Assuming you required 30% of the space to be occupied by the ImageView and the rest 70% of the space to be occupied by the VideoView).
You can code the XML file as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
android:layout_gravity="center">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7"/>
</LinearLayout>
This will embed the VideoView and ImageView into the LinearLayout (Parent view).
Try to use RelativeLayout,wrapContent and avoid using hardCoded sizes.
Or better define a separate layout for Landscape and Portrait View that may solve your issue.
Even if your issue is not solved try using this library i frequently use it in my projects-SDP
I ended up doing this programmatically using ViewTreeObserver GlobalLayout listener. It works great and I only need it for one view on the screen, so not a big deal. Button appears in the correct position relative to its parent size even with the phone is rotated.
var topPadding = Convert.ToInt32(_videoView.Height * .25);
_buttonLayout.SetPadding(_buttonLayout.PaddingLeft, topPadding, _buttonLayout.PaddingRight, _buttonLayout.PaddingBottom);
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
I need to have a dialog (it's a game dialog) where buttons are at the lower corners of the dialog. Not inside the dialog but rather on the very corners (i.e. part of the button will reside over the dialog and the part will be outside of it).
First, as far as I know you can't move layout children outside their parent.
I've never tried exactly what you're going for, but I think it can be done. The trick would be to go with an activity with a dialog theme (you can find examples of these on the developer site or the API demos). Make sure your layout's root node has width and height set to wrap_content. Your root layout should be a RelativeLayout and have NO background (android:background="#0000").
Next, add another layout to your root node (FrameLayout would probably work) with a custom drawable for a background (or use the one that the default dialog uses from the framework) and width and height set to fill_parent or match_parent. Set android:padding to some dip value which pulls the background in from the edge of the dialog.
The only thing left to do would be to add your other layout elements to the root node. The FrameLayout will be drawn beneath everything else, and the padding will create the illusion of borders which do not encompass your UI.
Update
Yikes, just tried the above with good and bad results. First, you'll definitely want to look at the "Custom Dialog" example from the API demo, which makes use of:
CustomDialogActivity.java
layout/custom_dialog_activity.xml
xml/styles.xml
drawable/filled_box
Create an activity which uses the above xml layout file, and set the style for the activity to Theme.CustomDialog that you defined in xml/styles.xml. This will get you a red background for your activity. You can then edit the filled_box shape file to just have one background attribute set to invisible ("#0000"). The result should be an dialog-shaped activity with no background.
Next I tried to hack a background using my thoughts from above. The idea should be that there's a phony background drawn behind the other UI elements which does not encompass them, so it could be "shrunk" using layout_margin and not affect them. The problem here is that the phony background needs to have width and height set to relative to the other UI elements, so it sort of HAS to encompass them, so it can properly measure its own width and height relative to them.
So I think the solution could be to do most of what I've said above, except don't try the phony background thing. Just use a 9-patch drawable for your root layout background, and shrink the edges of your background to be drawn farther in than your content. You'd still use the custom theme stuff from above with an invisible window theme.
Here is a sample layout which i tried:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:orientation="vertical"
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_frame">
</FrameLayout>
<Button android:layout_width="wrap_content"
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:text="Button"></Button>
</RelativeLayout>
here is the screenshot:
hope u get the hint , goodluck