5

I want EditText to temporarily not gain foucs.

Scenario is : I have two EditTexts. Whenever any EditText's text is changed, I want another EditText to not to respond to user clicks until another predefined event occurs.

To achieve that I tried this :

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "onResume");
    registerBroadcastReceivers();
}

// Register BroadcastReceivers
private void registerBroadcastReceivers() {
    Log.d(TAG, "Registering broadcast receivers.");

    // Google response receiver
    googleResponseReceiver = new GoogleAPIResponseReceiver();
    IntentFilter googleResponseIntentFilter = new IntentFilter(
            RS_GoogleApiIntentService.ACTION_RECEIVED_GOOGLE_RESPONSE);
    googleResponseIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    LocalBroadcastManager.getInstance(this).registerReceiver(
            googleResponseReceiver, googleResponseIntentFilter);
}

editText1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        // Set EditText non-focusable
        MyActivity.this.editText2.setFocusableInTouchMode(false);
    }
});

.
.
. 
// Somewhere in one function I call below function to start an IntentService.
.
.
. 

// Call Google API with given URL
private void CallGoogleApiWithUrl(String url, int requestId) {

    Intent intent = new Intent(this, RS_GoogleApiIntentService.class);
    intent.putExtra(RS_GoogleApiIntentService.EXTRA_URL_STRING, url);
    intent.putExtra(RS_GoogleApiIntentService.EXTRA_GOOGLE_API_REQUEST_ID,
            requestId);
    startService(intent);
}

// Broadcast receiver for Google API response
public class GoogleAPIResponseReceiver extends BroadcastReceiver {

    // Enabling EditText works up to this point.

    @Override
    public void onReceive(Context context, Intent intent) {

        // Stops working now onwards.

    }
}

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        // Set EditText focusable
        MyActivity.this.editText2.setFocusableInTouchMode(true);
}

Layout :

<LinearLayout
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/from_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:ems="10"
        android:hint="@string/from_location_hint"
        android:imeOptions="actionDone"
        android:imeActionLabel="Done"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/use_current_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:onClick="useCurrentLocation"
        android:text="@string/use_current"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:contentDescription="@string/swap_button_description"
    android:onClick="swapLocations"
    android:scaleType="fitStart"
    android:src="@drawable/swap" />

<EditText
    android:id="@+id/to_location"
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:gravity="left"
    android:hint="@string/to_location_hint"
    android:imeOptions="actionDone"
    android:imeActionLabel="Done"
    android:inputType="text" >
</EditText>

<LinearLayout
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/departuretime_date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clickable="true"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:hint="@string/departure_date_hint"
        android:inputType="datetime" >
    </EditText>

    <EditText
        android:id="@+id/departuretime_time"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clickable="true"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:hint="@string/departure_time_hint"
        android:inputType="datetime" >
    </EditText>

</LinearLayout>

<Button
    android:id="@+id/pick_match_create_trip"
    style="@style/big_centered_button_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="80dp"
    android:onClick="pickMatchesButtonClicked"
    android:text="@string/pick_your_matches" />

<TextView
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="75dp"
    android:text="@string/current_location_textlabel"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/current_location_text"
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginTop="5dp"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" />

<ListView
    android:id="@+id/places_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:cacheColorHint="#00FFFF"
    android:background="@color/white" >
</ListView>

EditText never gains focus again. Disabling focus works but enabling it again does not. I also tried using editText2.setEnabled(); method. It did not work, too.

Geek
  • 8,280
  • 17
  • 73
  • 137
  • Please show declaration of editText1 and editText2 ... – TN888 Aug 02 '13 at 11:50
  • you dont want the focus or you dont want the edittext to be visible – krishna Aug 02 '13 at 11:54
  • Just pointing out that calling `setFocusableInTouchMode(false)` after `setFocusable(false)` is redundant. Also, `setFocusable(true)` is not required if you are calling `setFocusableInTouchMode(true)`. – Vikram Aug 02 '13 at 11:55
  • are u able to do focusable false onEditTextChanged.if so then i think u can try focusable true by using handler with onPostDelayed method with a delay of 2000ms.i have used this on some similar situation – Manmohan Badaya Aug 02 '13 at 12:42

6 Answers6

8

Its really Simple my friend. You can try something like below:

   editText1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        // Set EditText non-focusable
        editText2.setEnable(false);
        editText2.setClickable(false);


    }
});

.
.  // some work
.

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        // Set EditText focusable
                editText2.setEnable(true);
        editText2.setClickable(true);
}

Just update your code something like above and try. that will surly works. if not then let me know, i will help you.

Enjoy Coding... :)

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • I tried it. Problem is once I disable it, it is not getting enabled again. – Geek Aug 09 '13 at 10:54
  • Then maybe the problem in somewhere else in your cod, do you wand to show us more of it, like the XML file? – felixd Aug 10 '13 at 16:54
  • @felixd Added xml file. The thing is if I put code of enabling EditText in some methods it works. To make it clear, first I make EditText disabled then call an IntentService and return results back to activity. To listen to IntentService's result I have broadcast receiver in my activity. So if I put code to enable EditText in broadcast receiver's `onReceive` or in any other method after broadcast receiver in app flow, this does not work. – Geek Aug 11 '13 at 08:10
  • @Akash sound like a bad wiring problem or a code block that is never reached ... please add your entire block of code including the intent request and broadcast receiver because it seems that your problem lies there. – Mortalus Aug 11 '13 at 10:39
2
Timer timer = new Timer();
timer.schedule(new TimerTask() {

  @Override
  public void run() {
    editText2.setEnable(false);
    editText2.setClickable(false);
  }
}, 20);
Ivan
  • 10,052
  • 12
  • 47
  • 78
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
1

try this..

editText2.clearFocus();
mdDroid
  • 3,135
  • 2
  • 22
  • 34
1

You set editText2.requestFocus(); when ever you want to focus on EditText.

Ved
  • 1,035
  • 14
  • 28
1

write it in manifest file of activity, It defocus the edittext by creation activity at first time. and re gain the focus by touching on Edit text

android:windowSoftInputMode="adjustPan|stateHidden"
Gangadhar Nimballi
  • 1,534
  • 3
  • 18
  • 46
1

you may use the following code and manipulate accordingly by java.

`<EditText ...
    android:clickable="false" 
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false">
 </EditText>'
UMESH0492
  • 1,701
  • 18
  • 31
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Saullo G. P. Castro Aug 10 '13 at 17:01