Showing posts with label Listeners. Show all posts
Showing posts with label Listeners. Show all posts

Tuesday, February 14, 2012

Filtering text for Soft Keyboard in android?

Lets say you want to show a list of available options as someone types. Filtering your list based the text of the TextEdit works fine until the user uses a soft keypad. The soft keypad buffers the characters so you do not get them as they are typed. To get around this issue, you can have your view implement the TextWatcher.

public class LocationDialog extends Dialog implements TextWatcher

Then you must implement the following methods.


@Override
public void afterTextChanged(Editable s)
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)

The one we care about is afterTextChange. Here is an example of filtering text to display available options for both the keypad and soft keypad.

public void filter(String curText){
    strings.clear();
    for(int i=0; i < allMaps.size(); i++){
        if(allStrings.get(i).toUpperCase().startsWith(curText)){
            strrings.add(allStrings.get(i));
        }
    }
    stringsAdapter.notifyDataSetChanged();
}

@Override
public void afterTextChanged(Editable s) {
    filter(s.toString().toUpperCase());
}

Sunday, January 29, 2012

Add Touch listener to imageview

public class Test extends Activity implements OnTouchListener {
  ImageView imageView;
  Bitmap bitmap;
  Canvas canvas;
  Paint paint;

  float downx = 0,downy = 0,upx = 0,upy = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imageView = (ImageView) this.findViewById(R.id.ImageView);
    Display currentDisplay = getWindowManager().getDefaultDisplay();
    float dw = currentDisplay.getWidth();
    float dh = currentDisplay.getHeight();

    bitmap = Bitmap.createBitmap((int) dw, (int) dh,
        Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    paint = new Paint();
    paint.setColor(Color.GREEN);
    imageView.setImageBitmap(bitmap);
    imageView.setOnTouchListener(this);
  }

  public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
      downx = event.getX();
      downy = event.getY();
      break;
    case MotionEvent.ACTION_MOVE:
      upx = event.getX();
      upy = event.getY();
      canvas.drawLine(downx, downy, upx, upy, paint);
      imageView.invalidate();
      downx = upx;
      downy = upy;
      break;
    case MotionEvent.ACTION_UP:
      upx = event.getX();
      upy = event.getY();
      canvas.drawLine(downx, downy, upx, upy, paint);
      imageView.invalidate();
      break;
    case MotionEvent.ACTION_CANCEL:
      break;
    default:
      break;
    }
    return true;
  }
}

Saturday, January 28, 2012

Get rectangle and circle axis click on imageview

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MypracticeActivity extends Activity {
    private float cX=555;
    private float cY=211;
    private float cR=50;
    
    private float topRX = 829;
    private float topRY = 120;
    private float bottomLX = 612;
    private float bottomLY = 244;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if(checkRect(x,y)){
                //Toast.makeText(getApplicationContext(), "X-"+String.valueOf(x)+"Y-"+String.valueOf(y), Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "Inside rect", Toast.LENGTH_SHORT).show();
            }
            
        }
        return super.onTouchEvent(e);
    }
    
    public boolean checkRect(float pX,float pY)
    {
        if(pX>=bottomLX && pX <= topRX)
        {
            if(pY>=topRY && pY <= bottomLY)
            {
                return true;
            }
        }
        return false;
    }
    
    public boolean checkPoints(float pX,float pY)
    {
        //(x-center_x)^2 + (y - center_y)^2 < radius^2
        double x = Math.pow((pX-cX), 2);
        double y = Math.pow((pY-cY), 2);
        if(cR>=(float) Math.sqrt(x+y))
            return true;
        return false;
    }
}