anu_r
anu_r

Reputation: 1622

Why doesn't my Button getting clicked in the ScrollView in Android?

I have a ScrollView with a fixed header and footer. My header and footer are in the scrollable_contents.xml layout. The part that gets scrolled is in the contents.xml . I want to make the Buttons in the scrollable_contents.xml clickable. I have done the following but the Button doesn't seem to get clicked. My codes and layout are as below. Please guide me step by step what to do.

scrollable_contents.xml

<?xml version="1.0" encoding="utf-8"?>
 <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" >

  <!-- Header aligned to top -->

 <RelativeLayout
  android:id="@+id/header"
  android:layout_width="match_parent"
  android:layout_height="70dp"
  android:layout_alignParentTop="true"
   android:background="@android:color/white"  >

  <ImageView
      android:id="@+id/imageView1"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:layout_alignParentLeft="true"
      android:layout_centerVertical="true"
      android:layout_marginLeft="18dp"
      android:src="@drawable/propic" />

  <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_marginLeft="24dp"
      android:layout_toRightOf="@+id/imageView1"
      android:text="Jack Reacher"
       android:textSize="17dp" 
      android:textColor="#000" />
  </RelativeLayout>

  <!-- Footer aligned to bottom -->

 <RelativeLayout
  android:id="@+id/footer"
  android:layout_width="fill_parent"
  android:layout_height="50dp"
  android:layout_alignParentBottom="true"
  android:background="@android:color/white" >

  <Button
      android:id="@+id/button1"
      android:layout_width="160dp"
      android:layout_height="50dp"
      android:background="@color/green"
      android:layout_alignParentTop="true"
      android:text="Accept" 
       android:textSize="17dp" 
       android:textColor="#FFFFFF"/>

  <Button
      android:id="@+id/button2"
      android:layout_width="160dp"
      android:layout_height="50dp"
       android:textSize="17dp" 
      android:layout_alignParentTop="true"
      android:layout_toRightOf="@+id/button1"
       android:background="@color/red"
      android:text="Decline" 
        android:textColor="#FFFFFF"/>

 </RelativeLayout>

 <!-- Scrollable Item below header and above footer -->
<ScrollView
android:id="@+id/scrollableContents"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer"
android:background="@android:color/white" 
android:layout_below="@id/header" >

 <!-- Inflate the contents of the ScrollView dynamicaly -->

  </ScrollView>

 </RelativeLayout>

contents.xml

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="190dp"
    android:layout_height="23dp"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp"
    android:background="@drawable/roundedittext"
    android:ems="10"
    android:paddingLeft="25dp"
    android:text="Task 1"
    android:textColor="#FFFFFF"
    android:textSize="17dp" >

    <requestFocus />
</EditText>

   // some more widgets

</RelativeLayout>

mycontactstemp.java

    public class mycontactstemp extends Fragment implements OnClickListener  {
 EditText statuses,name,desc,times,dates,e7;
 Button b1,b2,b3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.scrollable_contents, container,
            false);
    ScrollView scrollable_contents = (ScrollView)view.findViewById(R.id.scrollableContents);
    getActivity().getLayoutInflater().inflate(R.layout.contents, scrollable_contents);
  Button b1=(Button)view.findViewById(R.id.button1);
  Button b2=(Button)view.findViewById(R.id.button2);
  return view;
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Button b1=(Button)v.findViewById(R.id.button1);
    switch(v.getId())
    {
    case R.id.button1:
        Toast.makeText(getActivity(),"accept clicked",1000).show();
        break;
    case R.id.button2:
        Toast.makeText(getActivity(),"decline clicked",1000).show();
        break;
  }
}
}

Upvotes: 0

Views: 256

Answers (1)

andrei
andrei

Reputation: 2940

I don't see where you set the click listener on the buttons. Are you sure it is set?

You can either set it in xml but the method would be called in the activity and you have to call the fragment's method from the activity.

Or set the click listener programatically inside the fragment.

Upvotes: 2

Related Questions