user1871869
user1871869

Reputation: 3367

Make TextView not move when keyboard is on screen

I have a TextView that keeps moving every time I tap on my EditText and open my soft input keyboard. The EditText is located at the bottom of the page. I want it so that the TextView that I have doesn't move when the keyboard appears. In my case, the TextView is called No comments to display. Here is an example of what happens when I click on my EditText

enter image description here

Originally, the TextView is contained within the lighter grey area below but when I click on the EditText, it is pushed up. Here is my code:

public class Comments extends AppCompatActivity {
    private TextView noCommentsView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comments);
        EditText comment = (EditText) findViewById(R.id.write_comment);
        TextView noCommentsView = (TextView) findViewById(R.id.no_comments_text);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        noCommentsView.setVisibility(View.VISIBLE);         
    }
}

Now I know that adjustResize simply pushes everything up which is why the TextView gets pushed up so high. However, I want it so that the TextView stays put where it is. I have tried to use getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING); and this accomplishes my goal but the EditText does not move with the keyboard because the entire layout is not adjusted. Is there any way around this? Is there any way to move up the EditText but not have the TextView move up? Thanks!

Upvotes: 0

Views: 2668

Answers (5)

vibhor_shri
vibhor_shri

Reputation: 374

In the android Manifest, add the line:

android:windowSoftInputMode="stateHidden|adjustPan". adjustPan says:

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.

Source: http://developer.android.com/guide/topics/manifest/activity-element.html

Upvotes: 0

Parveen Prajapati
Parveen Prajapati

Reputation: 83

android:windowSoftInputMode="adjustPan|stateHidden"

Upvotes: 0

Bhoomi Zalavadiya
Bhoomi Zalavadiya

Reputation: 689

put your getWindow() methods above setContentView(). Or Else remove getWindow() methos from code and add android:windowSoftInputMode="adjustResize|stateHidden" in manifest file.

Upvotes: 4

Francisco Melicias
Francisco Melicias

Reputation: 321

on AndroidManifest.xml

add this: android:windowSoftInputMode="stateVisible|adjustResize"

Did this work for you? good luck with the project :)

Upvotes: 1

snachmsm
snachmsm

Reputation: 19223

checkout THIS post on Android Dev blog. most important for you might be line

android:windowSoftInputMode="adjustPan"

about 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.

more about other options/flags HERE

Upvotes: 0

Related Questions