nummer92
nummer92

Reputation: 31

Add textview right above Keyboard

At the moment my app consists from a EditText and a button, both at the bottom of the screen. If I'm typing something into the EditText and than hit that button a TextView gets created at the top of the screen.

But, what I want to achieve is that if I hit that button, that the TextView gets created right above the EditText field. And if I do that again I want the second TextView again created right above the EditText but below the other TextView.

This is my code so far.

Here's the xml: (This is all inside a RelativeLayout. But for some reason I can't put that in here because its not detected as code...)

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


<Button
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:text="Senden"
    android:layout_gravity="center_horizontal"
    android:layout_alignBottom="@+id/editText"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="470px"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

And my Activity:

public class MainActivity extends AppCompatActivity {

private Button button;
private EditText editText;
private LinearLayout linearLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);

    final View.OnClickListener btnHandler = new View.OnClickListener() {
        public void onClick(View v) {
            String msg = editText.getText().toString();
            editText.getText().clear();
            linearLayout.addView(createNewTextView(msg));
            View view = getCurrentFocus();
            if(view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    };

    button.setOnClickListener(btnHandler);
}

private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setTextColor(Color.BLACK);
    textView.setText(text);

    return textView;
}

What do I need to change so that it appears like i want it to?

Upvotes: 0

Views: 833

Answers (2)

Jin35
Jin35

Reputation: 8612

There is nice control for a lot of views: RecyclerView. Use if for handling your text views, and use LinearLayoutManager.setReverseLayout(true) for placing views in the bottom of container

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

Use LinearLayout as parent view with android:gravity="center|bottom" this property.

Now while adding textviews, insert newly created text view with position.

parentLinearLayout.addView(childtextview, index); //index represents it position in linearlayout,according to you index is always 2.

Upvotes: 1

Related Questions