Reputation: 4480
how's to solve the problem with resolution if I create layout from code?
I tested it on my phone which has 320 x 480 resolution and it's looks ok. but when I test it in my friends phone (Galaxy S) looks like the layout didn't stretch out.
Why I create the layout from code is because I don't know how to place buttons in specific location.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rLayout = new RelativeLayout(this);//(RelativeLayout)findViewById(R.id.relativeLayout1);
rLayout.setBackgroundResource(R.drawable.menu_blankscreen);
InitializeLayout();
setContentView(rLayout);
}
private void InitializeLayout() {
ImageButton newGame = new ImageButton(this);
//newGame.setBackgroundResource(drawable.menu_button_new);
newGame.setBackgroundResource(R.drawable.menu_button_new_xml);
newGame.setOnClickListener(newGameClick);
addView(newGame, 5, 174, 149, 41);
ImageView continueGame = new ImageView(this);
continueGame.setBackgroundResource(R.drawable.menu_button_continue_xml);
continueGame.setOnClickListener(continueClick);
addView(continueGame, 5, 218, 149, 41);
ImageView highscore = new ImageView(this);
highscore.setBackgroundResource(R.drawable.menu_button_highscore_xml);
highscore.setOnClickListener(highscoreClick);
addView(highscore, 5, 262, 149, 41);
ImageView achievement = new ImageView(this);
achievement.setBackgroundResource(R.drawable.menu_button_achievement_xml);
achievement.setOnClickListener(achievementClick);
addView(achievement, 5, 306, 149, 41);
ImageView option = new ImageView(this);
option.setBackgroundResource(R.drawable.menu_button_option_xml);
option.setOnClickListener(optionClick);
addView(option, 5, 350, 149, 41);
ImageView quit = new ImageView(this);
quit.setBackgroundResource(R.drawable.menu_button_quit_xml);
quit.setOnClickListener(quitClick);
addView(quit, 5, 395, 149, 41);
Button btnTest = new Button(this);
btnTest.setText("Test");
btnTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(StartMenuActivity.this, TestActivity.class);
startActivity(intent);
}
});
addView(btnTest, 270, 430,50, 50);
}
public void addView(View view, int x, int y, int width, int height) {
rParam = new RelativeLayout.LayoutParams(width, height);
rParam.leftMargin = x;
rParam.topMargin = y;
rLayout.addView(view, rParam);
}
I assume that leftMargin as x, and topMargin as y. is it pixel dependant??
Upvotes: 0
Views: 742
Reputation: 484
You are trying to place UI components "absolutely". The AbsoluteLayout has been depreciated and your problem is precisely the reason. You can achieve "perfect" placement of UI components for your dev device. When its run on another device with a different pixel density or resolution or... the list goes on, you will have problems.
To achieve perfect, "absolute" placement on every device is neither practical nor possible. You can get your components really, really close to pixel perfect on most devices using the various layouts and iterating different values in the properties. Read here for more information.
It takes some effort to grok the paradigm, but it's actually really easy compared to swing, at least for me it is.
This is how I think for a portrait view: We need a LL to flow from top to bottom, every component placed inside will either be above or below the next. Then we need another LL (nested) that flows left to right. Every component will be placed to the side of each component. You then specify gravity, margin, padding and weight, based on where you want the component to be placed "relative" to pixel screen density and resolution size. Here is a little example to show you what it looks like. Hope it helps!
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/linearLayout2"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button android:id="@+id/myButton"
style="@style/actionButton"
android:onClick="onClickFeature"
android:text="@string/title_button"
android:drawableTop="@drawable/pic_btn"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"/>
Upvotes: 1