1

(image attached in the bottom of the question)

I've this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLayout">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:paddingTop="10dp"
        android:layout_height="match_parent"
        android:id="@+id/viewsLayout"
        android:layout_weight="1.0">
    </LinearLayout>

</LinearLayout>

I would like to add 3x3 image views of balls to the internal linear layout 'viewsLayout' so in its appropriate class I'll do so:

    LinearLayout main = (LinearLayout) findViewById(R.id.viewsLayout);
    main.setWeightSum(3.0f);

    for (int i = 0; i < 3; ++i) {
        LinearLayout currentRow = new LinearLayout(this);
        currentRow.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        params.weight = 1.0f;
        currentRow.setLayoutParams(params);

        for (int j = 0; j < 3; ++j) {
            ImageView viewOne = new ImageView(this);
            viewOne.setBackgroundResource(R.drawable.ball);
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
            viewOne.setLayoutParams(params);
            viewOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
            viewOne.setAdjustViewBounds(true);
            currentRow.addView(viewOne);
        }
        main.addView(currentRow);
    }

The problem is that in a landscape mode the balls become ellipse (are too stretched) as you can see in the attached picture.

I saw that this question was already asked and it wasn't advised to avoid a landscape mode. (I'm adding a picture to make it easy to find for searchers.) I saw that the solution is to create a new layout for a landscape mode but I have lots of layouts so creating another layout for each of them for a landscape mode may be horrible. Is there a better solution? if not, will I have to set the width of the linearLayout\ImageView by dp and not MATCH_PARENT?

if the solution is short and easier to write it, please show me.enter image description here

Maor Cohen
  • 936
  • 2
  • 18
  • 33
  • 1
    You're setting the background of the ImageView and not the resource, so your scaling/adjustViewbounds won't affect it. Try `viewOne.setImageResource(R.drawable.ball);` instead. – Cruceo Nov 22 '16 at 23:47
  • Actually, that's still not going to be entirely what you want. If you want 3x3 to fit the center like that, you're going to have to either use different inflation code for landscape in order to keep them weighted properly – Cruceo Nov 22 '16 at 23:53
  • wow it looks great after setting the image resource! but why do you think it's not what I wanted? maybe my explanation wasn't good or it may have a behavior that I don't expect now? it looks even perfect now! :) – Maor Cohen Nov 23 '16 at 00:08
  • How can I set your comment as the answer for my question? – Maor Cohen Nov 23 '16 at 00:12

2 Answers2

1

You are using R.drawable.ball. Is it png file? Maybe you should try 9 patch file and avoid to stretch file in editor for 9 patch file format.

If it's appropriate in your case, you can draw a ball via drawable shapes

Nicolas
  • 74
  • 11
  • yes, it's a png file. however, the image is still stretched although I'm using a 9 patch image. Maybe it's because of the match_parent width.. – Maor Cohen Nov 23 '16 at 00:04
  • Try to specify in 9 patch image file HOW it should be stretched, as it's described here [link](https://developer.android.com/studio/write/draw9patch.html) – Nicolas Nov 23 '16 at 00:52
  • The similar issue is described here [link] http://stackoverflow.com/a/24505700/7092030 – Nicolas Nov 23 '16 at 00:53
1

First of all your image size may not be wide enough to fit in a landscape mode. For this you'll need a separate image to fit in landscape mode.

Secondly you may try creating a landscape layout which will be triggered on landscape mode.Create a layout-land folder and create a seperate xml for it.

Refer these links, https://developer.android.com/training/multiscreen/screensizes.html

https://developer.android.com/training/multiscreen/screendensities.html