0

I am working on an android project, in which I need to set ImageView background (ascending icon and descending icon) multiple times. to do this I am doing it with a clicking variable like:

sortBookedOnLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        pickUpImage.setBackgroundResource(R.drawable.ascending);
                        vouchredImage.setBackgroundResource(R.drawable.ascending);
                        cancledImage.setBackgroundResource(R.drawable.ascending);
                        if(Constant.bookedOn == 1) {
                            Log.d(TAG, "FliteronClick:1 ");
                            bookedonImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ascending_active));
//                            int ascending_active_b = R.drawable.ascending_active;
//                            bookedonImage.setBackgroundResource(ascending_active_b);
//                            bookedonImage.setImageDrawable(mContext.getResources().getDrawable( R.drawable.ascending_active));
                          //  bookedonImage.setBackground(mContext.getResources().getDrawable(R.drawable.ascending_active));
//                            ivPickUpTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ascending_active, 0);
                            Constant.bookedOn = 2;
                        }else if(Constant.bookedOn == 2){
                            Log.d(TAG, "FliteronClick:2 ");
                            bookedonImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.decending_active));
 //                           bookedonImage.setImageResource(android.R.color.transparent);
//                            int decending_active_b = R.drawable.decending_active;
//                            bookedonImage.setBackgroundResource(decending_active_b);
//                            bookedonImage.setImageDrawable(mContext.getResources().getDrawable( R.drawable.decending_active));
//                            ivPickUpTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.decending_active, 0);

                            Constant.bookedOn = 1;
                        }
                        }
                    }
                });

And My Xml layout is just like:

<LinearLayout
    android:id="@+id/sortBookedOnLayout"
    android:orientation="horizontal"
    android:background="@color/white"

    android:weightSum="2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_weight="1.8"
        android:layout_width="0dp"
        android:layout_height="match_parent">
        <RelativeLayout
            android:id="@+id/bookedOntxtLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <TextView
            android:id="@+id/ivPickUpTxt"
            style="@style/editTextTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/edittext_background"
            android:drawableLeft="@drawable/cal2"
            android:drawablePadding="10dp"
            android:hint="@string/booked_on"
            android:inputType="text"
            android:maxLines="1"
            android:padding="10dp"
            android:textColor="#000000"
            android:textCursorDrawable="@drawable/cursor_drawable"
            android:textSize="@dimen/filter_text_size"

            />
        </RelativeLayout>
    </LinearLayout>
        <LinearLayout
            android:layout_weight=".2"
            android:layout_width="0dp"
            android:layout_height="match_parent">

    <ImageView
        android:id="@+id/booedOnImage"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ascending"

        />

    </LinearLayout>

</LinearLayout>

Screenshot of my layout.enter image description here

in this image i am going to click on BookedOn layout.

And my targetSdkVersion 25

I have tried it multiple ways as I commented but image icon is not reflecting on imageView. any help will be appreciable.Thanks.

singh.indolia
  • 1,292
  • 13
  • 26
  • Is there any crash or warning . OutOfMemory ? – ADM Dec 22 '17 at 04:58
  • @ADM no crash and warning. app is working fine only icons are not reflecting on ImageView. – singh.indolia Dec 22 '17 at 04:59
  • If your logic is Ok . try to use `setImageResource()` with `scaleType` `FitXY` instead of setting background . – ADM Dec 22 '17 at 05:01
  • Check what is the initial value for **Constant.bookedOn** it should be 1 or 2. and also instead of **android:background="@drawable/ascending"** try to put **android:src="@drawable/ascending"**, because you are setting setImageDrawable in java code not background so it will overlap. – Muthukrishnan Rajendran Dec 22 '17 at 05:11
  • @ADM setImageResource() with scaleType FitXY also is not working. – singh.indolia Dec 22 '17 at 05:14
  • @MuthukrishnanRajendran initial Value of Constant.bookedOn is 1 and i have tried with android:src="@drawable/ascending" i have also comment the code as i have used multiple ways. – singh.indolia Dec 22 '17 at 05:15
  • still try one more time – singh.indolia Dec 22 '17 at 05:16
  • @singh.indolia then there must be Some logical issue with your code . Debug it . One more thing Make sure your image view is visible with some size not hide behind some other views . – ADM Dec 22 '17 at 05:30
  • @singh.indolia is it possible to share a screenshot of that? – Raghavendra Dec 22 '17 at 05:54
  • my layout is looking fine but let me edit my question with screenshot for your assurance. – singh.indolia Dec 22 '17 at 06:02

1 Answers1

0

Try this:

qImageView.setBackgroundResource(R.drawable.thumbs_down);

and add android:scaleType:fitxy

<ImageView
            android:id="@+id/booedOnImage"    
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitxy"
            android:background="@drawable/ascending"/>

(or) Try programmatically

 imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

Here's a thread that talks about the differences between the two methods.

I Understand your problem...You are not set the width of the LinearLayout

 <LinearLayout
            android:layout_weight=".2"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/booedOnImage"

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/abc_btn_rating_star_off_mtrl_alpha"

                />

        </LinearLayout>
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • I have tried it already as i comment the code in my question through this i am just able to change image only two times, when i click more than two times icon is not reflecting on imageView.as First Click-ascending, second click-descending, the third click- again ascending and so on, but this is not attaining. – singh.indolia Dec 22 '17 at 05:29
  • Through scale let me try – singh.indolia Dec 22 '17 at 05:30
  • @singh.indolia..have problem is that you are not set the width of Linear layout...Please check my updated answer – Gowthaman M Dec 22 '17 at 05:30
  • okay let me check my code and and yrs after doing i will tell u – singh.indolia Dec 22 '17 at 05:32
  • @singh.indolia as my point of you this is related to design problem...double check once your layouts completely – Gowthaman M Dec 22 '17 at 05:40
  • i have tried it also and i have edited my code as layout and my layout is seems to be fine and logic also working fine as i am printing log, i getting the correct sequence of 1 and 2 as per my click over ImageView but still I am not able to get icon after two times. – singh.indolia Dec 22 '17 at 06:00
  • Thanks alot. if i resove my issue i will update you. again thanks. – singh.indolia Dec 22 '17 at 06:51