0

I have a custom button in an activity. It works fine. I hit back, then restart the activity, and the button is gone. I created a stripped project to show what the problem is.

The application starts with this activity (just a button that starts Activity2):

public class TestCustomButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent intent = new Intent(TestCustomButtonActivity.this, Activity2.class);
                startActivity(intent);
                }
        });
    }
}

Activity2 has only one button

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/menu_left_button" />

menu_left_button is the xml selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:constantSize="true">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_left_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_left_hovered" /> <!-- focused -->
    <item android:drawable="@drawable/button_left_normal" /> <!-- default -->
</selector>

And that's all there is to it. You start the application and hit button1, and you are presented with Activity2 and our custom button. And it works fine. But if you hit the back button (close Activity2) and hit button1 again, our custom button is not there! It has disappeared. Any good reason for that?

Target api level is 4 (if this has something to do with it).

Activity2 code is nearly empty:

public class Activity2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
    }
}
cdriver
  • 155
  • 1
  • 12
  • Where is the activity 2 code? post it. – user370305 Nov 16 '11 at 10:31
  • add the code for activity2 also. – Shaireen Nov 16 '11 at 10:32
  • posted the Activity2 code and changed buttons' ids to button1 and button2. button2 is the one disappearing, in Activity2. – cdriver Nov 16 '11 at 11:13
  • I've tried many things, still not working. Changed api level to 7, nothing. Added one more button that performes: (a) btn2.invalidate(), nothing. (b) btn2.setBackgroundResource(R.drawable.menu_left_button), still nothing. Dimensions change from 50x50 to 19x0, no reason whatsoever to change to 19x0 and not some other random value. – cdriver Nov 16 '11 at 15:11
  • After trying every possible solution to this, i came up with this solution, which is not completely satisfying, but will get the job done. I created a new xml selector using the eclipse wizard (topleftbutton.xml). Copied in it the contents of my previous selector (menu_left_button.xml), without changing anything. It now works fine. But i'm still wondering why. Tried to copy menu_left_button.xml to a new file without underscores, but it doesn't work. It has to be a file created by the eclipse wizard. So it must be something that the wizard does, that i didn't do. Any help with that appreciated. – cdriver Nov 16 '11 at 16:07

3 Answers3

1

android setting:constantSize="false" helps for me

so your code will look like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:constantSize="false">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_left_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_left_hovered" /> <!-- focused -->
    <item android:drawable="@drawable/button_left_normal" /> <!-- default -->
</selector>
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Santific
  • 11
  • 1
  • Fixed my issue as well. On Android 2.2 device I did not have an issue. Testing on Android 4.x Nexus 7 I had an issue with selectors using constantSize="true" causing an ImageButton to size down to 0,0. The initial design time Background assignment with a selector that had constantSize="true" worked fine, but setting programatically via SetBackgroundResource() or SetBackgroundDrawable() would cause the ImageButton to size to 0,0. – t9mike Oct 05 '12 at 16:09
0

Now try again.

public class TestCustomButtonActivity extends Activity {
    /** Called when the activity is first created. */

   private static Button btn1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent intent = new Intent(TestCustomButtonActivity.this, Activity2.class);
                startActivity(intent);
                }
        });
    }
}
jainal
  • 2,973
  • 2
  • 20
  • 18
  • This is not solving the problem, although i have to admit i had 2 button1 ids in this project which was confusing at least. Corrected it now. The problem was not in this "button1" but in the other "button1", now button2. – cdriver Nov 16 '11 at 11:15
0

After several months as you can see, I found the answer to this, by chance. It is posted as an answer in another stackoverflow question : ImageButton does not display a particular drawable. The actual problem as i now know is that the image "button_left_normal" does not show, since it is the first image alphabetically. If the button had a fixed size and not "wrap_content" as dimensions, i would be able to click it, and see that "button_left_pressed" is shown without any problem. I hope this helps someone out there.

Community
  • 1
  • 1
cdriver
  • 155
  • 1
  • 12