Color button when pressed?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Color button when pressed?

Postby pascalbianca » Mon Apr 29, 2013 2:22 pm

Hello everyone,
i'm new to this forum and i have a question.
I use eclipse to make a Android program.
I have several buttons and when i push on them it works fine.
But i want to give the button a color when they are pressed, so i can see witch one is active and if i push the button again the orginal color comes back, so i can see its off.
Can anybody tell me how i must do that?

Thx.
pascalbianca
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Apr 24, 2013 9:09 pm

Top

Re: Color button when pressed?

Postby fChopin » Sat May 04, 2013 1:18 pm

Use TextView instead.

xml
Code: Select all
    <TextView
        android:id="@+id/textView1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/shape_text"
        android:gravity="center"
        android:text="TextView"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />


../res/drawable/shape_text_clicked.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
   
    <corners
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp" />
   <stroke android:width="1dp" android:color="#FF000000" />
   <gradient
      android:startColor="#7A67EE"
      android:endColor="#473C8B"
      android:angle="270"/>
   
</shape>


../res/drawable/shape_text.xml
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
   
    <corners
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp" />
   <stroke android:width="1dp" android:color="#FF000000" />
    <gradient
        android:startColor="#666666"
        android:endColor="#999999"
        android:angle="270"/>
   
</shape>


activity
Code: Select all
      TextView text = (TextView)findViewById(R.id.textView1);
      text.setTag(Boolean.FALSE);
      text.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            TextView t = (TextView)v;
            if(((Boolean) t.getTag()).booleanValue()) {
               t.setBackgroundResource(R.drawable.shape_text);
               t.setTag(Boolean.FALSE);
            } else {
               t.setBackgroundResource(R.drawable.shape_text_clicked);
               t.setTag(Boolean.TRUE);
            }
         }
      });
fChopin
Freshman
Freshman
 
Posts: 3
Joined: Sat May 04, 2013 10:53 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 6 guests