How to mantain aspect ratio of elements while rotating a ScrollView? - android

Let's assume a device of 800x1280 pixels in portrait mode with the following layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
android:fadingEdgeLength="0dip" >
<LinearLayout
android:id="#+id/MyLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
and with android:configChanges="orientation" in the Activity manifest.
Dynamically I add two childs views to MyLinearLayout with Layout of width = MATCH_PARENT and height = getWindowManager().getDefaultDisplay().getHeight(). So, I can scroll vertically between the two childs, each occuping a entire screen.
Now, when the device gets in landscape, the child's width get the dimension of 1280, stretching from the original 800, because of the MATCH_PARENT attribute.
Vertically, they are not stretched, since they were created with a fixed height of 1280 originally, they keep with 1280 of height.
What's the proper way to stretch this views vertically?
If I try to child.setLayoutParams inside onConfigurationChanged to width = MATCH_PARENT and height = 2048 (which is 1280 * 1280/800), I get an exception.

I'm working with dynamically instantiated CanvasView (from the S-Pen SDK) childs. I was able to do what I wanted with the help of this answer.
I have inherented com.samsung.sdraw.CanvasView and overrided onMeasure to preserve aspect ratio the way I wanted, and I needed to call setEnableZoom(false) for each child to correct content stretching, it was stretching the contents in double in landscape mode, going out of the screen boundaries.

Related

Xamarin.Forms 2.1 ListView Cell can't use match_parent any more?

In Xamarin.Forms 2.1, at least on Android, setting a control to height match_parent doesn't do anything.
Here is my setup:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="125px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutComment">
<LinearLayout
android:id="#+id/layoutTest"
android:layout_width="20px"
android:layout_height="100px"
android:background="#android:color/holo_red_dark"
>
</LinearLayout>
<LinearLayout
android:layout_toRightOf="#id/layoutTest"
android:id="#+id/layoutLinesContainer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="50px"
android:minHeight="50px"
android:background="#android:color/holo_blue_dark"
>
That is basically:
- Relative Layout with height wrap_content
- Linear Layout A with fixed width and height
- Linear Layout B with a min height/width and match_parent for height
The reason I put a dummy LinearLayout A is to tell the parent that it should at least fit this height.
I expect LinearLayout B to have the same height as its parent or AT LEAST the height of LinearLayout A that came before it or the min height of the parent.
However, LinearLayout B gets height 50px instead which is its min height.
Here is a screenshot:
Red is A and blue is B. B should be the full height of the cell or at least the same as A or the min height of the parent, but instead it uses its min height 50px.
This problem only happens when I used a cell renderer, it doesn't happen if I use a cell without a custom renderer but with its view set to a custom view that has a custom renderer.
My renderer is not doing anything abnormal, it's simply inflating that layout file, setting the provided "convertView" to it then setting the different values.
What should I do? All I want is for LinearLayout B to have the same height as its parent...

Changing background of LinearLayout changes it's width & height

I tried to change background of a LinearLayout to some wood texture, but it changes LinearLayout width and height depending on dimensions of the wooden background!
I need LinearLayout to disregard background dimensions!
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/wood"
android:orientation="vertical" >
some content here which I need to wrap height to them!
</LinearLayout>
Your LinearLayout's height is "wrap_content". Which means it takes height of the content(your background). You can change it to "match_parent" or give it a fixed size in dp. So that it remains same even with background.
If You want it to be of fixed either use fixed height and width in dp Or dynamically calculate height and width of old background and then apply new background with setting LinearLayout height and width to be of size you calculated programatically.
I simply replaced it with another container such as ConstraintLayout.. I don't have time to figure it out.

How can I design a layout bigger than phone's screen?

I'm developing an Android application and I want to design, in eclipse, a layout bigger than screen height.
I have a layout for a fragment and this fragment will be inside a ScrollView on FragmentActivity.
This is my fragment's layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/user_pro_main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text_state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/layout_state"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Do I have to change android:layout_height="match_parent" to make it bigger on eclipse's designer?
What do I have to do if I want to see the layout bigger on eclipse designer?
Answer is pretty simple: you can't view layout which is biggern then screen on Eclipse Editor.
Possible workarounds:
1. Comment part of top views (visible) to see bottom (which are invisible), then uncomment when ready to launch.
2. Change Device Preview to bigger resolution (Nexus 10), this will give you some extra space.
You can always explicitly set the exact dp value in layout_height, but of course most of the time I don't think you want a fixed value, so do it programatically.
LinearLayout yourLayout; // Get it by findViewById()
yourLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, your_calculated_height));
You would set android:layout_height="wrap_content" and as you add child elements beyond the physical screen it will continue to stretch the layout.
As for viewing this on Eclipse, I'm not sure. I personally would just run it on a device to view it.
just calculate device height and width and add int value to calculated height and width at runtime at layouts height and width.
public void deviceDisplay(){
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
}

Scrollview + scaling images = doesn't scroll properly, why not?

I have an image that I scale to the width of the phone/tablet. It is 224x1632. The imageview is within the scrollview, and I need to be able to scroll up and down on that image.
The issue is that the scrollview sets according to the image size (1632 in length) on create, but when the image scales/stretches it is 3x taller than the original. Now the scroll-view is too small to scroll the entire image.
Any way to make the scrollview fit the image?
Note: the image length will differ per phone, so I can't set it to a predetermined size.
XML Code:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:scrollbars="vertical"
android:src="#drawable/tiles" />
</ScrollView>
You set your ScrollView's height to wrap_content which means "be as big as my content." You also set your ImageView's height to fill_parent which means "be as big as my parent." Either one of these statements is enough to prevent scrolling. Your ImageView should be wrap_content and your ScrollView should be fill_parent.
Try scaleType="center" instead of "centerCrop"

LinearLayout remains width 0 (despite explicitly setting, or implicitly with weights)

With the below layout (a portion of a larger layout), the container layout's visibility is set to "gone" until a button is clicked. On the button click, if the container_ll is not visible, it's set to visible, and a custom view is added to the reminderViews_ll container.
The views are being added, and the container_ll view is visible on button click. What follows is the width and height of various views after the button is clicked.
container_ll width 420, height 96.
lineDivider_view width 420, height 2 (as expected)
reminder_img width 36, height 36 (as expected, hdpi phone)
reminderViews_ll width 0, height 96 (argh)
<LinearLayout
android:id="#+id/container_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
>
<View style="#style/lineDivider_view" />
<ImageView
android:id="#+id/reminder_img"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_horizontal"
/>
<!-- Stick the actual Reminder TVs + Del buttons in here dynamically -->
<LinearLayout
android:id="#+id/reminderViews_ll"
android:orientation="vertical"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
</LinearLayout>
I'm at a bit of a loss as to where to go from here. I was thinking invalidate layout, to force it to draw again after making the view visible, but that's never worked for me (seemingly), and if the reminderViews_ll can get a height of 96, then it can't be an issue with when it's calculating the dimensions of the LinearLayout.
I hope you have enjoyed reading this question as much as I have writing it. Any pointers, as always, appreciated.
The numbers you show look correct to me.
The container is width 420. The other views that are set to fill_parent or wrap_content take up all of the space (actually more). The Linear layout then goes to carve up the remaining space to any weighted views. Since there is no space to allocate, you get zero.
container_ll width 420
lineDivider_view - 420
reminder_img - 36
= -36
so this makes sense
reminderViews_ll width 0
There simply is no room to give it.
Why are you using a horizontal line divider in your horizontal view?
Ah, very confused: layout_width="fill_parent" and layout_weight="1.0" doesn't work?
I mean, layout_width="0dp" is guaranteed to be width 0, regardless of what you put into it, so that one will never do what you want. If you are using fill_parent and its still not working I'd question if you are adding your custom View into the right LinearLayout, because that really should work correctly.
I see that you've set android:layout_width for reminderViews_ll to 0 dp, you have to change this parameter, possibly dynamically if you want to. I don't really understand why you set it to 0 dp and then ask why it has zero width.

Categories

Resources