Android - background change from Java code - android

my problem is, I have custom listView filled from Runnable returnRes. It fills particular data in layout I have named as lay (R.id.layoutList). My aim is to have different colour for each lay in my listView, I want to switch colours between each. 1st is dark blue, second light blue, thir dark blue and so on... This code is doing well, but with no result, my custom listView is still black, when I change it in XML, it is changing, but not when it is set from Java. Any ideas?
Thanks
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(myTasks != null && myTasks.size() > 0){
TasksAdapter.notifyDataSetChanged();
LinearLayout lay=(LinearLayout)findViewById(R.id.layoutList);
for(int i=0;i<myTasks.size();i++){
TasksAdapter.add(myTasks.get(i));
if(i>0){
if(i%2==0){
lay.setBackgroundColor(R.color.background);
}
}else{
if(i>0){
lay.setBackgroundColor(R.color.lightBlue);
}
}
}
}
ProgressDialog.dismiss();
TasksAdapter.notifyDataSetChanged();
}
};

Try googling. getResources().R.color.lightBlue is not the actual color, it's the id of
the color resource (which is an integer code for the color). It will work fine if you use it
in an API which expects ids of resources, but setBackgroundColor
actually needs the code of the color.
colors and ids are both just coded as int when you come down to it, so it's
really easy to confuse one for the other.

yourlayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.yourbackgroundimage))

Related

How can you change a Swipe refresh layout's color for each rotation?

Has anyone noticed the SwipeRefreshLayout in gmail. It changes color for every rotation in a single load iteration. Does anyone have any idea how to achieve it?
The documentation for SwipeRefreshLayout details the setColorSchemeResources() and setColorSchemeColors() methods that can be used to set the color scheme of your SwipeRefreshLayout
The docs state:
Set the color resources used in the progress animation from color resources. The first color will also be the color of the bar that grows in response to a user swipe gesture.
You should note that setColorSchemeColors() should be used with specific colour values and setColorSchemeResources() should be used when you intend to use resource references to colours (e.g. R.color.red).
Please also note that setColorScheme(), the old way of completing this, is now deprecated.
Change colors as per UI requirement:
swipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.YELLOW, Color.BLUE);
Here is how you can get the colors from the colors.xml resource and set them as SwipRefreshLayout's color scheme.
int c1 = getResources().getColor(R.color.refresh_progress_1);
int c2 = getResources().getColor(R.color.refresh_progress_2);
int c3 = getResources().getColor(R.color.refresh_progress_3);
mySwipeRefreshLayout.setColorSchemeColors(c1, c2, c3);
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.simpleSwipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
#Override
public void onRefresh() {
// implement Handler to wait for 3 seconds and then update UI means update value of TextView
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// cancle the Visual indication of a refresh
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.setColorSchemeResources(R.color.Aqua,R.color.Blue,R.color.Violet);
// Generate a random integer number
Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;
// set the number value in TextView
textView.setText(String.valueOf(i1));
}
}, 3000);
}
});
}
In my case the
setColorSchemeColors(R.color.primary), directly adding the color(Color.parse("#ffbea12")) instead of resource reference should work but I had to use the resources
and
setColorSchemeResources(R.color.primary) Both did not work as expected
Then I found that in colors.xml I had missing
<color name="colorPrimary">#color/primary</color>
So after adding it like this I GOT the desired color
you can use any of those either passing Colors values or Colors Resource Ids
Passing Colors
swipeRefreshLayout.setColorSchemeColors({COLOR VALUE},{COLOR VALUE },
any number of colors values..., {COLOR VALUE});
Passing Colors Resources Ids
swipeRefreshLayout.setColorSchemeResources( R.color.{your Color resource Id},
any number of colors ids... , R.color.{your Color resource Id})```

Changing color of radio button moment after

Ok so i have radiobuttons that repressent correct and incorrect answer. What would like is to turn color of that button to some color for a moment and then turn it right back to its default color that i have already defined but i do not know how to implement this default color(R.drawable.radiodefault) to appear and change back color of the radiobutton moment after R.drawable.tocan/R.drawable.netocan is shown
if(currentQ.getANSWER().equals(answer.getText()))
{
answer.setBackgroundResource(R.drawable.radiotocan);
}
else if(currentQ.getANSWER()!=answer.getText()){
answer.setBackgroundResource(R.drawable.radionetocan);
}`
The problem might be in your else if condition. You are comparing strings with !=
if(currentQ.getANSWER().equals(answer.getText())){
answer.setBackgroundResource(R.drawable.radiotocan);
} else if(!currentQ.getANSWER().equals(answer.getText())){
answer.setBackgroundResource(R.drawable.radionetocan);
}
If there are no additional conditions then it would be better to use else instead of else if.
As for changing it back to default you could use handler.
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
//change it back
answer.setBackgroundResource(R.drawable.radiodefault);
}
}, delayInMillis);

Android View.getBackgroundColor #XXXXXXXX

The background color of my View is #FFFFFFFF. I want to get this through code. I do not want to just put #FFFFFFFF into the method because I will be changing the background through code, so this value will change all the time.
public void toggleEraser() {
ImageView btnEraser = (ImageView) this.findViewById(R.id.imgEraser);
ImageView btnBrush = (ImageView) this.findViewById(R.id.imgBrush);
if (erase) {
btnEraser.setImageResource(R.drawable.greyeraser);
btnBrush.setImageResource(R.drawable.brush);
} else {
btnEraser.setImageResource(R.drawable.eraser);
btnBrush.setImageResource(R.drawable.greybrush);
}
erase = !erase;
if (erase){
//Here is the problem
drawView.setColor(//drawView.getBackgroundColor());
}
else
drawView.setColor(brushColor);
drawView.setErase(erase);
}
store the color you want 0xFFFFFFFF in somevariable and then
change drawView.setColor( to drawView.setBackgroundColor(somevariable);
Check out this answer: Get the background color of a button in android. it is a little different but can lead you on the right path.

Dynamically change text color of all items in ListView

I want to change my activity theme dynamically without recreating activity, the only solution I've found is to change items properties (like background, textColor) in place, but I have a ListView with number of items,
I can iterate through all items in ListView and change textColor, but I think it's kinda an ugly solution.
Is there any better way to achieve this?
Thank you in advance
You could do something like this in your Adapters getView() method:
int theme = getThemeColorFromPrefrences();
if(theme == darkTheme){
textView.setTextColor(...);
}
else{...}
Now every time the user changes the color, you just call invalidateViews() on your ListView:
// set other theme color [...]
myListView.invalidateViews()
I assume you have custom arraylist of object, on that put a private int color
class myobject {
private int color;
public void setColor(int color) { this.color=color; }
public int getColor { return color; }
}
on your custom ArrayAdapter List in getView() set
((TextView)view.findViewById(R.id.textview)).setColor(item.getColor());
//Where item is your MyObject at ArrayList(Position)
When you want to change color just, on your onClick button Black
onClick() {
for(int i=0;i<arraylist.size();i++) { arraylist.get(i).setColor(Color.Black); }
arrayadapter.notifydatachange();
}
You can use your own layout for the ListView rows, and that way you can specify the text color on the layout file.

Animating LinearLayout background to blink in Android

The question is very simple but I can't manage to find a good solution to it. I have a LinearLayout in my activity. Depending on what the user does I need to make the background of my Layout blink 3 times. This means it will change the Background Color from transparent to Red and backwards for 3 times. Let me give you an example:
the user receives a question and 2 buttons with answers
the user presses the wrong answer. The layout containing the button will change it's background (Transparent - Red, Transparent - Red, Transparent - Red - Transparen) three times.
How can I make this in Android ? Thank you.
You could use a Handler with the postDelayed method. That would look something like this:
Handler h = new Handler();
int count = 0;
Runnable r=new Runnable()
{
public void run()
{
if(count < 6){
if(count % 2 == 0){
count++;
layout.setBackground(RED);
h.postDelayed(r,500);
}else{
count++;
layout.setBackground(TRANSPARENT);
h.postDelayed(r,500);
}
}
}
};
h.post(r);
#vidstige: I have tried your suggestions but the background changing wasn't fluent, sometimes it worked sometimes only half sometimes it didn't
#Tim: your solution seems very good but I had some issues with it making it work and as I'm not really good at java I gave up.
The solution that works is a combination of principles of both answers. I'm not sure if it's the best one but works very good in my case
Handler blinkHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
lay1.setBackgroundColor(Color.RED);
break;
case 1:
lay1.setBackgroundColor(Color.TRANSPARENT);
break;
}
super.handleMessage(msg);
}
};
for (int i=0; i<6; i++)
{
Message msg = new Message();
if(i % 2 == 0){
msg.what = 0;
}
else{
msg.what=1;
}
blinkHandler.sendMessageDelayed(msg, i*300);
}
Thank you all for your time.
Use a ScheduledThreadPoolExecutor to schedule an action to change the background color using View.setBackgroundColor(int color) to red/transparent.
Using the schedule(Runnable command, long delay, TimeUnit unit) method you can schedule all the color changes at once and they will be executed later in the correct order.
To make a color blink I remember there was a specific bit we used to set in C++ graphics library, in University days. I'm also trying to blink the background but I don't want to dedicate a thread for that purpose.
And if there's no alternate at all, I'll try to put a .gif image with alternating colors in the background.

Categories

Resources