need help in clearing the text in EditText view

Put your problem here if it does not fit any of the other categories.

need help in clearing the text in EditText view

Postby haden » Wed Oct 29, 2008 10:12 am

Hi,
I have a EditText with some default text in it. When a user clicks on it i want the text to disappear

this is what ive done

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                     fnameField = (EditText)findViewById(R.id.fname);
  3.  
  4.                        fnameField.setOnClickListener(this);
  5.  
  6.                    public void onClick(View view) {
  7.  
  8.      
  9.  
  10.                  fnameField.setText("");
  11.  
  12. }
  13.  
  14.  
  15.  
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


But i need to click on it twice to clear the field. I need everything to be cleared on a single tap on the EditText field

Can someone help me out here..
haden
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Thu Sep 11, 2008 11:51 am

Top

Postby Danuubz » Wed Oct 29, 2008 2:42 pm

Hello,

If your approach doesn't work, you can make an inner class that extends EditText and implements the android.view.GestureDetector.OnGestureListener interface.
There, you can overwrite the public boolean onSingleTapUp(MotionEvent e) or public boolean onDown(MotionEvent e) method.

Then, instead of EditText, you add your new inner class to your layout.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.android.screentest;
  2.  
  3.  
  4.  
  5. import de.android.screentest.R;
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.widget.LinearLayout;
  12.  
  13. import android.graphics.Color;
  14.  
  15. import android.graphics.drawable.Drawable;
  16.  
  17. import android.widget.*;
  18.  
  19. import android.view.*;
  20.  
  21.  
  22.  
  23. import java.io.*;
  24.  
  25. import android.content.*;
  26.  
  27.  
  28.  
  29. import android.view.WindowManager;
  30.  
  31.  
  32.  
  33. public class ScreenTest extends Activity
  34.  
  35. {    
  36.  
  37.         private LinearLayout main;
  38.  
  39.         private EditView ev;
  40.  
  41.        
  42.  
  43.     @Override
  44.  
  45.     public void onCreate(Bundle savedInstanceState)
  46.  
  47.     {
  48.  
  49.         super.onCreate(savedInstanceState);
  50.  
  51.        
  52.  
  53.         main = new LinearLayout(this);
  54.  
  55.         main.setBackgroundColor(Color.BLACK);
  56.  
  57.         main.setOrientation(LinearLayout.VERTICAL);
  58.  
  59.        
  60.  
  61.         ev = new EditView(this);
  62.  
  63.         ev.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
  64.  
  65.         ev.setText("Hello World");
  66.  
  67.         main.addView(ev);        
  68.  
  69.         setContentView(main);
  70.  
  71.     }
  72.  
  73.    
  74.  
  75.     class EditView extends EditText implements GestureDetector.OnGestureListener
  76.  
  77.     {
  78.  
  79.         GestureDetector gestureScanner;
  80.  
  81.        
  82.  
  83.         public EditView(Context context)
  84.  
  85.         {
  86.  
  87.                 super(context);
  88.  
  89.                 gestureScanner = new GestureDetector(this);
  90.  
  91.         }
  92.  
  93.        
  94.  
  95.         @Override
  96.  
  97.         public boolean onTouchEvent(MotionEvent me)
  98.  
  99.         {
  100.  
  101.                 return gestureScanner.onTouchEvent(me);
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         @Override
  108.  
  109.         public boolean onSingleTapUp(MotionEvent e)    
  110.  
  111.         {
  112.  
  113.                 ev.setText("");
  114.  
  115.                 return true;
  116.  
  117.         }
  118.  
  119.        
  120.  
  121.         @Override
  122.  
  123.         public boolean onDown(MotionEvent e)
  124.  
  125.         {
  126.  
  127.                 return true;
  128.  
  129.         }
  130.  
  131.        
  132.  
  133.         @Override
  134.  
  135.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
  136.  
  137.         {
  138.  
  139.                 return true;
  140.  
  141.         }
  142.  
  143.        
  144.  
  145.         @Override
  146.  
  147.         public void onLongPress(MotionEvent e) {  }
  148.  
  149.        
  150.  
  151.         @Override
  152.  
  153.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  154.  
  155.         {
  156.  
  157.                 return true;
  158.  
  159.         }
  160.  
  161.        
  162.  
  163.         @Override
  164.  
  165.         public void onShowPress(MotionEvent e) {   }    
  166.  
  167.     }    
  168.  
  169. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Re: need help in clearing the text in EditText view

Postby Sweevo » Wed Oct 29, 2008 11:26 pm

Do you mean when it first gains focus, not when it's clicked after it's already got focus?

I'm pretty sure there's a callback for that, too - but it's not the same one.
Sweevo
Developer
Developer
 
Posts: 35
Joined: Thu Oct 16, 2008 9:27 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests