Thursday, May 14, 2015

Setting onClickListner for the Drawable right of an EditText



public class CustomEditText extends EditText {
    private Drawable drawableRight;
    private Drawable drawableLeft;
    private Drawable drawableTop;
    private Drawable drawableBottom;
    int actionX, actionY;
    private DrawableClickListener clickListener;
    public CustomEditText (Context context, AttributeSet attrs) {
        super(context, attrs);
        // this Contructure required when you are using this view in xml
    }
    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);      
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mHeight = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }
    @Override
    public void setCompoundDrawables(Drawable left, Drawable top,
            Drawable right, Drawable bottom) {
        if (left != null) {
            drawableLeft = left;
        }
        if (right != null) {
            drawableRight = right;
        }
        if (top != null) {
            drawableTop = top;
        }
        if (bottom != null) {
            drawableBottom = bottom;
        }
        super.setCompoundDrawables(left, top, right, bottom);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Rect bounds;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            actionX = (int) event.getX();
            actionY = (int) event.getY();
            if (drawableBottom != null
                    && drawableBottom.getBounds().contains(actionX, actionY)) {
                clickListener.onClick(DrawablePosition.BOTTOM);
                return super.onTouchEvent(event);
            }
            if (drawableTop != null
                    && drawableTop.getBounds().contains(actionX, actionY)) {
                clickListener.onClick(DrawablePosition.TOP);
                return super.onTouchEvent(event);
            }
            // this works for left since container shares 0,0 origin with bounds
            if (drawableLeft != null) {
                bounds = null;
                bounds = drawableLeft.getBounds();
                int x, y;
                int extraTapArea = (int) (13 * CoreSystemContext.SCALE + 0.5);
                x = actionX;
                y = actionY;
                if (!bounds.contains(actionX, actionY)) {
                    /** Gives the +20 area for tapping. */
                    x = (int) (actionX - extraTapArea);
                    y = (int) (actionY - extraTapArea);
                    if (x <= 0)
                        x = actionX;
                    if (y <= 0)
                        y = actionY;
                    /** Creates square from the smallest value */
                    if (x < y) {
                        y = x;
                    }
                }
                if (bounds.contains(x, y) && clickListener != null) {
                    clickListener
                            .onClick(DrawableClickListener.DrawablePosition.LEFT);
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    return false;
                }
            }
            if (drawableRight != null) {
                bounds = null;
                bounds = drawableRight.getBounds();
                int x, y;
                int extraTapArea = 13;
                /**
                 * IF USER CLICKS JUST OUT SIDE THE RECTANGLE OF THE DRAWABLE
                 * THAN ADD X AND SUBTRACT THE Y WITH SOME VALUE SO THAT AFTER
                 * CALCULATING X AND Y CO-ORDINATE LIES INTO THE DRAWBABLE
                 * BOUND. - this process help to increase the tappable area of
                 * the rectangle.
                 */
                x = (int) (actionX + extraTapArea);
                y = (int) (actionY - extraTapArea);
                /**Since this is right drawable subtract the value of x from the width
                * of view. so that width - tappedarea will result in x co-ordinate in drawable bound.
                */
                x = getWidth() - x;
                 /*x can be negative if user taps at x co-ordinate just near the width.
                 * e.g views width = 300 and user taps 290. Then as per previous calculation
                 * 290 + 13 = 303. So subtract X from getWidth() will result in negative value.
                 * So to avoid this add the value previous added when x goes negative.
                 */
                if(x <= 0){
                    x += extraTapArea;
                }
                 /* If result after calculating for extra tappable area is negative.
                 * assign the original value so that after subtracting
                 * extratapping area value doesn't go into negative value.
                 */            
                if (y <= 0)
                    y = actionY;              
                /**If drawble bounds contains the x and y points then move ahead.*/
                if (bounds.contains(x, y) && clickListener != null) {
                    clickListener
                            .onClick(DrawableClickListener.DrawablePosition.RIGHT);
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    return false;
                }
                return super.onTouchEvent(event);
            }        
        }
        return super.onTouchEvent(event);
    }
    @Override
    protected void finalize() throws Throwable {
        drawableRight = null;
        drawableBottom = null;
        drawableLeft = null;
        drawableTop = null;
        super.finalize();
    }
    public void setDrawableClickListener(DrawableClickListener listener) {
        this.clickListener = listener;
    }
}



Also Create an Interface with
public interface DrawableClickListener {
    public static enum DrawablePosition { TOP, BOTTOM, LEFT, RIGHT };
    public void onClick(DrawablePosition target);
    }



Also set the drawableClickListener on the view in activity file.
editText.setDrawableClickListener(new DrawableClickListener() {

        public void onClick(DrawablePosition target) {
            switch (target) {
            case LEFT:
                //Do something here
                break;
            default:
                break;
            }
        }
    });

How to programatically set drawableLeft on Android button?

You can use the setCompoundDrawable method to do this. see the example here. I used this without using the setBounds and it worked for. u can try either way.
UPDATE: Copying the code here incase the link goes down
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );
or
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
or
txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);

Saturday, May 02, 2015

Android Sliding Menu Configuration

super.onCreate(savedInstanceState);

new DataBaseHelper(MainActivity.this);


TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Light.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf

getSlidingMenu().setMode(SlidingMenu.LEFT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

setBehindContentView(R.layout.frame_left_menu);

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_leftmenu_layout, new left_menu())
.commit();

setContentView(R.layout.frame_content);

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mDeviceWidth = dm.widthPixels;
mDeviceHeight = dm.heightPixels;

if(MainActivity.list_students.isEmpty() || obj_student.getIs_login()==0)
{
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new LoginFragment())
.commit();
}
else
{
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new DashboardFragment())
.commit();

MainActivity.mRightMenuSelectedItem = com.srt.educras.enumerator.right_menu_type.Dashboard.getNumericType();

getSlidingMenu().setSecondaryMenu(R.layout.frame_right_menu);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_rightmenu_layout, new right_menu())
.commit();
}

//setSlidingActionBarEnabled(false);

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar_layout);

//getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//getActionBar().setCustomView(R.layout.actionbar_layout);

//txtTitle = (TextView)findViewById(R.id.actionBarTitle1);

btnShowMenu = (ImageView)this.findViewById(R.id.btnShowMenu);
btnShowSecondaryMenu = (ImageView)this.findViewById(R.id.btnShowSecondaryMenu);

btnShowSecondaryMenu.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(getSlidingMenu().isMenuShowing())
showContent();
else
getSlidingMenu().showSecondaryMenu(true);
}
});

btnShowMenu.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(getSlidingMenu().isMenuShowing())
showContent();
else
getSlidingMenu().showMenu(true);
}
});

if(MainActivity.list_students.isEmpty())
{
//txtTitle.setText("Add new account");
btnShowSecondaryMenu.setVisibility(View.GONE);
}
//else
// txtTitle.setText(obj_student.getStudent_name());

SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);