So i have a OnClickListner, in here i would like to check the alpha state of a vector img.
p1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (/*aplpa*/ == 1) { //<----- alpha here
if (array[0] == 'o') {
p1.setBackgroundResource(R.drawable.ic_red);
array[0] = 'x';
} else {
p1.setBackgroundResource(R.drawable.ic_green);
array[0] = 'o';
}
array[1] = 1;
p1.setAlpha(1);
}
}
});
You shouldn't make your logic depend on the visible state of the app. Instead you should have some variable in your app which is what you set the alpha to when needed, and which you can check when you want.
Related
tutorial: JSON Get data from MySql
I followed this tutorial and everything went good. Even though the error not thrown, my app doesn't crash. But how to disable button after view the last array in JSON?
public void onClick(View v) {
if (v == nextBtn) {
if(TRACK<marks.length()){
TRACK++;
}
showData();
}
if (v == prevBtn) {
if(TRACK>0){
TRACK--;
}
showData();
}
}
EDIT : 1
Try below code:
public void onClick(View v) {
if (v == nextBtn) {
if(TRACK<marks.length()){
if(TRACK==0)
{
//Enable previous button because you are incrementing the count
prevBtn.setEnabled(true);
}
TRACK++;
if(TRACK==(marks.length()-1))
{
//Disable next button
v.setEnabled(false);
}
}
showData();
}
if (v == prevBtn) {
if(TRACK>0){
if(TRACK==(marks.length()-1))
{
//Enable next button because you are decrementing the count
nextBtn.setEnabled(true);
}
TRACK--;
if(TRACK==0)
{
//Disable previous button
v.setEnabled(false);
}
}
showData();
}
}
EDIT 2:
for making prevButton disabled by default do it like this, Where you are doing findViewById()
prevButton = ()findViewById(); // Your findViewById code..!!
prevButton.setEnabled(false);
Hope this helps..!!
I have one TextView in my application and want to change the Background color of the same TextView .When i click 1st time it would be red , click on same 2nd time it would be green and 3rd time click it would be blue color background by problematically.
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Drawable d = textType.getBackground();
Log.e("textType "," click !!! ");
if(d.getConstantState() == getResources().getDrawable(R.drawable.red_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.green_circle_shape);
}
if(d.getConstantState() == getResources().getDrawable(R.drawable.green_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.blue_circle_shape);
}
if(d.getConstantState() == getResources().getDrawable(R.drawable.blue_circle_shape).getConstantState())
{
textType.setBackgroundResource(R.drawable.red_circle_shape);
}
}
});
This cod is not working.Thanks to appropriate.
Create a global variable x initialize it with 0. Then code like this:
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if(x<4)
{
x= x+1;
}
else{
x = 1;
}
if(x==1)
{
// red color
}
else if(x==2)
{
// blue color
}
else if(x==3)
{
// green color
}
}
});
Use the below code,
textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
private int mCounter = 0;
#Override
public void onClick(View v)
{
if (mCounter == 0)
v.setBackgroundResource(R.drawable.red_circle_shape);
else
if (mCounter == 1)
v.setBackgroundResource(R.drawable.green_circle_shape);
else
v.setBackgroundResource(R.drawable.blue_circle_shape);
mCounter++;
}
});
Try this;
Define a global Variable int type.
Inside your onClick(), increment the int Variable
use switch or if statement to change the color when the variable increases e.g.
If(variable == 1)
// change color to Blue
else if (variable == 2)
// change color to Yellow
Hi Use the following code to Change the Color. Paste these lines inside your textView Onclick. Variable count is a global variable.
if (count == 0)
txtView.setTextColor(colorcode1);
else if (count == 1)
txtView.setTextColor(colorcode2);
else
txtView.setTextColor(colorcode3);
count++;
if (count > 2)
count = 0;
Try below code
TextView textType = (TextView)findViewById(R.id.textRNG);
textType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(current)
{
case 1:
tv.setBackgroundColor(Color.parseColor("#00ff00"));
current = 2;
break;
case 2:
tv.setBackgroundColor(Color.parseColor("#0000ff"));
current = 3;
break;
case 3:
tv.setBackgroundColor(Color.parseColor("#ff0000"));
current = 1;
break;
default:
break;
}
}
});
How can i change background color of my layout by clicking on a Button ?
This is my code :
Button color_change;
LinearLayout layout;
int value = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
color_change = (Button)findViewById(R.id.color_btn);
layout = (LinearLayout)findViewById(R.id.LL);
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value == 1) {
layout.setBackgroundColor(Color.RED);
}
else if (value == 2) {
layout.setBackgroundColor(Color.BLUE);
}
else if (value == 3) {
layout.setBackgroundColor(Color.MAGENTA);
}
else if (value == 4) {
layout.setBackgroundColor(Color.DKGRAY);
value = 0;
}
value++;
}
});
But i want replace if else condition with other code , because this code is complex and i want short code.
So any one suggest me, how can i short my code ?
You can try this
put all color into a array and in onclick method get particular color from the array and set it like
int color[]=new int[]{Color.BLUE,Color.RED,Color.GRAY};
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (value <color.length) {
layout.setBackgroundColor(color[value]);
}
value++;
}
});
There is no magic. Use the switch instead of else ifs. For the added clarity, you might consider having constants or something as values to value, so that you don't have to deal with hard-coded integers.
You could use an array to store the data:
int[] colors = {Color.RED, Color.BLUE, Color.BLACK};
int index = value % colors.length;
layout.setBackgroundColor(colors[index]);
value++;
You will use like this...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
switch(value)
{
case 1:layout.setBackgroundColor(Color.RED);
break;
case 2:layout.setBackgroundColor(Color.BLUE);
break;
case 3: layout.setBackgroundColor(Color.MAGENTA);
break;
case 4:layout.setBackgroundColor(Color.DKGRAY);
value = 0;
break;
}
value++;
}
});
A list of colors you want to use
List<int> colorsList = new List<int>();
colorsList.add(Color.RED);
colorsList.add(Color.WHITE);
colorsList.add(Color.BLUE);
colorsList.add(Color.GREEN);
//here you can add other colors to list
Iterator<int> colorIterator = colorsList.iterator();
function to get next color
int getNextColor()
{
if(colorIterator.hasNext())
{
return colorIterator.next();
}
else
{
colorIterator = colorsList.iterator();
return colorIterator.next();
}
}
and here is your listener
color_change.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int color = getNextColor();
layout.setBackgroundColor(color);
}
});
finishing up an android app, I have four methods each pertaining to their respective buttons. each is a color. if one is pressed, and its the correct color being told to be pressed it gets a +point, else it gets a -point.
these are the 4 methods im trying to combine into one, though I am having trouble figuring out that if i did, then I wouldnt have a way to to assign negative points. I was thinking that if i did like if blue, add points, else if teal add points etc... but this approach takes away from the fact that if they were told to press blue and pressed teal then it wouldnt register to add a -point.
here is the code:
public void blue_pressed(View view) {
correct = (TextView) findViewById(R.id.right);
incorrect = (TextView) findViewById(R.id.wrong);
if (count != 0 && !(count >NUMBER_ROUNDS) && color == blue) {
cor++;
correct.setText(getString(R.string.num_cor, cor));
} else {
inc++;
incorrect.setText(getString(R.string.num_inc, inc));
}
start_pressed(view);
}
public void teal_pressed(View view) {
correct = (TextView) findViewById(R.id.right);
incorrect = (TextView) findViewById(R.id.wrong);
if (count != 0 && !(count > NUMBER_ROUNDS) && color == teal) {
cor++;
correct.setText(getString(R.string.num_cor, cor));
} else {
inc++;
incorrect.setText(getString(R.string.num_inc, inc));
}
start_pressed(view);
}
public void purp_pressed(View view) {
correct = (TextView) findViewById(R.id.right);
incorrect = (TextView) findViewById(R.id.wrong);
if (count != 0 && !(count > NUMBER_ROUNDS) && color == purp) {
cor++;
correct.setText(getString(R.string.num_cor, cor));
} else {
inc++;
incorrect.setText(getString(R.string.num_inc, inc));
}
start_pressed(view);
}
public void pink_pressed(View view) {
correct = (TextView) findViewById(R.id.right);
incorrect = (TextView) findViewById(R.id.wrong);
if (count != 0 && !(count > NUMBER_ROUNDS) && color == pink) {
cor++;
correct.setText(getString(R.string.num_cor, cor));
} else {
inc++;
incorrect.setText(getString(R.string.num_inc, inc));
}
start_pressed(view);
}
thanks!
This question is really more about refactoring than Android.
You have 4 methods that differ only regarding the colour in the if statement. This means if you pass in the colour as a parameter, you would have only one method instead, and maintain only that. But you cannot change the signature of the "onClick" methods, so what to do? You create an additional private helper method that you call in all your four "onClick" methods.
In Eclipse you can extract easily the body of one of the "onClick" methods and refactor it into your helper method. Enter the shortcut: alt+command+M. Then change the signature of the extracted method manually to include an int parameter to compare color to. Finally you get...
private void buttonPressed(View view, int col){
correct = (TextView) findViewById(R.id.right);
incorrect = (TextView) findViewById(R.id.wrong);
if (count != 0 && !(count >NUMBER_ROUNDS) && color == col) {
cor++;
correct.setText(getString(R.string.num_cor, cor));
} else {
inc++;
incorrect.setText(getString(R.string.num_inc, inc));
}
start_pressed(view);
}
public void blue_pressed(View view) {
buttonPressed(view, blue);
}
public void teal_pressed(View view) {
buttonPressed(view, teal);
}
public void purp_pressed(View view) {
buttonPressed(view, purp);
}
public void pink_pressed(View view) {
buttonPressed(view, pink);
}
How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}