0

I am not sure why the button isn't working. Eclipse isn't showing any red lines and I am not getting any error in my app when I run it. I just seems like the app is completely ignoring the click. I am using nested layouts in my XML so could that be the cause?

The pictures seems to work fine when I click them but not for the button.

Code:

    public class HomeActivity extends Activity{

        private Button bSite;

        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.homeactivity);

            //addListenerSiteButton();
            bSite = (Button) this.findViewById(R.id.s_button);
            bSite.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://irishfairy.com/"));
                    startActivity(browserIntent);
                 } 
              });

            GridView gv = (GridView) findViewById(R.id.grid_view);

            gv.setAdapter(new ImageAdapter(this));

            /**
             * On Click event for Single Gridview Item
             * */
           gv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                    // passing array index
                    i.putExtra("id", position);
                    startActivity(i);
                }
            });
        }
}

XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000">

    <LinearLayout
        android:id="@+id/main_linear"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/s_button"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/roundedsitebutton"
            android:gravity="center"
            android:padding="15dip"
            android:clickable="true"
            android:text="@string/site" />

    </LinearLayout>

    <GridView
        android:id="@+id/grid_view"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:layout_below="@+id/main_linear">
    </GridView>

</RelativeLayout>

EDIT: I swapped the android:layout_alignmentParentTop and android:layout_alignmentParentBottom around in my XML and it worked:

<LinearLayout
       android:layout_alignParentTop="true"
  </LinearLayout>
<GridView
     android:layout_alignParentBottom="true"
</GridView>

However I want the button to be at the bottom.

This message came up when I run the app:

03-21 16:51:02.051: D/AbsListView(9953): unregisterIRListener() is called 

EDIT: I tried changing:

android:layout_below="@+id/main_linear"

to

android:layout_above="@+id/main_linear"

However I am getting the following error:

03-24 08:59:22.221: E/AndroidRuntime(31598): FATAL EXCEPTION: main
03-24 08:59:22.221: E/AndroidRuntime(31598): Process: com.apps.mobileapp, PID: 31598
03-24 08:59:22.221: E/AndroidRuntime(31598): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.mobileapp/com.apps.mobileapp.HomeActivity}: java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.Button
user1832478
  • 549
  • 2
  • 7
  • 17

2 Answers2

1

There appears to be a missing @Override annotation in your button onClickListener. Try changing it to:

bSite.setOnClickListener(new OnClickListener() {

    @Override //added this line
    public void onClick(View v) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://theirishfairydoorcompany.com/"));
        startActivity(browserIntent);
    }

});

You might want to also try removing android:clickable="true" from your button XML code if the above suggestion doesn't work. Sometimes it has unexpected behavior and moves the click to another view (see this question)

<Button
        android:id="@+id/s_button"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/roundedsitebutton"
        android:gravity="center"
        android:padding="15dip"
        android:text="@string/site" />
Community
  • 1
  • 1
anthonycr
  • 4,146
  • 1
  • 28
  • 35
  • That did not seem to work after adding @override and getting rid off android:clickable = true. However like what MeMe said i think its true that the gridview is overlaying the buttons when I set my LinearLayout to the bottom of my RelativeLayout. – user1832478 Mar 24 '14 at 09:07
1

Your gridview is blocking the button. Instead of having: android:layout_below="@+id/main_linear", change it to android:layout_above="@+id/main_linear"

Whitney
  • 1,217
  • 1
  • 8
  • 15
  • I tried changing to android_layout_above="@+id/main_linear". However I get a Fatal Exception. I have updated my answer. – user1832478 Mar 24 '14 at 09:06
  • It works now after I deleted the R.java file, cleaning the project. I will mark your answer as accepted. – user1832478 Mar 24 '14 at 09:20