I made animation for background colors of the text views like these:
final TextView textView1 =
(TextView)findViewById(R.id.leftleft);
Resources res = getResources();
final TransitionDrawable crossfaderView1 =
getTransitionDrawableFromColors(R.color.green, R.color.lime);
textView1.setBackground(crossfaderView1);
chooseChangeColors.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v) {
if (somethingElseIsPressed == true) {
crossfaderView1.reverseTransition(timeForTransition);
}
else {
crossfaderView1.startTransition(timeForTransition);
}
somethingElseIsPressed = !somethingElseIsPressed;
}
});
But I want to make text in these textViews also changing its own color and text itself.
How can I achieve this?
Related
ivSwapImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
frameFromX = llFrom.getX();
frameFromY = llFrom.getY();
frameToX = llTo.getX();
frameToY = llTo.getY();
if (isSwap) {
llFrom.setX(frameToX);
llFrom.setY(frameToY);
llTo.setX(frameFromX);
llTo.setY(frameFromY);
isSwap = false;
} else {
llFrom.setX(frameToX);
llFrom.setY(frameToY);
llTo.setX(frameFromX);
llTo.setY(frameFromY);
isSwap = true;
}
}
});`
Above write to change Two Linear layout swap to each other position.but need to add animation on that layouts
try this. how add an animation to llFrom and llTo LinearLayout?
I have a question regarding android development. I am trying to make an app that changes the screen color from red to blue and then green. I have asigned an onClickListener to a relativeLayout, that turns the screen from white to red. How do I make that same onClickListener do multiple things in an order, so that when I click the screen once it turns it to red and when I click it again it will turn the screen to blue etc.
Here is my code so far:
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.view);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layout.setBackgroundColor(Color.parseColor("#ff0000"));
}
});
Store the list of desired colors in an array. Keep track of the current color index and increment after each click like so:
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.view);
layout.setOnClickListener(new View.OnClickListener() {
String[] colors = new String[]{"#ff0000", "#00ff00", "#0000ff"};
int colorIndex = 0;
#Override
public void onClick(View view) {
String color = colors[colorIndex];
colorIndex = colorIndex++ % colors.length;
layout.setBackgroundColor(Color.parseColor(color));
}
});
The modulo operator (%) ensures we don't index past the end of the array.
track the state of your click. Like this
private int colorState =0;
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.view);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch(colorState){
case 0:
layout.setBackgroundColor(Color.parseColor("#ff0000"));
break;
case 1:
layout.setBackgroundColor(Color.parseColor("#000000"));
break;
case 2:
layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
break;
colorState++;
}
I have noticed a strange behavior of setColorFilter(). I have some buttons in my activity and when the user clicks on one of them, if a certain condition is true I want to set a green background to it, otherwise red. This doesn't work. However in another method, where there isn't any condition, the same statement works.
I have this problem only on ICS while on Lollipop all works well. I solved this by using a drawable to set the background, but I would like to understand why there is this problem.
First method:
private void setButtons() {
buttonList = new ArrayList<>();
/*buttons initialization*/
for (int i = 0; i < btnNumber; i++) {
buttonList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button btn = (Button) v;
String buttonText = btn.getText().toString();
if (buttonText.equals(getAnswer())) {
//set background to GREEN
btn.getBackground().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.GREEN), PorterDuff.Mode.MULTIPLY);
}
else {
//set background to RED
btn.getBackground().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.RED), PorterDuff.Mode.MULTIPLY);
}
}
});
}
}
Second method in the same class:
private void revealAnswer() {
String answer = getAnswer();
for (int i = 0; i < btnNumber; i++) {
Button currentBtn = buttonList.get(i);
String currentText = (String) currentBtn.getText();
if (currentText.equals(answer)) {
//set background to GREEN
buttonList.get(i).getBackground().setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.GREEN), PorterDuff.Mode.MULTIPLY);
break;
}
}
}
I want to change the font and colour of the title in dialog box, I want change font, size, and colour, what should I do?
here is my code,
ivworknggroup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(Ourwork.this);
dialog.setContentView(R.layout.nggroup);
dialog.setTitle("N.G.GROUP");
TextView tvnggroup1 = (TextView) dialog.findViewById(R.id.tvnggroup1);
TextView tvnggroup2 =(TextView)dialog.findViewById(R.id.tvnggroup2);
Typeface typeFace1 = Typeface.createFromAsset(getAssets(),"fonts/antennalight.ttf");
tvnggroup1.setTypeface(typeFace1);
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/antennabold.ttf");
tvnggroup2.setTypeface(typeFace);
tvnggroup2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.nggroupindia.com/"));
startActivity(browserIntent);
}
});
dialog.show();
}
});
can any one help me?
thank u.
Well i had a similar situation once, but this is what got it solved for me.
Dialog sortDialog = new Dialog(getApplicationContext());
sortDialog.setContentView(R.layout.adtype_alertdialog);
sortDialog.setTitle("N.G.GROUP");
int dividerId = sortDialog
.getContext()
.getResources()
.getIdentifier("android:id/titleDivider", null,
null);
if (dividerId != 0) {
View divider = sortDialog.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(
R.color.yellow));
}
TextView tv = (TextView) sortDialog
.findViewById(android.R.id.title);
if (tv != null) {
tv.setTextColor(getResources().getColor(R.color.yellow));
}
sortDialog.show();
android:id/titleDivider & android.R.id.title in an identifier found in the alert_dialog.xml in your SDK folder
you should use custom view for title of your dialog maybe this link help you
how to include custom title view with in AlertDialog in android?
try this way
private String HALLOWEEN_ORANGE = "#FF7F27";
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Message").show();
setTitle("Title").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(10);//to change font size
textView.setTextColor(Color.RED); // to change color
//to change font family
Typeface face = Typeface.createFromAsset(getAssets(),"font/fontFileName.ttf");
textView.setTypeface(face);
Currently I am displaying two texts in the activity.
e.g.: "Group 1" and "Group 2".
I let user select the text (just like a button).
I need to find out whether the above displayed text is selected or not and then change the background color.
Here is the code I use for that. tv.isSelected() always evaluates to 'false'. Can any body tell me what I am doing wrong.
Is the "isSelected()" method used for a different purpose than how I use it?
TextView textView = new TextView(this);
textView.setText("Group 1");
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView tv = (TextView) view;
if (tv.isSelected()) {
tv.setBackgroundColor(Color.BLUE);
} else {
tv.setBackgroundColor(Color.WHITE);
}
}
});
linearLayout.addView(textView);
Try to do the following:
if (tv.getId() == textBox1Id) {
firstSelected = true;
} else { firstSelected = false; }
Try this
OnCreate()
{
TextView textView = new TextView(this);
textView.setText("Group 1");
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setSelected(true);
}
});
if (textView.isSelected()) {
textView.setBackgroundColor(Color.BLUE);
} else {
textView.setBackgroundColor(Color.WHITE);
}
linearLayout.addView(textView);
}
You could use a boolean variable to handle this:
private boolean tvSelected = false;
Your onClick method would change:
TextView textView = new TextView(this);
textView.setText("Group 1");
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (tvSelected) {
textView.setBackgroundColor(Color.WHITE);
tvSelected = false;
else {
tv.setBackgroundColor(Color.BLUE);
tvSelected = true;
}
});
linearLayout.addView(textView);
In terms of isSelected() usage, according to the API documentation:
A view can be selected or not. Note that selection is not the same as
focus. Views are typically selected in the context of an AdapterView
like ListView or GridView; the selected view is the view that is
highlighted.