android rotate layout with handler - android

i try to rotate layout with handler.i wrote some code
void handlechange() {
Handler hand = new Handler();
hand.postDelayed(new Runnable() {
#Override
public void run() {
change();
}
private void change() {
myImage.setRotation(5);
handlechange();
}
}, 500);
}
i can rotate layout but i want to rotate layout every time. 500 milisecond.
setRotate working only one time.
how i can solve my problem? if anyone knows solution please help me

This will rotate any view you want. Will increment 5 to it's rotation every 500ms. Change the values to what you want.
public void rotateImage(final View myView) {
final Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
#Override
public void run() {
myView.setRotation(myView.getRotation() + 5);
rotateImage(myView);
}
}, 500);
}
Notice the myView.getRotation() + 5. Otherwise you're setting the rotation to 5 every time.

Related

Can I slide an android viewpager within a different time slot?

I want to slide my an android viewpager within a different slide slot.It means first page comes after the 5 second and second page will be appearing in 8 second.I need to slide viewpager but that slides are need to come within different time frame.is it possible to do that thing? Any help to slow this error would be highly appreciated.
I did the following code segment.But it will change viwepager for some constant time period.
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == signageResourceStoreModelList.size()) {
currentPage = 0;
}
try {
viewPager.setCurrentItem(currentPage, true);
} catch (Exception e) {
e.printStackTrace();
}
currentPage = currentPage + 1;
}
};
timer = new Timer();
timer.schedule(new TimerTask() { // task to be scheduled
#Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
}
Yes, definitely you can do this by some programming logic.
Declare a class variable
for eg. int time=2000;
To change the view inside viewpager programmatically
public void MoveNext(View view) {
pager.setCurrentItem(pager.getCurrentItem() + 1);
//Write logic of incrementing time here
//e.g.time=time+1000;
}
Now define a handler
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MoveNext();
}
},time);

Refresh activity without any animation

I want to refresh an activity which will change a textview value after every 5 second without any animation.i searched in stackoverflow also but can't get proper solution.please help
Im not sure if this answers your question but would it work if set the text in the textView every 5 seconds? Using the txtView.setText("your text"); and then have a delay in between each time you set it? I hope this works and makes sense
You can do it with a Handler. Here's how :
private boolean started = false;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textview.setText("####"); // <- text to be updated every 5 sec
if(started) {
start();
}
}
});
}
};
public void stop() {
started = false;
handler.removeCallbacks(runnable);
}
public void start() {
started = true;
handler.postDelayed(runnable, 2000);
}

Hide TextView after some time in Android

I want to hide TextView after some time interval say 3 seconds. I googled and found some code and I tried code as shown below but It is not working.
Please tell me what is the wrong with this ?
tvRQPoint.setText("+0");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setText("+0");
}
}, 3000);
One more thing, how to remove timeout ? As I am using this on click event of ListView, If user clicks on one option and then clicks on second option, then as 3 seconds got over (after clicked on first option), It does not show second option for 3 seconds.
try View INVISIBLE or GONE like:
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setVisibility(View.INVISIBLE);
}
}, 3000);
Set View visibility with view.setVisibility(View.INVISIBLE|View.VISIBLE|View.GONE);
How about hiding your text view with some animation?
int delayMillis = 3000;
Handler handler = new Handler();
final View v = tvRQPoint; // your view
handler.postDelayed(new Runnable() {
#Override
public void run() {
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
v.startAnimation(animate);
v.setVisibility(View.GONE);
}, delayMillis);
What you are trying to do is ok but after three seconds you want to hide the textview so use setVisibility
tvRQPoint.setText("+0");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setVisibility(View.INVISIBLE);
}
}, 3000);
try this...
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.tv);
tv.setText("+0");
tv.postDelayed(new Runnable() {
public void run() {
tv.setVisibility(View.INVISIBLE);
}
}, 3000);
}
}
I simply animated the View from alpha 1 to 0, meaning visibility from 100% to 0%.
scrollUp = findViewById(R.id.lottieAnimationView);
scrollUp.postDelayed(new Runnable() {
#Override
public void run() {
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(400);
scrollUp.startAnimation(alphaAnimation);
scrollUp.setVisibility(View.INVISIBLE);
}
},5000);
I am using the LottieAnimationView but same is applicable with any other view
just replace the id in R.id.{ID OF YOUR VIEW}
alphaAnimation.setDuration(400); This means the animation will take 4 seconds to complete i.e higher this number the slower the animation.
},5000);
and the "5000" means 5 seconds delay.
For every second of delay you put it as 1000 ms or milliseconds of delay.
1 Seconds = 1000 ms
Hope it helps someone, all other answers are correct as well, this is just another way of doing it.
Try this-
Handler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
tvRQPoint.setText("+0");
handler = new Handler();
handler.postDelayed(csRunnable, 3000);
}
Runnable csRunnable =new Runnable()
{
#Override
public void run()
{
tvRQPoint.setVisibility(View.INVISIBLE);
}
};
You are setting the text in run() method. you can hide the text in two ways
View.INVISIBLE - give the space for the textview and hide its content
View.GONE - remove the space for textview and hide its content
so call
tvRQPoint.setVisibility(View.INVISIBLE);
(or)
tvRQPoint.setVisibility(View.GONE);
hope it's work:
tvRQPoint.setText("+0");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
tvRQPoint.setVisibility(View.GONE);
}
},3000);

Image view image need to change periodically in android

I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.
Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};

Hide A Layout After 10 Seconds In Android?

I have a layout displayed on a button click.I want to hide that layout after 10 seconds.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mVolHandler = new Handler();
mVolRunnable = new Runnable() {
public void run() {
mVolLayout.setVisibility(View.GONE);
}
};
}
private OnTouchListener mVolPlusOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mVolLayout.setVisibility(View.VISIBLE);
mVolHandler.postDelayed(mVolRunnable, 10000);
}
}
Make use of Handler & Runnable.
You can delay a Runnable using postDelayed of Handler.
Runnable mRunnable;
Handler mHandler=new Handler();
mRunnable=new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
yourLayoutObject.setVisibility(View.INVISIBLE); //If you want just hide the View. But it will retain space occupied by the View.
yourLayoutObject.setVisibility(View.GONE); //This will remove the View. and free s the space occupied by the View
}
};
Now inside onButtonClick event you have to tell Handler to run a runnable after X milli seconds:
mHandler.postDelayed(mRunnable,10*1000);
If you want to cancel this then you have to use mHandler.removeCallbacks(mRunnable);
Update (According to edited question)
You just need to remove callbacks from Handler using removeCallbacks()
So just update your code inside onTouch method like this :
mVolLayout.setVisibility(View.VISIBLE);
mVolHandler.removeCallbacks(mVolRunnable);
mVolHandler.postDelayed(mVolRunnable, 10000);
You can use an Animation started when you click the button, with 10 seconds duration that fades out the layout and probably sets its visibility to GONE at the end.
You can use postDelayed:
val delay = 3000L // 3 seconds
view.postDelayed({ view.visibility = View.GONE }, delay)
Use Handler to hide layout after 10 seconds. Use Post delayed method
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
pd=ProgressDialog.show(this,"","Loading, Please wait .. ",true);
setTitle("set title");
final Handler uiThreadCallback=new Handler();
final Runnable runInUIThread= new Runnable(){
public void run(){
fnDraw();
pd.dismiss();
}
};
new Thread(){
#Override public void run(){
uiThreadCallback.post(runInUIThread);
}
}.start();
}
public void fnDraw(){
setContentView(R.layout.define ur xml if any);
i1=(ImageView)findViewById(R.id.i1);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
timerRegister=new Timer();
lIteration=0;
checkTime=new TimerTask(){
public void run(){
if(lIteration==1){
timerRegister.cancel();
uiDrawThreadCallback.post(runInUIDrawThread);
}
lIteration++;
return;
}
};
timerRegister.scheduleAtFixedRate(checkTime,0,10000);
}
final Handler uiDrawThreadCallback=new Handler();
final Runnable runInUIDrawThread= new Runnable(){
#Override
public void run(){
fnDrawAgain();
}
};
public void fnDrawAgain(){
Intent intent=new Intent(this,new class you want to open.class);
startActivity(intent);
}
}
try it m sure it gonna work in ur on create screen

Categories

Resources