How can I change the color of the button when it's clicked, I'm making a quiz app and when the button is clicked it will change its color
You can use a MaterialButtonToggleGroup to select from a group of choices:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:selectionRequired="true">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
style="?attr/materialButtonOutlinedStyle"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Just set View.OnClickListener to your Button and setBackgroundColor to it.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundColor(Color.parseColor("#ff0000"));
}
});
If you want to reset color when the other Button is clicked, you can use a common OnClickListener among the Buttons. Below is an example:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1;
private Button button2;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
button1.setBackgroundColor(Color.parseColor("#ff0000"));
button2.setBackgroundColor(Color.parseColor("#0000ff"));
button3.setBackgroundColor(Color.parseColor("#0000ff"));
break;
case R.id.button2:
button1.setBackgroundColor(Color.parseColor("#0000ff"));
button2.setBackgroundColor(Color.parseColor("#ff0000"));
button3.setBackgroundColor(Color.parseColor("#0000ff"));
break;
case R.id.button3:
button1.setBackgroundColor(Color.parseColor("#0000ff"));
button2.setBackgroundColor(Color.parseColor("#0000ff"));
button3.setBackgroundColor(Color.parseColor("#ff0000"));
break;
}
}
}
Related
I want to know how to link a button to edittext so that anytime a certain id is put on the edittext and a button is press, it send you to another page and give detail information about that I'd. I am a beginner so help me pls.
In the XML file, you need to have EditText and Button like the following,
<EditText
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Submit" />
And your code should be like this.
public void onCreate(Bundle savedInstanceState) {
// Initialize edittext and button
EditText txt = (EditText) findViewById(R.id.txt);
Button btn = (Button) findViewById(R.id.btn);
// Set listener to the button
btn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
String text = txt.getText().toString();
yourFunction(text);
}
});
}
private void yourFunction(String text) {
// Your search code goes here
}
This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
Hi friends can you please help me how can i make a button visable after another button is clicked
Try this:
buttonONE.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonTWO.setVisibility(View.VISIBLE);
}
});
In layout xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonTWO"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
Like this:
public class YourClass extends Activity{
Button button1;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from your xml
setContentView(R.layout.xml_layout_name);
// Locate the buttons in your xml
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
//set listener on button1
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make button2 visible when button1 is pressed
button2.setVisibility(View.VISIBLE);
}
});
}
}
In xml_layout_name:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:visibility="gone"/>
</RelativeLayout>
everyone. I am new in android. I am creating two activities in android ,1st activity has two image view and one button . and 2nd activity has one button. when I go to second activity and then from second activity agian . one image view of 1st activity should visibility gone. how can i do this.
here is my code
Activitymain.xml
<ImageView
android:id="#+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
}
secondactivity.xml
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
please help me
Activitymain.xml
<ImageView
android:id="#+id/1stimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/2ndimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to second activity" />
mainactivity.java
ImageView imageView1, imageView2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.1stimage);
imageView2 = (ImageView) findViewById(R.id.2ndimage);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Firstintent= new Intent(MainActivity.this,SecondActivity.class);
startActivity(Firstintent);
}
});
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getBoolean("isResult", false)){
imageView1.setVisability(View.GONE);
}
}
}
secondactivity.xml
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to 1st activity" />
secondactivity.java
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent SecondIntent = new Intent(SecondActivity.this,
MainActivity.class);
SecondIntent.putExtra("isResult", true);
SecondActivity.this.startActivity(SecondIntent);
SecondActivity.this.finishActivity();
}
});
}
Use putextra(tag, some variable to use in mainActivity) and Bundle extras = getIntent().getExtras(); extras.get[type of variable](tag) to pass a variable (probably a boolean) to tell the onCreate() to show or not show the pic.
EDIT:
Here is one way to implement it: in the first activity:
1) Check if there are extras (A.K.A. if second activity was loaded/ there is a true)
2) If (yes) { if (first pic is loaded) { remove first pic } else if (first pick is not but second is loaded) { remove second pic } ... for all the pictures you want to do this to.
Remember you only need to pass true/false from the second activity (and then handle it in the first/one being modified)
I have a simple xml with a question and two buttons. When one of buttons is pushed i will compare if the id of the pushed button is equal to "Blanco" or "Negro".
The XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/pregunta" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Blanco"
android:onClick="respuesta"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Negro"
android:onClick="respuesta"/>
</LinearLayout>
</LinearLayout>
This is de java code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void respuesta(){
//The doubt.
//Here the if/else to compare ID with the button text
}
}
Don't do it like that. Create an onClickListener for each Button, so you know exactly which one is being pressed. For example:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener{
public void onClick(View v) {
// call code here, knowing that button1 was pressed
}
});
button2.setOnClickListener(new View.OnClickListener{
public void onClick(View v) {
// call code here, knowing that button2 was pressed
}
});
}
}
implement onClickListener into your Activity.
public class MainActivity extends ActionBarActivity implements OnClickListener
Declare the Button variables inside your class
Button btblanco, btnegro;
Implement the clickListener event on onCreate
btblanco = (Button) findViewById(R.id.button1);
btnegro = (Button) findViewById(R.id.button2);
btblanco.setOnClickListener(this);
btnegro.setOnClickListener(this);
And put this inside onClickListener method.
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
Toast.makeText(getApplicationContext(), "Blanco", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(getApplicationContext(), "Negro", Toast.LENGTH_SHORT).show();
break;
}}
if you are interested in just knowing which button was clicked, then add a onclicklistener to the button objects. Google for it a bit and the samples you find will show you how to use a switch case structure to d actions based on which button was clicked.