How to make EditText not editable but scrollable

EditText not Editable but Scrollable

When we need to show long text like Terms and conditions in EditText. We need to allow scrolling and it should not be editable.

Editable

For this I tried in XML set EditText attributes to show vertical scrollbars

android:scrollbars="vertical"

And then to make EditText not editable i tried different ways like

android:inputType="none"

not worked, and also tried

android:Editable=false

but it is deprecated.

And then tried this line in code

editTextView.setEnabled(false);

Its not allowed to Edit. But unable scroll text in EditText.

Solution:

And Finally i found the work around for it. Added this line in code.

editTextView.setKeyListener(null);

It worked perfectly….

Hope this helps somebody….

You may be also interested in

 

Show soft keyboard for EditText when Fragment starts

At times When app screen starts, we may need to show Soft keyboard Popup for the EditText, which having focus. So user don’t need to touch on the EditText to show keyboard.

show soft keyboard

To show keyboard for EditText view when Activity starts. We can just implement in XML design file in EditText tag by using <requestFocus />. see below

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="15">
<requestFocus/>
</EditText>

But the above XML code not works when using in Fragment, to show keyboard when Fragment screen starts.

Solution to show soft keyboard

We need to handle in code to force show keyboard. Following code works fine..

/**
* TODO showInputMethod
*/
public void showInputMethod() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}

Call this method in Fragment OnCreateView(…). Now It will show the softkeyboard.

And You can hide the Softkeyboard, when user touched on any other place than the EditText in Activity screen, using following code (place this code in Activity)

  /* (non-Javadoc)
* @see android.app.Activity#dispatchTouchEvent(android.view.MotionEvent)
*/
  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
      View v = getCurrentFocus();
      boolean ret = super.dispatchTouchEvent(event);
      if (this instanceof SkylineMessagesActivityAdv) {
          return false;
      }
      if (v instanceof EditText) {
          View w = getCurrentFocus();
          int scrCoords[] = new int[2];
          w.getLocationOnScreen(scrCoords);
          float x = event.getRawX() + w.getLeft() - scrCoords[0];
          float y = event.getRawY() + w.getTop() - scrCoords[1];
          if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
              InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
          }
      }
      return ret;
  }

Hope it helps somebody..

Cheers 🙂

You may be also interested in

 

Set maxLength of EditText dynamically

Set maxLength of EditText dynamically

At times you may want to set maxlength of EditText view dynamically.

maxLength

You’ve to use setFilters method of EditText, as shown below.

EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);

Hope it helps somebody…
Cheers 🙂

You may be also interested in

Validation using default feature of EditText

Default Validation EditText

I found a good article here on how to use the default feature of EditText to show the error. While writing the validation on EditText on a button click event or TextChanged event instead of showing alert dialog if you want to show error we can use the default feature of EditText as

EditText username = (EditText)findViewById(R.id.uname);
if( username.getText().toString().length() == 0 )
    username.setError( "Empty Text Field");

sample image

Validation

Hope it helps somebody 🙂

You may be also interested in

Validate a URL / Website name in EditText in Android?

Validate a URL / Website

To validate URL/ Website name entered in a EditText, this following works fine…

private boolean isValidUrl(String url) {
      Pattern p = Patterns.WEB_URL;
      Matcher m = p.matcher(url.toLowerCase());
      if(m.matches())
         return true;
      else
        return false;
}

URL should be in lowercase to validate correctly. Here is ref link.

Hope this helps somebody…

Cheers…

Bottom layout below ScrollView Or ListView moving up along with Soft keyboard

Hi Guys,

When you have a layout at bottom of the screen, below a ScrollView/ListView. If you have EditText to edit data,  when Softkeyboard pops up, the bottom layout moves up along with SoftKeyboard. And at times, it’s not require the bottom layout to moves up with Soft Keyboard.

bottom layout

To handle this, you just need to set this attribute to ScrollView Or ListView, keeps the layout at the bottom

android:isScrollContainer=”false”

Solves the issue.

Cheers. 🙂