1

I have a situation where I need a scrollview to have a background image that shouldn't scroll along with it's parent when moving. Before any of you suggest me the links for setting background image and this that, I have already tried and it's not working.

The whole story goes like: I have an activity with fragments which have their own backgrounds with some input fields. When focusing over input fields, keyboard appears and background image squeezes. For that I put an image on background of scrollview that fixed my issue of squeezing background but raised another concern that background Image should stay static while scrolling the parent scrollview.

The second solution any of you may suggest is setting background of my activity rather playing with scrollview. That's right, but for that I had to make a style element with background of theme which appears odd while transitioning different fragments plus it adds overhead when I have a lot of code and fragments to move forward and back.

That's the point where I am stuck. I have gone through links below, if you just need to know that I tried it or not.

link1 link2 link3 ... and so on

below is the layout I am using for my fragments (it's all being done programmatically)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/backgroundView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:scaleType="fitXY" />

        <LinearLayout
            android:id="@+id/parent_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </FrameLayout>
</ScrollView>
</LinearLayout>

So if you guys have any better solution keeping in mind the situation I have, will be warmly welcomed. Thank you

Community
  • 1
  • 1
Saqib
  • 1,120
  • 5
  • 22
  • 40

3 Answers3

3

None of the above works for me with some reason but this things works.

getWindow().setBackgroundDrawableResource(R.mipmap.img_reg_bg);

put it in oncreate method hope it will help.

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
2

If I guess right you try to fix your hotfix to get it working propperly. If I'm right you sould go back to scratch and do it without your scrollview.

If you have a look at following Link: http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

you'll see softkeyboard mode can be adjusted. "adjustPan" should solve your problem.

"adjustPan"

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

LuXoR
  • 53
  • 6
  • 1
    I really like this answer! It may not be suitable in some situations, but is in many others. I didnt know this existed. it will come in useful :) – IAmGroot Feb 08 '16 at 08:22
0

Add someimgage to your drawable folderes, like drawable-mdpi, drawable-hdpi. In your LinearLayout "@+id/top_layout" add attribute:

android:background="@drawable/someimage"
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • and where m I supposed to add that background? I think I described that any background image to parentlayout squeezes it when keyboard is shown! – Saqib Feb 08 '16 at 08:07
  • You have missed end tag. This background is fixed, how it has been squeezed? – Stanojkovic Feb 08 '16 at 08:09
  • that was just a typo! – Saqib Feb 08 '16 at 08:12
  • Add `android:layout_height="wrap_content"` to your `ScrollView` and try to use `LinearLayout` instead of `FrameLayout`. – Stanojkovic Feb 08 '16 at 08:17
  • And the end, try to add this to your `` tag in your manifest file `android:windowSoftInputMode="adjustPan|stateVisible"` – Stanojkovic Feb 08 '16 at 08:22
  • I tried that and because of linearlayout I can't put it's background and framelayout is used for solely the reason that I need to have two views in scrollview as it doesn't allow more than one views directly! – Saqib Feb 08 '16 at 09:16