Change Image when Touch a dynamic view with onTouchListner

196 views Asked by At

Image is not changing when i touch on the layout. It is dynamic and i have added this to a layout. here is my full code by which i have created a dynamic layout

        ID = 1;
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;
        Log.i("VRV", "Width :: " + width);
        Log.i("VRV", "height :: " + height);
        mWidth = width /    32;
        Log.i("VRV", "mWidth :: " + mWidth);
        int minHeight1 = height / 10;
        int mHeight = height - (int) (minHeight1 * 0.9);
        int minHeight = mHeight / 10;
        Log.i("VRV", "minHeight is ::" + minHeight);
        mAddingValue = (int) (minHeight * 0.9) + (int) (minHeight * 1);
        mHeaderLayout.getLayoutParams().height = (int) (minHeight * 2);
        mFooterLayout.getLayoutParams().height = (int) (minHeight * 2);
        mLinearLayout.getLayoutParams().height = (int) (minHeight * 6);
        mLinearLayout.setOnTouchListener(onTouchListener);

        connect(getIntent().getStringExtra("DEVICENUM"));
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<Integer> jList = new ArrayList<>();
        for (int i = 0; i < 209; i++) {
            int rowno = i / 30;
            int col = i % 7;
            int j1 = (rowno * 100) + col;
            if (list.contains("" + j1)) {
                Log.d("VRV13", " design new  " + i);
                jList.add(i + 1);
            }
        }
        Log.i("VRV", "list.toString() :: " + jList.toString());

        for (int i = 0; i < 7; i++) {
            LinearLayout linearLayout = new LinearLayout(Drawingletters.this);
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mWidth));
            linearLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
            mViewList.add(linearLayout);
            for (int j = 0; j < 30; j++) {
                mView = new ImageView(Drawingletters.this);
                mView.setId(ID + j);
                mView.setLayoutParams(new LinearLayout.LayoutParams(mWidth, mWidth));
                Log.i("VRV", "list.contains(ID) :: " + ID + " ==> " + list.contains(ID));
                if (jList.contains(ID + j)) {
                    mView.setTag(1);
                    mView.setImageResource(R.drawable.round_white);
                } else {
                    mView.setTag(0);
                    mView.setImageResource(R.drawable.round_gray);
                }

                mView.setPadding(1, 1, 1, 1);
                mTextList.add(mView);
                linearLayout.addView(mView);
            }
            ID = ID + 30;
            mLinearLayout.addView(linearLayout);
        }

this code making my layout looking like this enter image description here

When i touch or drag on this , i am set white round image to it. Then the layout should be look like this

enter image description here

In this when i drag the layout white image is showing according to x-y co-ordinate and when i click on single white image it will vanish (not exactly vanish because i set grey round image to it. But my main problem is that when i click on a single grey round image the image is not changing (it is changing only when i drag). I want to change image in touch and drag both. Here is my onTouchListner code

View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int y = (int) event.getY();
        int x = (int) event.getX();
        if (y / mWidth < 7 && y / mWidth > -1) {
            ViewGroup viewGroup = mViewList.get(y / mWidth);
            ImageView mText = (ImageView) viewGroup.getChildAt(x / mWidth);
            if (mText != null) {
                if (isTouched && event.getAction() == MotionEvent.ACTION_UP) {
                    isTouched = false;
                    Log.i("VRV", "onTouchEvent mText.getId() :: " + mTextList.get(mText.getId() - 1).getTag());
                    mText.setImageResource(R.drawable.round_gray);
                    mText.setTag(1);
                    Log.i("VRV", "onTouchEvent mText.getId() :: " + mText.getId());
                    mTextList.get(mText.getId()-1).setTag(1);
                    return true;
                } else{
                    isTouched = true;
                    isTouchedenable = false;
                    mText.setImageResource(R.drawable.round_white);
                    mText.setTag(0);
                    Log.i("VRV", "onTouchEvent mText.getId() :: " + mText.getId());
                    mTextList.get(mText.getId()-1).setTag(0);
                }
            }
        }
        return true;
    }
};

Please help me with this

0

There are 0 answers