Android Image button Does not work on some phones (Moto g2, Galaxy s2)

In my Android application, I use some image buttons to scroll the ViewFlipper.

My code works fine for all tracks except 1 in some phones (it does not work for Motorola Moto g2 - Android 4.4.4, Samsung Galaxy s2 - Android 4.4.4but works great for Galaxy S3 - Android 4.4.4, Asus Zenfone 5, LG L2and on the emulator).

The code for the snippet is as follows:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/exercise_g_background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sfcodingteam.psykeappautostima.slidingmenu.fragments.exercise.ExerciseG">

    <ImageButton
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/buttons_exercise_g"
        android:contentDescription="@string/Forward"
        android:src="@drawable/ic_go_next" />

    <ImageButton
        android:id="@+id/btnPrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/buttons_exercise_g"
        android:contentDescription="@string/Back"
        android:src="@drawable/ic_go_previous" />

    <ImageButton
        android:id="@+id/btnAudio"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnNext"
        android:layout_centerHorizontal="true"
        android:background="@drawable/buttons_exercise_g"
        android:contentDescription="@string/Audio" />

    <ViewFlipper
        android:id="@+id/viewFlipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textViewTitleG00"
                    style="@style/TitleStyle"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:text="@string/G" />

                <TextView
                    android:id="@+id/textViewG00"
                    style="@style/TextStyleFirstSentence"
                    android:layout_alignParentEnd="false"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textViewTitleG00"
                    android:layout_marginTop="25dp"
                    android:text="@string/ExerciseG00" />

                <Button
                    android:id="@+id/btnStart"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_below="@+id/textViewG00"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="80dp"
                    android:background="@drawable/buttons_exercise_g"
                    android:text="@string/btnStart"
                    android:textColor="@android:color/white"
                    android:textStyle="bold" />
            </RelativeLayout>
        </ScrollView>
    </ViewFlipper>
</RelativeLayout>

(the fragment contains a lot of content, this is part of it).

This is the initialization of all image buttons:

protected void initializeBtnNext() {
        btnNext = (ImageButton) view.findViewById(R.id.btnNext);
        if (btnNext == null)
            return;
        btnNext.setVisibility(View.INVISIBLE);
        btnNext.setEnabled(false);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewFlipper.getDisplayedChild() + 1 < viewFlipper.getChildCount())
                    goNext();
                else getActivity().onBackPressed();
            }
        });
    }

    protected void initializeBtnAudio() {
        btnAudio = (ImageButton) view.findViewById(R.id.btnAudio);
        if (btnAudio == null)
            return;
        btnAudio.setImageResource(R.drawable.ic_play);
        btnAudio.setEnabled(false);
        btnAudio.setVisibility(View.INVISIBLE);
        btnAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickBtnAudio();
            }
        });
    }

    protected void initializeBtnPrevious() {
        btnPrevious = (ImageButton) view.findViewById(R.id.btnPrevious);
        if (btnPrevious == null)
            return;
        btnPrevious.setVisibility(View.INVISIBLE);
        btnPrevious.setEnabled(false);
        btnPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goPrevious();
            }
        });
    }

    protected void initializeBtnStart() {
        btnStart = (Button) view.findViewById(R.id.btnStart);
        if (btnStart == null)
            return;
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                goNext();
            }
        });
    }

Initialization is the same for all fragments (all fragments have btnNext, btnPrevious, btnAudio, and btnStart).

Only btnStart works correctly, while others do not.

Do you have any suggestions?

Edit: found a problem, viewFlipper was enabled by buttons that I was trying to achieve.

,

android:layout_above="@+id/btnNext"

viewFlipper, ! !

+4
4

ImageButton XML

<ImageButton
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/buttons_exercise_g"
    android:contentDescription="@string/Forward"
    android:src="@drawable/ic_go_next" 
    android:clickable="true"/>

Java.

// Set this oncreate method 

    ImageButton btn=(ImageButton)findViewById(R.id.btnNext);
    btn.setOnClickListener(this);


// Then implement this 
     @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
            case R.id.btnNext :
               //Show Toast
                break;

            default :
                break;
        }

    }
+2

Enabled ? Disabled.

btnXXX.setEnabled(false);
0

You can try. I use this and work great for all devices:

<ImageView
            android:id="@+id/imgButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:focusable="true"
            android:longClickable="true"
            android:onClick="refreshData"
            android:layout_margin="3dp"
            android:contentDescription="@string/lbl_refresh"
            android:src="@drawable/refresh_image" />

refreshData java method:

public void refreshData(View view) {
       // all the code here
    }
-1
source

XML file:

            <EditText
                android:id="@+id/editbankrefdate"
                android:layout_height="50dip"
                android:layout_marginLeft="3dip"
                android:background="#FFFFFF"
                android:inputType="date"
                android:singleLine="true"
                android:tag="focusable"
                android:width="170dip" />

            <ImageButton
                android:id="@+id/selectdateimg"
                android:layout_width="wrap_content"
                android:layout_height="50dip"
                android:layout_alignBottom="@+id/editText"
                android:layout_toRightOf="@+id/editText"
                android:cropToPadding="true"
                android:src="@drawable/calendar" />

and java code:

ImageButton xClickDateImg;

xClickDateImg = (ImageButton) findViewById(R.id.selectdateimg);

xClickDateImg.setOnClickListener(this);


/*
 * PerForm Date Click Operation ---------------
 */
@Override
public void onClick(View v) {
    showDialog(0);
}

@Deprecated
protected Dialog onCreateDialog(int id) {
    return new DatePickerDialog(this, datePickerListener, year, month, day);
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        edtRefdate.setKeyListener(null);
        edtRefdate.setText(selectedDay + " / " + (selectedMonth + 1)
                + " / " + selectedYear);
    }
};
/*
 * PerForm Date Click Operation Ended
 * -------------------------------------------------------------
 */
-1
source

Source: https://habr.com/ru/post/1568722/


All Articles