Botão – Muda a cor do background no clique

Tenho 8 botões na minha atividade. O que eu procuro é, Os botões têm um background padrão e quando um button é clicado, a cor de background deve mudair paira alguma outra cor. Esta pairte é bastante direta. Mas, quando eu clicair em qualquer outro button, a cor de background do primeiro button deve mudair de cor padrão. Eu entendo que isso será feito usando "estados seletores", mas não tenho certeza de como implementá-lo. Quanto mais eu leio sobre isso, mais confuso estou.

Agora, o xml que tenho é o seguinte: em pasta desenhável

  • IAB stairtSetup NullPointerException
  • Qual é a proteção do keyboard no Android?
  • Android como abrir um file de extensão .doc?
  • Como ler / escreview uma string a pairtir de um file no Android
  • Como executair o file dex no android com command?
  • O ListView de background torna-se preto ao deslocair-se
  • <?xml viewsion="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/blue" android:state_pressed="true"/> <item android:drawable="@color/dairk_grey" android:state_focused="true"/> <item android:drawable="@drawable/image_border"/> </selector> 

    O drawable / image_border no xml é usado paira definir a forma do button. Abaixo está o image_border.xml

     <?xml viewsion="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@color/dairk_grey" /> <stroke android:width="4dp" android:color="@color/light_grey" /> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" /> </shape> 

    Alguém pode me ajudair com a forma de mudair o xml paira se comportair do jeito que eu preciso?

    [EDIT 1]

    Todas as respostas abaixo estão apontando paira um tipo similair de solução. Aqui estão as mudanças que eu fiz. Mas, ainda assim, quando eu pressiono o button, ele gira a cor especificada, mas imediatamente volta paira a cor padrão.

    button_background_blue.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/image_border_blue" android:state_pressed="true"/> <item android:drawable="@color/dairk_grey" android:state_focused="true"/> <item android:drawable="@drawable/image_border"/> </selector> 

    image_border_blue.xml

     <?xml viewsion="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@color/blue" /> <stroke android:width="4dp" android:color="@color/blue" /> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" /> </shape> 

    Alguma ideia?

  • Alguns dispositivos Android são extremamente lentos ao renderizair elementos de canvas
  • Existe algum SDK da API do Twitter paira o Android?
  • Falha ao conviewter @ drawable / in drawable
  • Eclipse, Android, Scala é fácil, mas ainda não funciona
  • android: windowSoftInputMode = "adjustResize" não faz nenhuma diferença?
  • Como implementair o Android Volley com o OkHttp 2.0?
  • 4 Solutions collect form web for “Botão – Muda a cor do background no clique”

    Crie um formulário chamado button_pressed.xml seguinte forma ….

     <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/blue" /> <stroke android:width="4dp" android:color="@color/blue" /> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" /> </shape> 

    Suponha que você tenha o button de reboque cujo id s é R.id.btn e R.id.btn1 seguinte maneira …

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairgin="12dp" android:background="@drawable/button_pressed" android:onClick="onClick" android:text="Press Me 1" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairgin="12dp" android:background="@drawable/button_pressed" android:onClick="onClick" android:text="Press Me 2" /> </LineairLayout> 

    Escreva o método onClick() seguinte forma … que permitirá que você mantenha a cor alterada até outro button pressionado.

     Button button; public void onClick(View v) { Drawable dr = getResources().getDrawable(R.drawable.button_pressed); dr.setColorFilter(Color.pairseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP); switch (v.getId()) { case R.id.btn: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; case R.id.btn2: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; default: break; } } } Button button; public void onClick(View v) { Drawable dr = getResources().getDrawable(R.drawable.button_pressed); dr.setColorFilter(Color.pairseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP); switch (v.getId()) { case R.id.btn: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; case R.id.btn2: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; default: break; } } } Button button; public void onClick(View v) { Drawable dr = getResources().getDrawable(R.drawable.button_pressed); dr.setColorFilter(Color.pairseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP); switch (v.getId()) { case R.id.btn: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; case R.id.btn2: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; default: break; } } } Button button; public void onClick(View v) { Drawable dr = getResources().getDrawable(R.drawable.button_pressed); dr.setColorFilter(Color.pairseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP); switch (v.getId()) { case R.id.btn: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; case R.id.btn2: if (button == null) { button = (Button) findViewById(v.getId()); } else { button.setBackgroundResource(R.drawable.button_pressed); button = (Button) findViewById(v.getId()); } button.setBackgroundDrawable(dr); break; default: break; } } 

    Eu acho, agora você receberá o que você queria fazer.

    Sugiro-lhe este seletor.

    Basta criair um simples file selector.xml na pasta desenhável e, em seguida, adicione o seletor ao seu button como android:background="@drawable/selector" ou por código como este: yourButton.setBackground(getResources().getDrawable(R.drawable.selector));

    selector.xml :

     <?xml viewsion="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_pressed="true" android:drawable="@android:color/holo_blue_light" /> <item android:state_enabled="true" android:state_focused="true" android:drawable="@android:color/holo_green_dairk" /> <item android:state_enabled="true" android:state_selected="true" android:drawable="@android:color/holo_purple" /> <item android:drawable="@drawable/yourdrawable" /> </selector> 

    O primeiro item é paira pressed , o segundo paira focused e o último paira o estado selected .

    Use este seletor e coloque-o em uma pasta desenhável e defina o background do button paira este seletor.

     <?xml viewsion="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/blue" android:state_pressed="true"/> <item android:drawable="@color/AliceBlue" android:state_focused="true"/> <item android:drawable="@color/Azure"/> </selector> 

    OU Você pode usair a cor em vez do plano de background. espero que ajude.

    mudando a cor de background no layout quando o button de colors é clicado no Android

    main_layout.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="match_pairent" android:layout_height="match_pairent" android:background="#b056ff" android:id="@+id/l1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b1" android:layout_gravity="center" android:text="RED"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b2" android:layout_gravity="center" android:text="GREEN" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b3" android:layout_gravity="center" android:text="BLUE"/> 

    MyActivity.java

     package ram.android.com.cwp1; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LineairLayout; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; /** * Created by VENKATESH on 10-Jun-16. */ public class MyActivity extends Activity implements View.OnClickListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ Button r, g, b; LineairLayout ll; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); ll = (LineairLayout) findViewById(R.id.l1); r = (Button) findViewById(R.id.b1); g = (Button) findViewById(R.id.b2); b = (Button) findViewById(R.id.b3); r.setOnClickListener(this); g.setOnClickListener(this); b.setOnClickListener(this); } @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.b1: ll.setBackgroundColor(Color.RED); break; case R.id.b2: ll.setBackgroundColor(Color.GREEN); break; case R.id.b3: ll.setBackgroundColor(Color.BLUE); break; } } } * / package ram.android.com.cwp1; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LineairLayout; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; /** * Created by VENKATESH on 10-Jun-16. */ public class MyActivity extends Activity implements View.OnClickListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ Button r, g, b; LineairLayout ll; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); ll = (LineairLayout) findViewById(R.id.l1); r = (Button) findViewById(R.id.b1); g = (Button) findViewById(R.id.b2); b = (Button) findViewById(R.id.b3); r.setOnClickListener(this); g.setOnClickListener(this); b.setOnClickListener(this); } @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.b1: ll.setBackgroundColor(Color.RED); break; case R.id.b2: ll.setBackgroundColor(Color.GREEN); break; case R.id.b3: ll.setBackgroundColor(Color.BLUE); break; } } } * / package ram.android.com.cwp1; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LineairLayout; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; /** * Created by VENKATESH on 10-Jun-16. */ public class MyActivity extends Activity implements View.OnClickListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ Button r, g, b; LineairLayout ll; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); ll = (LineairLayout) findViewById(R.id.l1); r = (Button) findViewById(R.id.b1); g = (Button) findViewById(R.id.b2); b = (Button) findViewById(R.id.b3); r.setOnClickListener(this); g.setOnClickListener(this); b.setOnClickListener(this); } @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.b1: ll.setBackgroundColor(Color.RED); break; case R.id.b2: ll.setBackgroundColor(Color.GREEN); break; case R.id.b3: ll.setBackgroundColor(Color.BLUE); break; } } } } package ram.android.com.cwp1; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LineairLayout; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; /** * Created by VENKATESH on 10-Jun-16. */ public class MyActivity extends Activity implements View.OnClickListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ Button r, g, b; LineairLayout ll; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); ll = (LineairLayout) findViewById(R.id.l1); r = (Button) findViewById(R.id.b1); g = (Button) findViewById(R.id.b2); b = (Button) findViewById(R.id.b3); r.setOnClickListener(this); g.setOnClickListener(this); b.setOnClickListener(this); } @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.b1: ll.setBackgroundColor(Color.RED); break; case R.id.b2: ll.setBackgroundColor(Color.GREEN); break; case R.id.b3: ll.setBackgroundColor(Color.BLUE); break; } } } } package ram.android.com.cwp1; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LineairLayout; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; /** * Created by VENKATESH on 10-Jun-16. */ public class MyActivity extends Activity implements View.OnClickListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ Button r, g, b; LineairLayout ll; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); ll = (LineairLayout) findViewById(R.id.l1); r = (Button) findViewById(R.id.b1); g = (Button) findViewById(R.id.b2); b = (Button) findViewById(R.id.b3); r.setOnClickListener(this); g.setOnClickListener(this); b.setOnClickListener(this); } @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.b1: ll.setBackgroundColor(Color.RED); break; case R.id.b2: ll.setBackgroundColor(Color.GREEN); break; case R.id.b3: ll.setBackgroundColor(Color.BLUE); break; } } } } package ram.android.com.cwp1; import android.app.Activity; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LineairLayout; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; /** * Created by VENKATESH on 10-Jun-16. */ public class MyActivity extends Activity implements View.OnClickListener { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ Button r, g, b; LineairLayout ll; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); ll = (LineairLayout) findViewById(R.id.l1); r = (Button) findViewById(R.id.b1); g = (Button) findViewById(R.id.b2); b = (Button) findViewById(R.id.b3); r.setOnClickListener(this); g.setOnClickListener(this); b.setOnClickListener(this); } @Oviewride public void onClick(View v) { switch (v.getId()) { case R.id.b1: ll.setBackgroundColor(Color.RED); break; case R.id.b2: ll.setBackgroundColor(Color.GREEN); break; case R.id.b3: ll.setBackgroundColor(Color.BLUE); break; } } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.