In my Android application I use some Image Buttons to scroll through a ViewFlipper.
My code works perfectly for all fragments except for 1 in some phones (it doesn't work for the Motorola Moto g2 - Android 4.4.4
, Samsung Galaxy s2 - Android 4.4.4
but it works fine on Galaxy S3 - Android 4.4.4
, Asus Zenfone 5
, LG L2
and on the emulator).
The code for the fragment is like followings:
<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 contain large contents, here is a part of that).
This is the initialization of all the 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();
}
});
}
The initialization is same for all the fragments (all the fragments have a btnNext, btnPrevious, btnAudio and btnStart).
Only btnStart works properly and others don't.
Do you have any Suggestions?
Edit: Found the problem, the viewFlipper was ON the buttons I was trying to reach.
So, I just had to add
android:layout_above="@+id/btnNext"
in the viewFlipper and I'm good to go! Thank you all!
Just updated your 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"/>
In your 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With