Why doesn't edittext change dp when the user starts typing in it?

149 Views Asked by At

I would have thought this is pretty simple but I've tried several things and it doesn't work. Basically, when activity starts I want my edit text to be invisible (I'm doing this by making size to 0dp, width and height).

Then, the idea is when user types on softkeyboard, the size changes, and user can see the editText and what is being typed.

Here's my code :

MainActivity.java

package com.example.chris.sunil_gupta;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//create a ListView object called listview
        ListView listview;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.ListView_2);

        final EditText editText = (EditText) findViewById(R.id.editText);
        editText.requestFocus();

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {


                if (editText.length() > 0) {

                    //Your query to fetch Data
//            editText.getLayoutParams().width=32;
//            editText.getLayoutParams().height=50;
                    editText.setWidth(32);
                    editText.setHeight(50);
                }
            }
        });

    }

}

Layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context=".MainActivity">

    <ListView android:id="@+id/ListView_2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/black"
        android:layout_weight="1">
    </ListView>

    <EditText
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/editText"
        android:background="@null"
        android:inputType="phone">
    <!--android:visibility="invisible"-->
    </EditText>


</LinearLayout>
4

There are 4 best solutions below

1
On BEST ANSWER

Sorry,change to this:

float density=getResource().getDisplayMetrics().density;
editText.getLayoutParams().width =(int)(32*density);
editText.getLayoutParams().height =(int)(50*density);
editText.requestLayout();
4
On
try it
 if (s.length() > 0) {

                    //Your query to fetch Data
//            editText.getLayoutParams().width=32;
//            editText.getLayoutParams().height=50;
                    editText.setWidth(32);
                    editText.setHeight(50);
                }
            }
instead of
 if (editText.length() > 0) {

                    //Your query to fetch Data
//            editText.getLayoutParams().width=32;
//            editText.getLayoutParams().height=50;
                    editText.setWidth(32);
                    editText.setHeight(50);
                }
            }
1
On

Try the following:

editText.getLayoutParams().width = 32;
editText.getLayoutParams().height = 50;
editText.requestLayout();

However, these values are in pixels. So, you may need to perform some conversion from dp to px before setting the values.

0
On

try this. set minWidth and setHeight and set width and height to wrap_content

  <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:id="@+id/editText"
    android:background="@null"
    android:inputType="phone">