2

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

Sudhansu
  • 780
  • 1
  • 9
  • 28
  • You are trying get the child at (x / width). You have to pass the index of the child (eg. getChildAt(5) if you need the 6th element (yeah 5 because it ranges 0-5)) instead of the coordinates,or use `setOnItemClickListener()` if possible. – Endre Börcsök Aug 03 '17 at 12:07
  • Hello Endre i am setting the gridview dynamically to the layout , so layout does not have setOnItemClickListner so what is the best way to do it? – Sudhansu Aug 03 '17 at 12:19
  • @Sudhansu you can add setOnItemClickListener whenever you create your GridView it doen't matter if you do it from a layout or dynamically. Also the reason why your image isn't changing is because of one of two reasons, you logic for determining the view clicked is more than likely incorrect and is either returning null or the wrong ImageVew, possibly one that is off screen. Try scrolling and see if one of the other images you didn't actually touch, is different. – cincy_anddeveloper Aug 03 '17 at 12:27
  • Hello Endre and Wade , i am so sorry that i have said i used gridview but i was not , pardon me for that. I have uploaded my complete code , please have a look – Sudhansu Aug 04 '17 at 09:44

0 Answers0