Hot questions for Using Android EditText in android linearlayout

Top Java Programmings / Android EditText / android linearlayout

Question:

I have a simple EditText, but when I expand that it gets cut. Maybe my Layout composition is the cause, someone know what the problem is?

My xml file below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="@dimen/surround_paddings"
    android:layout_margin="@dimen/surround_margins">

    <ImageView
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back_arrow"
        android:layout_gravity="left"
        android:layout_weight="0.5"
        android:padding="10dp"/>

    <TextView
        android:id="@+id/serverRoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Server 0 - Sala 0"
        android:textSize="@dimen/subtitle"
        android:fontFamily="@font/baloo"
        android:layout_marginLeft="@dimen/surround_margins"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="9"
        android:lineSpacingExtra="@dimen/text_spacing"/>

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_marginTop="-5dp"
    android:background="@color/colorAccent"/>

<TextView
    android:id="@+id/subject"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/subtitle"
    android:fontFamily="@font/baloo"
    android:text="@string/subject"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="@color/colorPrimaryDark"
    android:lineSpacingExtra="@dimen/text_spacing"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/rounded_grey">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:background="@drawable/rounded_grey"
        android:baselineAligned="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="2">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvMessages"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="@dimen/surround_margins"
                android:background="@drawable/rounded_primary_double"
                android:padding="10dp"
                android:layout_weight="1"
                android:divider="@color/colorAccent"
                android:dividerHeight="1.3dp"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="9"
                android:padding="@dimen/surround_paddings"
                android:background="@drawable/rounded_white">

                <LinearLayout
                    android:id="@+id/commentIcon"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@drawable/rounded_primary_dark_circle"
                    android:src="@drawable/pencil_icon"
                    android:elevation="@dimen/elevation1"
                    android:orientation="vertical"
                    android:layout_gravity="center"
                    android:layout_margin="@dimen/surround_margins"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/questionsNumber"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:fontFamily="@font/baloo"
                        android:textColor="@color/colorAccent"
                        android:textSize="@dimen/subtitle"
                        android:layout_gravity="center"
                        android:gravity="center"/>
                </LinearLayout>

                <EditText
                    android:layout_weight="7"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:background="@drawable/rounded_white"
                    android:hint="@string/wait_your_time"
                    android:fontFamily="@font/baloo"
                    android:layout_marginLeft="@dimen/surround_margins"
                    android:autofillHints="@string/wait_your_time"
                    android:inputType="text" />

                <LinearLayout
                    android:id="@+id/sendIcon"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@drawable/rounded_accent_circle"
                    android:src="@drawable/pencil_icon"
                    android:elevation="@dimen/elevation1"
                    android:orientation="vertical"
                    android:layout_gravity="center"
                    android:padding="@dimen/surround_paddings"
                    android:layout_margin="@dimen/surround_margins"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:src="@mipmap/send_icon"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@drawable/rounded_grey"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="8">

                    <ImageView
                        android:id="@+id/nextIcon"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:src="@mipmap/next_icon"
                        android:layout_gravity="center"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/jump_time"
                        android:textSize="@dimen/small_text"
                        android:lineSpacingExtra="@dimen/text_spacing"
                        android:fontFamily="@font/baloo"
                        android:gravity="center"
                        android:layout_marginTop="@dimen/negative"
                        android:layout_gravity="center"
                        android:textColor="@color/black"/>
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginTop="-5dp"
                        android:background="@color/colorAccent"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/whose_time"
                        android:textSize="@dimen/small_text"
                        android:lineSpacingExtra="@dimen/text_spacing"
                        android:fontFamily="@font/baloo"
                        android:gravity="center"
                        android:layout_marginTop="@dimen/negative"
                        android:layout_gravity="center"
                        android:textColor="@color/black"/>
                </LinearLayout>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rvLine"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="@dimen/surround_margins"
                    android:background="@drawable/rounded_grey"
                    android:padding="@dimen/surround_paddings"
                    android:layout_gravity="center"
                    android:layout_weight="2"
                    android:divider="@color/colorAccent"
                    android:dividerHeight="1.3dp"/>

            </LinearLayout>

            <LinearLayout
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="9"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="@color/colorAccent"/>

                <LinearLayout
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@drawable/rounded_accent_circle"
                    android:src="@drawable/pencil_icon"
                    android:elevation="@dimen/elevation1"
                    android:orientation="vertical"
                    android:layout_gravity="center"
                    android:layout_margin="@dimen/surround_margins"
                    android:gravity="center">

                    <TextView
                        android:id="@+id/timer"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:fontFamily="@font/baloo"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="@dimen/subtitle"
                        android:layout_gravity="center"
                        android:gravity="center"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Here the image of what happens when I try to type something: keyboard not expanded

Here's the problem

Idk if this lots of Layouts may be causing it. I thought I should just take enough room to show the whole editText


Answer:

In order to prevent this you can add this to your manifest :

<activity
 android:windowSoftInputMode="adjustPan">
</activity>

If this will not work for you you can use this directly inside the xml :

android:windowSoftInputMode="adjustNothing|adjustResize"

Sometime this will not solve the problem as well and you will have to add this attribute dynamically:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Question:

I have added dynamically Linear Layout which have edit field, now i want get the text from those edit fields. Here is my code for adding layout dynamically

linearLayoutForm = (LinearLayout) findViewById(R.id.linearLayoutForm);
        btnAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final LinearLayout newView = (LinearLayout)getLayoutInflater().inflate(R.layout.single_skill_row, null);
                newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
                btnRemove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        linearLayoutForm.removeView(newView);
                    }
                });
                linearLayoutForm.addView(newView,0);
            }
        });

I have tried to get text with this code but i got null pointer exception.

register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            LinearLayout et2=(LinearLayout) linearLayoutForm.getChildAt(linearLayoutForm.getChildCount());
            EditText e = (EditText) et2.getChildAt(et2.getChildCount());
            String s=e.getText().toString();
});

here is XML of single_skill_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowdetail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/editDescricao"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.62"
        android:ems="10"
        android:inputType="text"
        android:maxLines="1">
        <!--<requestFocus />-->
    </EditText>

    <ImageButton
        android:id="@+id/btnRemove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/btn_remove"
        android:src="@drawable/ic_remove_black_18dp" />
</LinearLayout>

Answer:

You can do it like this:

First of All in your class you add a field:

...
EditText myEditText;
...

Then after adding your linearLayout you store your editText in the field like this:

...
final LinearLayout newView = (LinearLayout)getLayoutInflater().inflate(R.layout.single_skill_row, null);
myEditText = newView.findViewById(R.id.editDescricao);
...

After that in the onClick callback of your button you can get it's text like this:

register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        String text = myEditText.getText().toString();
});

Question:

This is my aim:

I have a linearlayout where everything is centered both vertically and horizontally

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center">

<EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/top_edittext_form_bg"
        android:hint="Email or Username"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5pt"/>
<EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_edittext_form_bg"
        android:inputType="textPassword"
        android:hint="Password"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5pt"  />

...

I have been messing around trying to take out the layout_margin, but this affects all elements (even the ones underneath these two).

Is there any way for me to simply combine these two edittexts like in the picture without affecting my linearlayout?


Answer:

Try this:

...

<EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/top_edittext_form_bg"
        android:hint="Email or Username"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5pt"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        />
<EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_edittext_form_bg"
        android:inputType="textPassword"
        android:hint="Password"
        android:layout_gravity="center_horizontal" 
        android:layout_marginBottom="5pt"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
/>
...

Tell me if it works. :)