简体   繁体   中英

View.setBackgroundResource() is not setting the correct background resource

I am coding a "set pin" fragment for the second time and it's not working. Everything works great until I use switch blocks and View.OnTouchListener . I am trying to change the buttons background resource to a different drawable when pressed down and then back when finger is lifted.

My problem is that the background resource is not changing at all.

XML for layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/tertiary_dark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Design.NoActionBar"
>

<View
    android:id="@+id/divider3"
    android:layout_width="364.059dp"
    android:layout_height="2dp"
    android:background="@color/secondary_accent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tableLayout" />

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <Button
            android:id="@+id/pin7"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:background="@drawable/button_1_background_idle"
            android:text="7"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin8"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="8"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin9"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="9"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/pin4"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="4"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin5"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="5"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin6"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="6"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/pin1"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="1"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin2"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="2"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin3"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="3"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/pina"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="A"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin0"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="0"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pinb"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="B"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>
</TableLayout>

<View
    android:id="@+id/view"
    android:layout_width="364.059dp"
    android:layout_height="75dp"
    android:layout_marginTop="104dp"
    android:background="@color/primary_dark"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.489"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/slatebook"
    android:text="1234567890ab"
    android:textColor="@color/primary_light"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="@+id/view"
    app:layout_constraintEnd_toEndOf="@+id/view"
    app:layout_constraintStart_toStartOf="@+id/view"
    app:layout_constraintTop_toTopOf="@+id/view" />

<View
    android:id="@+id/divider"
    android:layout_width="364.059dp"
    android:layout_height="2dp"
    android:background="@color/secondary_accent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view" />

</androidx.constraintlayout.widget.ConstraintLayout>

Code for java class:

package com.example.portal;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.system.StructUtsname;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import java.util.ArrayList;

public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.login_layout, container, false);
    }

    @SuppressLint("ClickableViewAccessibility")
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        load();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void load() {
        ArrayList<Button> pinbtns = new ArrayList<>();
        ArrayList<Character> vals = new ArrayList<>();

        pinbtns.add((Button) getActivity().findViewById(R.id.pin0));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin1));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin2));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin3));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin4));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin5));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin6));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin7));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin8));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin9));
        pinbtns.add((Button) getActivity().findViewById(R.id.pina));
        pinbtns.add((Button) getActivity().findViewById(R.id.pinb));

        vals.add('0');
        vals.add('1');
        vals.add('2');
        vals.add('3');
        vals.add('4');
        vals.add('5');
        vals.add('6');
        vals.add('7');
        vals.add('8');
        vals.add('9');
        vals.add('a');
        vals.add('b');

        for (int r = 0; r < pinbtns.size(); r++) {
            Button btn = pinbtns.get(r);
            btn.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:

                            btn.setBackgroundResource(R.drawable.button_1_background_pressed);

                            System.out.println("down");

                            return true;
                        case MotionEvent.ACTION_UP:

                            Thread t = new Thread(() -> {
                                try {
                                    Thread.sleep(50);
                                } catch (Exception ig) {
                                    ig.printStackTrace();
                                }
                                getActivity().runOnUiThread(() -> {
                                    btn.setBackgroundResource(R.drawable.button_1_background_idle);
                                });
                            });
                            t.start();

                            System.out.println("up");

                            return true;
                    }

                    return false;
                }
            });

        }

    }
}

Code for drawables:

    <?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <solid
        android:color="@color/primary_dark"/>

</shape>


<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <solid
        android:color="@color/primary_accent"/>

</shape>

And the color resourcefile for good measure.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary_light">#dcdcdc </color>
    <color name="primary_dark">#1c1d1f</color>
    <color name="secondary_dark">#151515</color>
    <color name="tertiary_dark">#323335</color>
    <color name="secondary_light">#9ea09f</color>
    <color name="primary_accent">#c41442</color>
    <color name="secondary_accent">#711930</color>
</resources>

Just to clarify one more time. When I press a button (for example pin7) the system prints "down" to the console but the background does not change one bit. When I release I get "up" in the console. So it works as intended except for the background.

I have tried changing the setBacgroundResource to setBackroundColor and same outcome.

I have spent 2 hours and am trying to find out why the setBackgroundResource() is not working. I added some modification on the shape and I noticed that the change only made on the dimensions except the Color. So I created a style to eliminate the Tinte I put the backgroundTint on @null and everything works correctly.

here is the style I added

 <style name="NotTintedButton" parent="Widget.AppCompat.Button.Colored">
        <item name="backgroundTint">@null</item>
 </style>

And all you have to do is apply this style on each buttons. for example:

 <Button        
            style="@style/NotTintedButton"
            android:id="@+id/pin8"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="8"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM