Reputation: 10609
I created a background image which fits exactly into the device display without status bar and actionbar. That means the whole height of my device is 1280 pixels. Without statusbar and actionbar it is 1038 pixels. My background image is exactly 1038 pixels high. When i set the image as the background of my layout:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackground(Helper.loadDrawableFromFile(this.getResources(), Helper.getBackgroundLoginImagePath()));
....
}
It fits perfectly into the screen as i want it to. the problem is that i have a scroll view and when i want to type something in my edittexts the keyboard pops up and my background image gets clinched.
So i changed my code to set the background image to the window in my activities onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_login);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
this.getWindow().setBackground(Helper.loadDrawableFromFile(this.getResources(), Helper.getBackgroundLoginImagePath()));
...
}
But for some reason the background image now gets stretched so that the height is bigger than the original and i cant see the whole image. (its about 60 pixels to high).
What is happening there? Why is 'setBackgroundDrawable' stretching my image?
EDIT
When i do the following in a dialog fragment, which in my opinion is exactly the same as doing it in an activity, works perfectly:
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
dialog.getWindow().setBackgroundDrawable(Helper.loadDrawableFromFile(this.getResources(), Helper.getBackgroundLoginImagePath()));
...
}
so why isnt it working for an activity?
Upvotes: 1
Views: 3224
Reputation: 32261
If you do not want your view to be pushed by the keyboard add
android:isScrollContainer="false"
You can use ImageView instead of view. setBackground()
See this related question.
Upvotes: 2
Reputation: 912
You have to scale the image if you don't want to be stretched. To do that it would be better for me to use the following into the R.layout.activity_login:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/yourdrawable"
android:scaleType="centerInside">
</ImageView>
</FrameLayout>
The CENTER_INSIDE will "scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding)". For more info regarding the scaling types you can have a look here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Finally, it's worth mentioning that the setBackgroundDrawable was deprecated in API level 16. And the documentation propose to use setBackground(Drawable) instead.
Upvotes: 2