mardi 5 mai 2015

How can I change the color of button, when its touched, in a scrollview?

I am trying to make a scrollview which contains 3 buttons, and I want to allow these three buttons to change color onTouch;however, when I tried to set an onTouchListener, the button, when touched, changed color, but when I lifted my finger, it didn't change. Is there anyway I can do this?

Here is an example of an onTouch and onClick Listener I have:

callButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    callButton.setBackgroundColor(Color.parseColor("#b20000"));

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    callButton.setBackground(getDrawable(R.drawable.buttonshape1));
                }

                return false;
            }
        });
callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String phno = "314-605-4110";
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + phno));
                startActivity(intent);
            }
        });

And here is my XML for the scrollview:

      <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
       xmlns:tools="http://ift.tt/LrGmb4"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       tools:context="com.google.helpstl2.VariableLocation">
       <ScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           >

           <RelativeLayout
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" android:orientation="vertical">


               <Button
                   android:id="@+id/callButton"
                   android:textAllCaps="false"
                   android:text="Give us a call "
                   android:textColor="#4E4E4E"
                   android:textSize="15sp"
                   android:background="@drawable/buttonshape"
                   android:gravity="center_vertical|center_horizontal"
                   android:paddingLeft="130dp"
                   android:paddingRight="130dp"
                   android:drawablePadding="-8dp"
                   android:layout_width="match_parent"
                   android:layout_height="47dp"
                   android:layout_below="@+id/imageOverlay"

                   android:onClick="phoneClick"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_alignParentRight="true"
                   android:layout_alignParentEnd="true" />




               <LinearLayout
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@drawable/mapbackground"

                   android:id="@+id/mapLay"


                   android:layout_alignParentRight="true"
                   android:layout_alignParentEnd="true"
                   android:layout_below="@+id/callButton"
                   android:weightSum="1"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_marginLeft="12dp"
                   android:layout_marginRight="12dp"
                   android:layout_marginTop="9dp">


                   <fragment
                       android:layout_width="match_parent"
                       android:layout_height="220dp"
                       android:name="com.google.android.gms.maps.MapFragment"
                       android:id="@+id/placeMap"
                       android:background="@drawable/mapbackground"
                       android:layout_alignParentLeft="true"
                       android:layout_alignParentStart="true"
                       android:layout_gravity="center_vertical"


                       android:duplicateParentState="true"
                       android:layout_margin="2dp"
                       android:layout_marginBottom="50dp" />
               </LinearLayout>

               <Button
                   android:id="@+id/directionsButton"
                   android:textAllCaps="false"
                   android:text="Come see us"
                   android:textColor="#000000"
                   android:textSize="14sp"
                   android:paddingLeft="10dp"
                   android:gravity="center_vertical"
                   android:drawablePadding="10dp"

                   android:layout_width="match_parent"
                   android:layout_height="44dp"
                   android:background="@drawable/buttonshape1"

                   android:onClick="directionsClick"
                   android:singleLine="false"
                   android:layout_marginTop="9dp"
                   android:layout_below="@+id/mapLay"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_marginLeft="12dp"
                   android:layout_marginRight="12dp" />

               <Button
                   android:id="@+id/moreInfoButton"
                   android:textAllCaps="false"
                   android:text="More Info"
                   android:textColor="#000000"
                   android:textSize="14sp"
                   android:gravity="center_vertical"
                   android:paddingLeft="10dp"
                   android:drawablePadding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="44dp"
                   android:background="@drawable/buttonshape2"
                   android:onClick="directionsClick"
                   android:singleLine="false"
                   android:layout_below="@+id/directionsButton"
                   android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                   android:layout_marginLeft="12dp"
                   android:layout_marginRight="12dp" />




           </RelativeLayout>
       </ScrollView>


   </RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire