Animating LinearLayout background to blink in Android - 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.

Related

How to display three list view in loop one by one automatic?

I want to display three ListView in loop one by one, with different data after 30 sec interval change to next list view and auto scroll in each and every list view.
Any help regarding this would be really appriciated!
You firstly need to loop statement in which you'll loop through the handler. Then you'll set a delay using the handler (note I've set the delay for 30000ms which is equivalent to 30 seconds; therefore, adjust the figure to your requirement).
for (int i = 0; i < 3; i++) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i == 0){
//This will be the first loop
}else if(i == 1){
//This will be your second loop
} else{
//This will be the third and final loop
}
}, 30000);
}
You should write a method to set your listview and trigger that method within the run() function. But you can customize that functionality based on your own requirements/how you best understand it. Additionally, because you are going to use different parameters for the listview, you'll have to go through if statements to determine at which part of the loop you are as this will determine which listview is going to be set now.
For multiple view types you can use RecyclerView with multiple view types.
Recycler view has more uses over ListView.
Please check example https://www.journaldev.com/12372/android-recyclerview-example

Images and Checkboxes with the Microsoft Band 2

I am trying to create a tile on the Microsoft Band 2 using Android Studio. I was wondering if it is possible to add images to a button like I would be able to on an android phone. My other question is about checkboxes. Are there checkboxes on the band? If not is there another way to get similar functionality? I need users to be able to click multiple things for a single question. Any help would be greatly appreciated.
For the checkboxes I would do a layout where you have a small text button next to a larger text button within a layout. When the large text button gets clicked, call an update function from your receiver that changes the text of the smaller button (possibly an asterisk or some other character that looks like a bullet point and it seems to appear and disappear). For example, your update function could look like this (slight modification from the example tile code given in the SDK):
private final int bulletTextId = 12;
private final int textButtonId = 21;
private boolean isActiveBullet = false;
private void onButtonClicked(int clickedID) {
switch (clickedID) {
case textButtonId:
String text = "";
isActiveBullet = !isActiveBullet;
if (isActiveBullet) text = "*";
try {
client.getTileManager().setPages(tileId,
new PageData(pageId1, 0)
.update(new TextBlockData(bulletTextId, text))
.update(new TextButtonData(textButtonId, "Text Button")));
} catch (BandIOException e) {e.printStackTrace();}
break;
default:
Log.e("", "Unknown button press received");
}
}
For multiple buttons you might need a map of button to boolean and switch the corresponding ones. If you can't figure that out, comment and I'll follow up.
Originally I was thinking it would make sense to change the background color, but that doesn't seem to be supported by the sdk.
As for using an image for the background, I don't think that is currently supported, just from looking at the function definitions in the sdk source code, but I would actually love to know that for sure as well.
Edit: I found this shortly after posting. It appears you can use bitmaps as masks, but I am not sure how to do that. Hopefully someone will come along and tell us because I would like to know too :)
"8.5.1
Icons Used as
FilledButton
Masks
By defining an Icon bitmap that acts as a mask and
then
superimposing that Icon over a
FilledButton
(see
Negative
Margins
)
, you can create the effect of the Icon image becoming visible when the button is pressed. That is, the
Icon bitmap
is defined to
have transparent pixels for the desired image, and opaque pixels els
e
where
. When the
user presses the
FilledButton
, the
FilledButton
color changes
but
shows through
only
the
transparent portions of
the Icon bitmap. "
And here is other relevant code if you want it:
private PageLayout createButtonLayout() {
return new PageLayout(
new ScrollFlowPanel(15, 0, 260, 105, FlowPanelOrientation.HORIZONTAL)
.addElements(new TextBlock(0, 0, 20, 45, TextBlockFont.MEDIUM)
.setMargins(5, 0, 0, 0)
.setId(bulletTextId))
.addElements(new TextButton(0, 0, 190, 45).setMargins(5, 0, 0, 0)
.setId(textButtonId).setPressedColor(Color.BLUE))
);
}
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == TileEvent.ACTION_TILE_OPENED) {
} else if (intent.getAction() == TileEvent.ACTION_TILE_CLOSED) {
}
/* ***** THIS IS THE ONLY EVENT WE ACTUALLY CARE ABOUT ***** */
else if (intent.getAction() == TileEvent.ACTION_TILE_BUTTON_PRESSED) {
TileButtonEvent buttonData = intent.getParcelableExtra(TileEvent.TILE_EVENT_DATA);
appendToUI("button is " + isActiveBullet + " ");
onButtonClicked(buttonData.getElementID());
}
}
};

How to display 3 2 1 text in the center of the screen in transparency mode

First of all, I am new to android development and searched everywhere but didn't get any solution to my problem. So please don't rate this post negative.
I am making a small game application in which I want to display 3 2 1 at the center of the screen in bixtext, and it should come in transparency mode such that the activity below the
3 2 1 text should be visible and when it reaches 1 then the activity should start like in any game.
Add a TextView to your xml layout (textView123 here) and that wouldn't be so hard to go:
final Handler handler = new Handler();
final TextView textView = (TextView) findViewById(R.id.textView123);
final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3);
final Runnable counter = new Runnable() {
#Override
public void run() {
textView.setText(Integer.toString(n.get()));
if(n.getAndDecrement() >= 1 )
handler.postDelayed(this, 1000);
else {
textView.setVisibility(View.GONE);
// start the game
}
}
};
handler.postDelayed(counter, 1000);
As for making the Countdown activity transparent - getWindow().setBackgroundDrawable(null).
What you can do is when you launch the timer to countdown, when it finishes the 1 countdown, create a transition to the next activity with a fade-in transition.
Though i don't understand why you don't use a frameLayout and the countdown timer as just a view on the same activity. That would certainly be a better approach.

Android:- change the text on buttons sequentially using animation

I have a sequence of buttons in android and I need to change the text of those buttons sequentially (slowly one by one) How can I achieve this? I cant apply any animation here?
Its really hard to tell exactly
what you want to do here but one option could be to use a TimerTask You could repeat changing the text of the buttons at whatever intervals you want. You could use a for loop to iterate over the buttons
Handler myHandler = new Handler();
// your buttons something like this
int[] ButtonArray = {R.id.button1,R.id.button2.....};
String[] stringArray = {"Hi","Hello","oi"....};
// get all the button
private Button[] myButtons=new Button[buttonArray.length];
for(int i = 0; i < buttonArray.length ; i++){
myButtons[i] = (Button) findViewById(mAlphabetsId[i]);
}
//Handler to do repetitive task
.................
Start The repetitive task
counterValue = 0;
Size =buttonArray.length; //Number of buttons
myHandler.postDelayed(mMyRunnable, speed);
.............................
private Runnable mMyRunnable = new Runnable()
{
public void run()
{
if(counterValue<Size){
myButtons[counterValue].setText(stringArray[CounterValue]);
myHandler.postDelayed(mMyRunnable, 1000); //Call again with 1 sec delay
counterValue++;
}else{
myHandler.removeCallbacks(mMyRunnable);
counterValue=0;
}
}
};
This code may contain errors since I did it in a hurry. Try it first.
Let me know if you get stuck.

Android - background change from Java code

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))

Categories

Resources