I am using a fragment and there is a button as soon as I click on it the background will become dim and textView will be visible.The appearance of the dimming effect and text will take place at once.For some reason I don't get these result.
Here is my code:-
activate_wifi_button = (Button)wifi_and_hotspot.findViewById(R.id.Activate_wifi);
activate_wifi_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
WindowManager.LayoutParams layoutParams=getActivity().getWindow()
.getAttributes();
layoutParams.dimAmount = 0.7f;
getActivity().getWindow().setAttributes(layoutParams);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
text.setVisibility(View.VISIBLE);
text_animate_dots.setVisibility(View.VISIBLE);
timer.cancel();
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(runnable);
}
}, 2000, 2000);
}
});
I have used handler and runnable because I want to animate the textview for 2 sec but the animate part can come latter at first I need to do the above task.
For the desired output, you can take semi-transparent layout and textView in FrameLayout. In onclick method, set its visibility VISIBLE; and for better user experience, set Fade animation to textview.
Related
I have my custom view that extends SurfaceView and here I have a spinner among others on it. I set its visibility to visible and invisible in turns but after one cycle of showing and hiding, next time it does not show up anymore. To verify what's wrong I draw on canvas text:
spiner3.getVisibility()
spiner3.getX()
spiner3.getY()
but nothing of these seem to be wrong I mean X and Y are on the within the screen and getVisibility returns View.VISIBLE. So what can be the other reason of not showing up if it is not visibility property and coordinations?
I did a workaround. In Activity that uses this view I added such code:
Handler mTimerHandler;
Timer timer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gra_layout);
vg = findViewById(R.id.content_frame);
refreshSpinner();
...
}
private void refreshSpinner() {
mTimerHandler = new Handler();
timer.schedule(new TimerTask() {
public void run() {
mTimerHandler.post(() -> {
vg.invalidate();
});
}
}, 0, 100);
}
I created new custom Button class, I want to achieve, whenever user go to any activity my generic button want to expand from circle to default width. While expanding I want to hide button text for while until button animation complete.
Please check my below code:
private void animateMe(Context context){
final String btnText = this.getText().toString();
final FIButton fiButton = this;
fiButton.setText("");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
fiButton.setText(btnText);
}
},500);
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.expand);
super.startAnimation(animation);
}
Easily by
ViewCompat.animate(fiButton ).setStartDelay(500).alpha(1).setDuration(700).setInterpolator(new DecelerateInterpolator(1.2f)).start();
note that you have to set the fiButton alpha to zero android:alpha="0.0"
in you xml or on create view
this line will animate your view from 0 to 1 in 700 millisecond after 500 millisecond.
You can use an AnimationListener. On finishing the animation you should do setText on the TextView.
private void animateMe(Context context){
final String btnText = this.getText().toString();
final FIButton fiButton = this;
fiButton.setText("");
Animation animation = AnimationUtils.loadAnimation(context, R.anim.expand);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
fiButton.setText("");
}
#Override
public void onAnimationEnd(Animation animation) {
fiButton.setText(btnText);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
super.startAnimation(animation);
}
I have used a service which posts code to a handler at an interval of 3 seconds to keep generating a simple touch event (a button press).
This is the service code:
public class BackgroundTouchService extends IntentService
{
public BackgroundTouchService() {
super("BackgroundTouchService");
}
#Override
protected void onHandleIntent(Intent intent)
{
final MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_BUTTON_PRESS, 400, 400, 0);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run()
{
View v = new View(getApplication());
v.onTouchEvent(event);
handler.postDelayed(this, 3000);
}
});
}
}
In my UI, I have covered the screen with buttons (with appropriate listeners) so that I can easily spot which button has been pressed. However, the main activity loads up and then nothing happens. Why is this?
EDIT:
As Vojtěch Sáze correctly pointed out, the handler in the above code is not associated with the main thread, and hence cannot be used to modify the UI by generating touch events. Hence, I wrote the code for the handler in the main activity itself:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTouch();
}
private void startTouch()
{
final MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_BUTTON_PRESS,400.0f, 400.0f, 0);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run()
{
onTouchEvent(event);
handler.postDelayed(this, 3000);
}
});
}
}
However, this still does not do anything. Any ideas?
SOLUTION:
Okay, so in the event type parameter of the MotionEvent.obtain method I had originally specified ACTION_BUTTON_PRESS, however, when I used to separate events to specify ACTION_DOWN and ACTION_UP separately, this seemed to work.
Any explanations as to why this happened are welcome.
In case someone needs this, here is the code:
final MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,400.0f, 400.0f, 0);
final MotionEvent event2 = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_UP, 400.0f, 400.0f, 0);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run()
{
dispatchTouchEvent(event);
dispatchTouchEvent(event2);
handler.postDelayed(this, 3000);
}
});
I think you need to use dispatchTouchEvent method to send the touch event to the view.
I'm not sure if this will help. But definitely there is problem with
final Handler handler = new Handler();
From documentation:
Default constructor associates this handler with the Looper for the current thread. If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.
But you don't have main thread in onHandleIntent(). You'll need to create the Handler in the main thread.
Just use code below. Using Android Handler, you need also looper for posting action.
final Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run()
{
onTouchEvent(event);
handler.postDelayed(this, 3000);
}
});
I am trying to update a textview to a value (a timer) within my current xml im viewing, the java for the timer is part of a glrenderer class, everything works fine, except I can't update my textview, I have already extended Activity to the class like this:
class MyGLRenderer extends Activity implements GLSurfaceView.Renderer {
So that I am able to use this inside the timer loop:
TextView view = (TextView) findViewById(R.id.timetextview);
view.setText(String.valueOf(globals.gametime));
It crashes when it tries to create the viewtime object.
There is one way I can get it to partially, the Timer value is a global one so I can show it by creating a button in the main.java like so:
public void buttonExample (View v)
{
TextView view = (TextView) findViewById(R.id.timetextview);
view.setText(String.valueOf(globals.gametime));
}
But with this it wont update with the actual timer, I'm unsure why its crashing when I'm using the top 2 blocks of code, it compiles fine.
I doubt , you are updating the Textview in timer thread .
You can try this in your timer thread -
runOnUiThread(new Runnable() {
public void run() {
view.setText(String.valueOf(globals.gametime));
}
});
Using the code Gaurav provided to access the uithread in my timer worked perfectly, here is what I used
public Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable() {
public void run() {
TextView view = (TextView) findViewById(R.id.timetextview);
view.setText(String.valueOf(globals.GameTime));
}
});
}
}, 250, 250);
I am working on a layout which shows a tab like structure on bottom of the layout. Which I need to show on double tap and then hide it after 5 sec. So I am using this countdown timer:
public void timer()
{
cdt=new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {
System.out.println("Timer Working"+millisUntilFinished+"");
}
#Override
public void onFinish() {
System.out.println("Finished");
main =(LinearLayout)findViewById(R.id.parent);
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)main.getLayoutParams();
mlp.height=420;
set_up_views();
find_module();
tl.setVisibility(View.INVISIBLE);
}
}.start();
}
But I dont know how to stop and restart this timer. How can I do?
I suggest you not to Use CountDownTimer for this case.
Use Handler.postDelayed(Runnable runnable, long delay)
public class yourActivity extends Activity
{
public Handler handler = new Handler();
...
public void hideAfter5Sec()
{
handler.postDelayed(new Runnable()
{
View view = findViewById(view_to_hide);
view.setVisibility(View.INVISIBLE);
}, 5000);
}
}
postDelayed will execute that code after 5Sec.
EDITED:
postDelayed will be call only once after 5 Sec through Lopper.loop(). If there are multiple call to hideAfter5Sec() then only you will get multiple call to postDelayed.
If you have multiple call hideAfter5Sec() i dont think there is any wrong because hideAfter5Sec() is just hidding it. so if it one or many your view will be hidden.
If in case you want to hide only in the last call of hideAfter5Sec() use this variant.
public class yourActivity extends Activity
{
public Handler handler = new Handler();
public long lastHideAfter5Sec = 0L;
...
public void hideAfter5Sec()
{
lastHideAfter5Sec = System.currentTimeMillis();
handler.postDelayed(new Runnable()
{
if(System.currentTimeMillis() - lastHideAfter5Sec < 5000)
return;
View view = findViewById(view_to_hide);
view.setVisibility(View.INVISIBLE);
}, 5000);
}