Reputation: 17018
I am looking for the optimal way to use a .png image as the background for my app's main activity screen. I want to be able to fit properly on as many devices as possible.
What I have works, what I want to know is what best practices are for portability.
Here is the code I am using in my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_background"
android:padding="25dp"
>
...other stuff like a few buttons that go on top of background
Currently the file "main_background.png" is a 800px by 600px image. I notice on my phone at least the horizontal dimension gets shrunken inwards distorting the image as such. Is there an ideal Width::Height ratio I could make my background image?
Thank you.
Upvotes: 3
Views: 8877
Reputation: 6105
What you would do is set up your xml like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/main_background"
android:padding="25dp"
>
notice the lack of layout_height
...
In your main Activity, you would find the view, and execute the following line:
myLinLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, myLinLayout.getWidth()));
where myLinLayout
is the linear layout, and you follow the LayoutParams
method:
setLayoutParams(int width, int height);
Upvotes: 3