I have created an app that suppose to toggle the button background color on every click. Here is the code:
package com.example.flash.light;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button screen = (Button) findViewById(R.id.screen);
Drawable background = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(background);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
}
}
});
}
Click on the button doesn't response.
Thanks
I belive the problem here is that the resource returned by your
getResources().getDrawable(int id)
is different everytime you call it. Android just constructs new instance of drawable class instead of returning old one. (At least I belive that there is no caching in this case)
Comparing three different instances with == operator will never return true.
Second thing is an obvious bug in the code:
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(**newBackground**);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(**newBackground**);
}
instead of background you should have newBackground there.
Try this :
public class MainActivity extends Activity {
private boolean isBlack = false;
#Override
public void onCreate(Bundle savedInstanceState) {
// your code
final Button screen = (Button) findViewById (R.id.screen);
isBlack = true;
screen.setBackgroundColor(Color.BLACK);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
if (isBlack){
screen.setBackgroundColor(Color.WHITE);
isBlack = false;
}
else{
screen.setBackgroundColor(Color.BLACK);
isBlack = true;
}
}
});
}
}
Related
I have in an activity some inputs required to make a drawable shape. This is a little example of it:
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.OVAL);
gd.setColor(Color.RED);
gd.setCornerRadius(12);
preview.getLayoutParams().width = 100;
preview.getLayoutParams().height = 100;
preview.setBackground(gd);
But when I want to implement it into a spinner, I have to choose like all the options multiple times so I can get the result. And also there is a button which should show the preview, doesn't work.
This is my code:
import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import yuku.ambilwarna.AmbilWarnaDialog;
public class CustomizationActivity extends AppCompatActivity {
int currentColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization);
final EditText widthText = findViewById(R.id.width);
final EditText heightText = findViewById(R.id.height);
final Spinner shapeSpinner = findViewById(R.id.shape);
final View colorView = findViewById(R.id.color);
final View preview = findViewById(R.id.preview);
final EditText size = findViewById(R.id.size);
final EditText corners = findViewById(R.id.corners);
Button button = findViewById(R.id.button);
final GradientDrawable gd = new GradientDrawable();
shapeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
gd.setShape(GradientDrawable.RECTANGLE);
gd.setCornerRadius(Integer.parseInt(corners.getText().toString()));
preview.getLayoutParams().width = Integer.parseInt(widthText.getText().toString());
preview.getLayoutParams().height = Integer.parseInt(heightText.getText().toString());
}
if (position == 2) {
gd.setShape(GradientDrawable.OVAL);
preview.getLayoutParams().width = Integer.parseInt(size.getText().toString());
preview.getLayoutParams().height = Integer.parseInt(size.getText().toString());
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
colorView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(CustomizationActivity.this, currentColor, false, new AmbilWarnaDialog.OnAmbilWarnaListener() {
#Override
public void onCancel(AmbilWarnaDialog dialog) {
}
#Override
public void onOk(AmbilWarnaDialog dialog, int color) {
currentColor = color;
colorView.setBackgroundColor(color);
gd.setColor(color);
}
});
dialog.show();
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preview.setBackground(gd);
}
});
}
}
It is somehow working but I don't know how to correct it. What I want is - if it possible - to show a preview directly when a value is changed. - And if not, after editing everything, get the result on button click.
I don't know but I think the problem is with the spinner.
Any help please?
Try to call in your onclick() method after setting property call gd.invalidateSelf();
I used below code to close app after back button pressed. Some time ago it worked but I tried to use it again and have:
Error:(88, 13) error: class, interface, or enum expected.
If I remove this code app can be build, I don't see where is problem?
Here is the MainActivity where onBackPressed is implemented:
package com.example.chab.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.os.Handler;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.a);
ImageView image1 = (ImageView) findViewById(R.id.b);
ImageView image2 = (ImageView) findViewById(R.id.c);
ImageView image3 = (ImageView) findViewById(R.id.d);
ImageView image4 = (ImageView) findViewById(R.id.e);
ImageView image5 = (ImageView) findViewById(R.id.f);
ImageView image6 = (ImageView) findViewById(R.id.g);
Picasso.with(this).load("http:/1.jpeg").into(image);
Picasso.with(this).load("http://1.jpeg").into(image1);
Picasso.with(this).load("http://1.jpeg").into(image2);
Picasso.with(this).load("http://1.jpeg").into(image3);
Picasso.with(this).load("http://1.jpeg").into(image4);
Picasso.with(this).load("http://1.jpeg").into(image5);
Picasso.with(this).load("http://1.jpeg").into(image6);
Button btnOne = (Button) findViewById(R.id.Btn);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Activitydwa.class);
startActivity(intent);
}
});
}
} //THIS BRACKET MUST BE MOVED TO THE END OF CODE!
private Boolean exit = false;
#Override
private void super.onBackPressed() {
if (exit) {
this.finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
EDIT:
Solved.
Bracket before Boollean must be moved to the end of code. Then all works. Thank you.
Your first problem is that you have your method implemented outside of the class. In java, methods need to belong to a class, interface, or enum. Double check your brackets and move your method to the inside of your class brackets. Your second problem is that you have the wrong signature for the onBackPressed method. See the code below:
This is what you have:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
}
private Boolean exit = false;
#Override
private void super.onBackPressed() {
// ...
}
This is what you need:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
private Boolean exit = false;
#Override
public void onBackPressed() {
// ...
}
}
Try to put
super.onBackPressed()
before finish()
Try this.finish() OR
Try super.onBackPressed(); OR
call NavUtils.navigateUpFromSameTask(this); to get back to previous screen/ activity
Android. i am a newbie I have 6 images. One image view, two buttons previous and next. Have written the program, but when executing, crashes. I have written the logic of how to change the images in image view, I have removed all the syntax errors. here is my Mainactivity.java code
package com.example.hm2_koppineedi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imageView;
Button previous,next;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
imageView= (ImageView)findViewById(R.id.imageView1);
previous = (Button)findViewById(R.id.button1);
previous.setOnClickListener((OnClickListener) this);
next = (Button)findViewById(R.id.button2);
next.setOnClickListener((OnClickListener) this);
}
public void onClick(View view)
{
int a=0;
switch (view.getId())
{
case R.id.button2:
if (a == 0)
{
imageView.setImageResource(R.drawable.hdimage);
a = 1;
}
else if (a == 1)
{
imageView.setImageResource(R.drawable.elephant);
a = 2;
}
else if (a == 2)
{
imageView.setImageResource(R.drawable.giraffe);
a = 3;
}
else if (a == 3)
{
imageView.setImageResource(R.drawable.horse);
a = 4;
}
else if (a == 4)
{
imageView.setImageResource(R.drawable.lion);
a = 5;
}
else if (a == 5)
{
imageView.setImageResource(R.drawable.tiger);
a = 6;
}
else if (a == 6)
break;
case R.id.button1:
a--;
button2.performClick();
next.performClick();
break;
}
}
}
Wrong code:
previous.setOnClickListener((OnClickListener) this);
next.setOnClickListener((OnClickListener) this);
Use this:
previous.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
);
I think you need to override the OnClick method, and make sure it is in your activity.
public class myActivity extends Activity {
...
#Override
public void onClick(View v) {
// Do something
}
...
}
You should not have to cast "this", but call...
setOnClickListener(this);
...from inside one of your activity methods (for instance : OnCreate), so that "this" be the activity. That way, all calls to "Click" in the context of your activity will be sent to your OnClick method.
You have lots of examples on this site to show that.
I'm beginner in android and I wrote the following simple program that just shows the text of the button I press on a TextView (I always get a runtime error):
package com.hmz.secondapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AppPageActivity extends Activity {
Button btnPlus;
Button btnMinus;
Button btnMul;
Button btnDiv;
TextView resultTextView;
View.OnClickListener operations = new View.OnClickListener() {
public void onClick(View v) {
Button pressedButton = (Button) v;
String buttonLabel = pressedButton.getText().toString();
resultTextView.setText(buttonLabel);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_page, menu);
return true;
}
}
Your btnPlus object is null. You need to instantiate it in the onCreate method like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMinus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMul = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnDiv = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
Also instantiate the resultTextView object before you use setText method.
From your code the problem is seems that you are not initializing the buttons (btnPlus,btnMinus,btnMul,btnDiv) and the TextView resultTextView.
btnPlus = (Button)findViewById(Button Id);
btnMinus = (Button)findViewById(Button Id);
btnMul = (Button)findViewById(Button Id);
btnDiv = (Button)findViewById(Button Id);
resultTextView = (Button)findViewById(TextView Id);
First initialize these Views then you can apply any operation on these views like Click Events and setting the text.
Trying to create a game that kids chase the marble. I want to have the buttons background change only if another buttons background equal something. But can't get the onclick with the a if statement to work. Here is the code I currently have.
package test.tablet.design;
import java.text.DateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MarbleGame extends Activity {
private Button btnA1mc; private Button btnA2mc; private Button btnA3mc; private Button btnA4mc; private Button btnB1mc; private Button btnB2mc; private Button btnB3mc; private Button btnB4mc;
private Button btnC1mc; private Button btnC2mc; private Button btnC3mc; private Button btnC4mc; private Button btnD1mc; private Button btnD2mc; private Button btnD3mc; private Button btnD4mc;
private Button btnE1mc; private Button btnE2mc; private Button btnE3mc; private Button btnE4mc; private Button btnF1mc; private Button btnF2mc; private Button btnF3mc; private Button btnF4mc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marble_chase);
TextView tv_time = (TextView) findViewById(R.id.tv_time);
String currenttime = DateFormat.getDateTimeInstance().format(new Date());
tv_time.setText(currenttime);
btnA1mc = (Button)findViewById(R.id.btnA1mc); btnA2mc = (Button)findViewById(R.id.btnA2mc); btnA3mc = (Button)findViewById(R.id.btnA3mc); btnA4mc = (Button)findViewById(R.id.btnA4mc);
btnB1mc = (Button)findViewById(R.id.btnB1mc); btnB2mc = (Button)findViewById(R.id.btnB2mc); btnB3mc = (Button)findViewById(R.id.btnB3mc); btnB4mc = (Button)findViewById(R.id.btnB4mc);
btnC1mc = (Button)findViewById(R.id.btnC1mc); btnC2mc = (Button)findViewById(R.id.btnC2mc); btnC3mc = (Button)findViewById(R.id.btnC3mc); btnC4mc = (Button)findViewById(R.id.btnC4mc);
btnD1mc = (Button)findViewById(R.id.btnD1mc); btnD2mc = (Button)findViewById(R.id.btnD2mc); btnD3mc = (Button)findViewById(R.id.btnD3mc); btnD4mc = (Button)findViewById(R.id.btnD4mc);
btnE1mc = (Button)findViewById(R.id.btnE1mc); btnE2mc = (Button)findViewById(R.id.btnE2mc); btnE3mc = (Button)findViewById(R.id.btnE3mc); btnE4mc = (Button)findViewById(R.id.btnE4mc);
btnF1mc = (Button)findViewById(R.id.btnF1mc); btnF2mc = (Button)findViewById(R.id.btnF2mc); btnF3mc = (Button)findViewById(R.id.btnF3mc); btnF4mc = (Button)findViewById(R.id.btnF4mc);
btnE2mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
btnE2mc.setBackgroundResource(R.drawable.marble_x);
btnB4mc.setBackgroundResource(R.drawable.marble);
}
});
btnB4mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (btnE2mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnB4mc.setBackgroundResource(R.drawable.marble_x);
btnC1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
btnC1mc.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnC1mc.setBackgroundResource(R.drawable.marble_x);
btnF1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
}
}
I would have an internal boolean set to true whenever the button is clicked and the background is changed.
boolean isBackgroundChanged = false;
Whenever the button is clicked, you change the background and set isBackgroundChanged to true.
And then, instead of doing:
btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)
you would have
if(isBackgroundChanged)...
You're trying to compare the Button object with a Drawable object, which will always return false.
You might instead want to use View.setTag() to track the state for each individual button. With a tag (named or unnamed) you can associate arbitrary data with an individual View. In the example below, auto-boxing is used to convert booleans into Booleans.
if ((Boolean)btn1.getTag()) {
btn1.setTag(false);
btn2.setTag(true);
// TODO actual background swapping
}
For readability and reducing duplication, you might want to use a helper function that can do the swapping for you, which will take two Buttons as arguments.