The rectangle is the entire screen. The dotted line represents the center of the height and width. Each red line is an imageview and the green dot is the center of the imageview. Each imageview is equidistant from the neighbouring imageview and the screen(heightwise)
I am designing main menu for my small game app that should work in all screens as shown in the figure. I browsed through many tutorials and came up with the following code but i am not getting the expected result. In one emulator, The last imageview is not visible and the imagviews are not centered. In my samsung galaxy ace, only the first imageview is visible. Is anything wrong with the code.?
package com.example.layout_test2;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
int screenWidth,width_centre;
int screenHeight,first_icon_position,second_icon_position,third_icon_position,fourth_icon_position;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout background= new LinearLayout(this);
background.setOrientation(LinearLayout.VERTICAL);
background.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
ImageView icon_1 = new ImageView(this);
ImageView icon_2 = new ImageView(this);
ImageView icon_3 = new ImageView(this);
ImageView icon_4 = new ImageView(this);
icon_1.setScaleType(ImageView.ScaleType.MATRIX);
icon_1.setImageResource(R.drawable.start);
//icon_1.setOnClickListener(this);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start, o);
int w1 = bmp.getWidth();
int h1 = bmp.getHeight();
icon_2.setScaleType(ImageView.ScaleType.MATRIX);
icon_2.setImageResource(R.drawable.settings);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.settings, o);
int w2 = bmp.getWidth();
int h2 = bmp.getHeight();
icon_3.setScaleType(ImageView.ScaleType.MATRIX);
icon_3.setImageResource(R.drawable.more_games);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.more_games, o);
int w3 = bmp.getWidth();
int h3 = bmp.getHeight();
icon_4.setScaleType(ImageView.ScaleType.MATRIX);
icon_4.setImageResource(R.drawable.exit);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.exit, o);
int w4 = bmp.getWidth();
int h4 = bmp.getHeight();
if (Build.VERSION.SDK_INT >= 17) {
Point size = new Point();
try {
this.getWindowManager().getDefaultDisplay().getRealSize(size);
screenWidth = size.x;
screenHeight = size.y;
System.out.println(String.valueOf(screenWidth) );
System.out.println(String.valueOf(screenHeight));
} catch (NoSuchMethodError e) {
Log.i("error", "it can't work");
}
} else {
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
System.out.println(String.valueOf(screenWidth) );
System.out.println(String.valueOf(screenHeight));
}
int width_centre1 = (screenWidth-w1)/2;
int width_centre2 = (screenWidth-w2)/2;
int width_centre3 = (screenWidth-w3)/2;
int width_centre4 = (screenWidth-w4)/2;
first_icon_position = (int) (screenHeight-h1)/8;
second_icon_position = (int) 3*(screenHeight-h2)/8;
third_icon_position = (int) 5*screenHeight/8;
fourth_icon_position = (int) 7*screenHeight/8;
System.out.println(screenHeight);
System.out.println(first_icon_position);
System.out.println(second_icon_position);
System.out.println(third_icon_position);
System.out.println(fourth_icon_position);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams1.setMargins(width_centre1,first_icon_position, 0, 0);
background.addView(icon_1,layoutParams1);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams2.setMargins(width_centre2,second_icon_position, 0, 0);
background.addView(icon_2,layoutParams2);
LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams3.setMargins(width_centre3,300, 0, 0);
background.addView(icon_3,layoutParams3);
LinearLayout.LayoutParams layoutParams4 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams4.setMargins(width_centre4,180, 0, 0);
background.addView(icon_4,layoutParams4);
//make visible to program
setContentView(background);
}
}