Android: Background image resize on keyboard pop up

后端 未结 12 930
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 20:01

I am developing an application in which the background image get shrink on keyboard pop-up. My .xml is as follows :



        
12条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-03 21:01

    Ok if I got your question right, you want a layout with a background and a scrollview. And when the softkeyboard pops up, you want to resize the scrollview, but keep the background at full size right? if that's what you want than I may have found a workaround for this issue:

    What you can do is make 2 activities.

    Activity 1:

    public class StartActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent StartApp = new Intent(this, DialogActivity.class);
        startActivity(StartApp);         // Launch your official (dialog) Activity
    
        setContentView(R.layout.start);  // your layout with only the background 
        }
    }
    

    Activity2:

    public class DialogActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);  // get rid of dimming
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  //get rid of title bar (if you want)
    
        setContentView(R.layout.dialog);  //Dialog Layout with scrollview and stuff
    
        Drawable d = new ColorDrawable(Color.BLACK);  //make dialog transparent
        d.setAlpha(0);
        getWindow().setBackgroundDrawable(d);
        }
    }
    

    start layout:

      //your background
    
    

    dialog layout:

    
    
        
    
                
    
                    
    
                
         
    
    

    AndroidManifest.xml

    start Activity:

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    

    dialog Activity

    android:theme="@android:style/Theme.Dialog"
    android:windowSoftInputMode="adjustResize"
    android:excludeFromRecents="true"
    

    Edit: To finish Activities at once use the following somewhere inside your DialogActivity (example: override the backbutton):

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(getApplicationContext(), StartActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
    }
    

    and in onCreate() of StartActivity:

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }
    else {
        Intent StartApp = new Intent(this, TestActivity.class);
        startActivity(StartApp);
    }
    

    Screenshots!

    Normal Normal

    Clicked the EditText box Clicked textbox

    After Scrolled down (ps: i put textbox inside scrollview thats why its gone ;) ) after scrolled down

    I hope this will help your out ;)

提交回复
热议问题