Reputation: 694
The last ImageButton doesn't work, when I swap the last ImageButton with the second last ImageButton it works but the last one doesn't work now, so I am sure my buttons are registering correctly except for the last one, anytime I swap them around the last one always doesn't work, I have no hidden layouts or anything of a sort:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center|left"
android:text="Choose your option(s) below:"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="italic"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_margin="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibSingle"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvSingle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Single"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibSeeking"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvSeeking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Seeking"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibDrinking"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvDrinking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Drinking"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
>
<ImageButton
android:id="@+id/ibSmoking"
android:layout_width="20dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:src="@drawable/radio_empty"
android:layout_gravity="center"
android:layout_margin="20dp"
android:scaleType="fitCenter"
android:background="@color/transparent"
/>
<TextView
android:id="@+id/tvSmoking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Smoking"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
</LinearLayout>
Notice the 'Smoking' ImageButton is the last one, if I swap it with 'Drinking' for example then Smoking works and drinking doesn't work
JAVA:
ibSingle = (ImageButton) findViewById(R.id.ibSingle);
ibSeeking = (ImageButton) findViewById(R.id.ibSeeking);
ibDrinking = (ImageButton) findViewById(R.id.ibDrinking);
ibSmoking = (ImageButton) findViewById(R.id.ibSmoking);
sendSingle = "0";
sendSeeking = "0";
sendDrinking = "0";
sendSmoking = "0";
ibSingle.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (sendSingle.equals("0"))
{
ibSingle.setImageResource(R.drawable.radio_fill);
sendSingle = "1";
}
else if (sendSingle.equals("1"))
{
ibSingle.setImageResource(R.drawable.radio_empty);
sendSingle = "0";
}
}
});
ibSeeking.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (sendSeeking.equals("0"))
{
ibSeeking.setImageResource(R.drawable.radio_fill);
sendSeeking = "1";
}
else if (sendSeeking.equals("1"))
{
ibSeeking.setImageResource(R.drawable.radio_empty);
sendSeeking = "0";
}
}
});
ibDrinking.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (sendDrinking.equals("0"))
{
ibDrinking.setImageResource(R.drawable.radio_fill);
sendDrinking = "1";
}
else if (sendDrinking.equals("1"))
{
ibDrinking.setImageResource(R.drawable.radio_empty);
sendDrinking = "0";
}
}
});
ibSmoking.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (ibSmoking.equals("0"))
{
Toast.makeText(getApplicationContext(), "last-button clicked", Toast.LENGTH_SHORT).show();
ibSmoking.setImageResource(R.drawable.radio_fill);
sendSmoking = "1";
}
else if (sendSmoking.equals("1"))
{
Toast.makeText(getApplicationContext(), "last-button clicked", Toast.LENGTH_SHORT).show();
ibSmoking.setImageResource(R.drawable.radio_empty);
sendSmoking = "0";
}
}
});
And yes I prefer to code my own manual radio or checkbox buttons and not use the the built in method.
Everything works as expected except the last ImageButton (whichever one is placed last), it makes a click sound only. I tried to add a firth dummy ImageButton now the last 2 ImageButtons ('Dummy' and 'Smoking') didn't work, if it did then I would make 'Dummy' invisible but too bad that hack didn't work.
Upvotes: 0
Views: 95
Reputation: 71
The main Problem is in your condition if(ibSmoking.equals("0")). You are checking the condition with imagebutton name rather than String value. Replace this condition with if(sendSmoking.equals("0")).
Upvotes: 0
Reputation: 984
Your code fault in this line if (ibSmoking.equals("0"))
in ibSmoking
clickListener. You check wrong condition in this line.
Upvotes: 0
Reputation: 195
Please check you if condition in are you use if (ibSmoking.equals("0")) replace ibSmoking to sendSmoking.
Upvotes: 1