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);
}
Related
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.
I am trying to apply a frosty glass effect to the background image of an activity, but I don't know how to do this can anyone know or do this kind of application if yes then help me please.
Expected output:-
This is my code where I am using Blurry library but when the application run it does work as expected:-
public class SplashActivity extends AppCompatActivity {
long delay = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
Blurry.with(getApplicationContext())
.radius(25)
.sampling(2)
.async()
.animate(500)
.capture(findViewById(R.id.content))
.into((ImageView) findViewById(R.id.content));
Timer runSplash = new Timer();
TimerTask showSplash = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent(SplashActivity.this,MainActivity.class));
}
};
runSplash.schedule(showSplash,delay);
}
}
May be this library is helpful to you.
https://android-arsenal.com/details/1/2192
I'm a beginner in android development. I have a Fragment (FragA) in which I have a button that does a calculation and set the value to a variable (x) and pass this value to another fragment (FragB). Every time I press the button in FragA the value of "x" is passed to FragB.
Now, what I want is to do an action in FragB (refresh the view of a graph) each time the value of "x" is changed. If you need more information feel free to ask.
Thanks
I found a way to make a RealTime chart here. The important part is:
public class BH06_Graph_Frag extends Fragment{
private Runnable mTimer;
//i.e private LineGraphSeries<DataPoint> mSeries;
...
#Override
public void onResume() {
super.onResume();
mTimer = new Runnable() {
#Override
public void run() {
//reception of value "x"
//do something
// i.e mSeries.resetData(new DataPoint[] {
// new DataPoint(6, x) });
//
mHandler.postDelayed(this, 300);
}
};
mHandler.postDelayed(mTimer, 300);
}
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 want to make 2 animation on activity for 2 images, and I want to do it with 2 conditions:
1. I want to start the animation after the activity finished to load to page, so instead of putting animation code under onCreate i put it under onResume is that OK? There is a better way to do it?
2. I want the second animation will start only after the first animation is finished...
Thanks
You'll want to use an Animation.AnimationListner You can set one on your first animation that will get a callback when the animation is complete. Inside that callback you can add the code that will start the second animation.
Depending on the API level you are coding for you can use AnimationSet or AnimatorSet. Also if you are extending View or one of its subclasses you can override View.onAnimationStart() and View.onAnimationFinish(). Or use the listener Tim mentions.
public class SplashActivity extends Activity{
Animation FadeInanimation, FadeOutanimation;
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img= (ImageView) findViewById(R.id.img);
//Your Code Block....
FadeInanimation = AnimationUtils.loadAnimation(this,R.anim.image_fadein);
//FadeInanimation.setRepeatCount(Animation.INFINITE);
//FadeInanimation.setRepeatMode(Animation.RESTART);
FadeInanimation.setAnimationListener(FadeInAnimationListener);
FadeOutanimation = AnimationUtils.loadAnimation(this,R.anim.image_fadeout);
//FadeOutanimation.setRepeatCount(Animation.INFINITE);
//FadeOutanimation.setRepeatMode(Animation.RESTART);
FadeOutanimation.setAnimationListener(fadeOutAnimationListener);
img.startAnimation(FadeInanimation);
}
AnimationListener FadeInAnimationListener = new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
plane.startAnimation(FadeOutanimation);
}
};
}