0

I have the following issue I cant solve:

I'm building an android app with multiple fragments. On the second fragment there is a button for which the background (i.e. a drawable) needs to be changed every time the button is pressed. Before I add more logic, I want this to work but I'm not able to get it to work. Help is much appreciated!

round_button_f2_decrease.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorButtonF1Decrease" />
        </shape>
    </item>
    <item
        android:drawable="@drawable/ic_trending_down_white_24px"
        android:gravity="center" />
</layer-list>

round_button_f2_increase.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorButtonF1Increase" />
        </shape>
    </item>
    <item
        android:drawable="@drawable/ic_trending_up_white_24px"
        android:gravity="center" />
</layer-list>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/f2_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Placeholder"
        android:textAlignment="center"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/f2_button"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_marginBottom="24dp"
        android:layout_marginEnd="24dp"
        android:elevation="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

FragmentTwo.java

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class FragmentTwo extends Fragment {
    private static final String TAG = "FragmentTwo";

    private Button mbtnSort;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_two, container, false);

        mbtnSort = view.findViewById(R.id.f2_button);
        mbtnSort.setBackground(getResources().getDrawable(R.drawable.round_button_f2_decrease));
        mbtnSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable btnBackground = mbtnSort.getBackground();
                if (btnBackground.equals(getResources().getDrawable(R.drawable.round_button_f2_decrease))) {
                    Toast.makeText(getActivity(), "IF", Toast.LENGTH_LONG).show();
                    mbtnSort.setBackground(getResources().getDrawable(R.drawable.round_button_f2_increase));
                } else {
                    Toast.makeText(getActivity(), "ELSE", Toast.LENGTH_LONG).show();
                    mbtnSort.setBackground(getResources().getDrawable(R.drawable.round_button_f2_decrease));
                }
            }
        });
        return view;
    }
}
Maarten
  • 7
  • 5

3 Answers3

0

Use

mbtnSort.setBackground(R.drawable.round_button_f2_decrease);
Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14
  • my bad it should be setBackgroundResource instead and you will always get else because getResources.getDrawable will return new object every time instead put a global flag like a boolean there. – Taranmeet Singh May 07 '18 at 15:07
  • Can you please explain: "...instead put a global flag like a boolean there." or maybe an example? – Maarten May 07 '18 at 15:10
  • try something like this public class Fragment { private boolean isSelected = false; public void onCreate(){ mBtnSort.setOnClickListener( new View.OnClickListener(){ if(isSelected){ // set first image } else{ // set second image } // reverse boolean isSelected = !isSelected; }); } } – Taranmeet Singh May 07 '18 at 15:17
0

Now it works:

public class FragmentThree extends Fragment {
    private static final String TAG = "FragmentThree";

    private Button mbtnSort;
    private boolean mbtnPressed = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_three, container, false);

        mbtnSort = view.findViewById(R.id.f3_button);
        mbtnSort.setBackgroundResource(R.drawable.round_button_f2_decrease);
        mbtnSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mbtnPressed == true) {
                    Toast.makeText(getActivity(), "IF", Toast.LENGTH_LONG).show();
                    mbtnSort.setBackgroundResource(R.drawable.round_button_f2_decrease);
                    mbtnPressed = false;
                } else {
                    Toast.makeText(getActivity(), "ELSE", Toast.LENGTH_LONG).show();
                    mbtnSort.setBackgroundResource(R.drawable.round_button_f2_increase);
                    mbtnPressed = true;
                }
            }
        });
        return view;
    }
}
Maarten
  • 7
  • 5
0

i think i understand what u try to do your problem not in change background your problem with background itself

i search for you for alot of thing to know how to get back ground and check it with your round_button_f2_decrease.xml

so it's so difficult

if you make this with flag or some thing it will be helpfull for your issue now

i found this may help Comparing two drawables in android

and this Comparing resources within two drawables

Hassan Badawi
  • 302
  • 1
  • 10