0

I am having a footer layout included in my map layout. The footer has 3 ImageButtons. I tried everything to figure out to catch the clicks but without success. I only need the footer to be working in this activity so i dont see it necessary creating an extra class file if possible.

footer.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:weightSum="4"
    android:clickable="true"
    android:background="@drawable/footer">

    <ImageButton
        android:id="@+id/guardianButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:clickable="true"
        android:layout_weight="1"
        android:background="@drawable/guardianbutton" >

</ImageButton>

    <ImageButton
        android:id="@+id/sosButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:background="@drawable/sosbutton"
        android:paddingLeft="5dp"
        android:layout_weight="2"
        android:paddingRight="5dp" />

    <ImageButton
        android:id="@+id/settingsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sosButton"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:background="@drawable/settingsbutton" >

 </ImageButton>
  </LinearLayout>
  </merge>

mapviewmain.xml

<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"
tools:context=".MapViewMain" 

 <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@layout/header"
    class="com.google.android.gms.maps.SupportMapFragment" />

    <!--  Header Starts-->
 <LinearLayout 
android:orientation="vertical"
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header"
android:paddingTop="5dip"
android:paddingBottom="5dip">

 </LinearLayout>
 <!--  Header Ends -->    

        <!-- Footer Start -->

    <RelativeLayout android:id="@+id/RelativeLayout03"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
    <include layout="@layout/footer" /></RelativeLayout>
    <!-- Footer Ends -->

 </RelativeLayout> 

Everything is being displayed correctly. I am just not get access to the onclick listeners of the ImageButtons. So far i tried the following:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.footer, container, false);
    container.addView(view);



ImageButton loadmore = (ImageButton) view.findViewById(R.id.sosButton);
loadmore.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Toast.makeText(MapViewMain.this, "SOS Button pressed", Toast.LENGTH_LONG).show();
    }
});

    return view;
}
SunnySonic
  • 1,318
  • 11
  • 37

1 Answers1

1

because your fragment does not contain the imagebutton. your mapviewmain.xml is under your activity so you have to do like this.

class MainFragmentActiviy extend FragmentActivity
 {  public ImageButton loadmore;

  onCreate(Bundle savedInstanceState)
    {
      setContentView(R.layout.mapvuewmain.xml);
       loadmore=findViewById(R.id.sosButton);
    }
}

and in your fragment

public void OnCreate(Bundle SavedInstanceState)
{
   MainFragmentActiviy yourActivity=getActivity();
}
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.footer, container, false);
yourActivity.loadmore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
    Toast.makeText(MapViewMain.this, "SOS Button pressed", Toast.LENGTH_LONG).show();
}
 });
 return view;
}
Hardik
  • 17,179
  • 2
  • 35
  • 40
  • Thank you for your answer Hardik. I still can't wrap my head around it. Currently i have an activity MapViewMain.java using the mapviewmain.xml layout which includes the footer.xml layout. You mean i will have to create a separate activity, for example footer.java which will have the code of your fragment above? Your MainFragmentActivity would be my MapViewMain.java? Further I am having problems with this code: MainFragmentActiviy yourActivity=getActivity(); Does my footer class have to extend fragment? Getting suggestion to change to FragmentActivity instead of MainFragmentActivity – SunnySonic Nov 02 '13 at 16:09
  • no you don't need to create a separate activity . just your activity must extend FragmentActivity (e.g MapViewMain extend FragmentActivity). just do that for your activity. and after in your fragment do as my answer. – Hardik Nov 03 '13 at 06:21
  • I really cant follow you. Please see my edited answer what i tried so far. I think your fragment code has some mistake. you are not finding the button in the layout but setting an onclicklistener... – SunnySonic Nov 04 '13 at 04:30
  • used this to solve my problem: http://stackoverflow.com/questions/4121986/button-onclick-listener-in-included-layouts – SunnySonic Nov 04 '13 at 08:16
  • @SunnySonic ya i am finding Imagebutton "loadmore" on MainFragmentActivity see my answer, and you can get fragmentActivity refrence to you fragment and setOnclicklistener on that as i did in my answer.thanks. – Hardik Nov 04 '13 at 09:47