(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.