I am using picasso with timer to change url of image in MainActivity oncreate() so first time i open application it doesn't work i navigate to other activity and return to main it works
i am printing 'test' in Runnable and it is working
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent_view = findViewById(android.R.id.content);
Image_view = findViewById(R.id.adv_image);
global = (GlobalVariable) getApplication();
db = new DatabaseManager(this);
db_user = new DatabaseUserManager(this);
final String [] url = {"https://png.pngtree.com/thumb_back/fh260/back_pic/00/03/20/63561dc0bf71922.jpg",
"https://placeit-assets.s3-accelerate.amazonaws.com/landing-pages/make-a-twitch-banner2/Twitch-Banner-Blue-1024x324.png",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQgYdaf-JhDiFVeQjL6ZRskiF1CRADiJfgDKI3PKBfCMrnnPcHP"};
final String [] paths = {"https://www.google.com/",
"https://www.youtube.com/",
"https://www.facebook.com/"};
Timer adtimer = new Timer();
adtimer.schedule(new TimerTask() {
int count = 0 ;
#Override
public void run() {
count++;
if(count >= url.length )
count = 0;
ActivityMain.this.runOnUiThread(new Runnable(){
#Override
public void run() {
System.out.println("test");
Picasso.get()
.load(String.format(url[count]))
.fit()
.into(Image_view);
}
});
Image_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uriUrl = Uri.parse(paths[count]);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
});
}
} , 200 , 5000);
Related
In NoteInfoactivity I have a code below, but
Note allNote = NoteDatabase.getInstance(getApplicationContext()).noteDao().getAllNoteId(noteID);
is executed in main thread. How can i execute it in background? what is the best way?
public class NoteInfoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_info);
TextView textViewTitle = findViewById(R.id.textViewNoteTitle);
TextView textViewPriority = findViewById(R.id.textViewPriority);
Intent intent = getIntent();
if (intent != null && intent.hasExtra("NoteID")) {
long noteID = intent.getIntExtra("NoteID", -1);
Note allNote = NoteDatabase.getInstance(getApplicationContext()).noteDao().getAllNoteId(noteID);
String title = allNote.getTitle();
int priority = allNote.getPriority();
textViewTitle.setText(title);
textViewPriority.setText(String.valueOf(priority));
} else {
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_SHORT).show();
}
}
}
You can put it in a thread and then call for a handler to do the UI changes on the main thread.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_info);
TextView textViewTitle = findViewById(R.id.textViewNoteTitle);
TextView textViewPriority = findViewById(R.id.textViewPriority);
Intent intent = getIntent();
Handler handler = new Handler();
if (intent != null && intent.hasExtra("NoteID")) {
long noteID = intent.getIntExtra("NoteID", -1);
new Thread(new Runnable() {
#Override
public void run() {
Note allNote = NoteDatabase.getInstance(getApplicationContext()).noteDao().getAllNoteId(noteID);
handler.post((Runnable) () -> {
String title = allNote.getTitle();
int priority = allNote.getPriority();
textViewTitle.setText(title);
textViewPriority.setText(String.valueOf(priority));
});
}
}).start();
} else {
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_SHORT).show();
}
}
Here my transition and reverse transition happens only once not 10 times, Can u tell me where I'm wrong ??
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++)
{
Resources res = getApplicationContext().getResources();
Drawable background[] = new Drawable[2];
background[0] = res.getDrawable(R.drawable.shapes);
background[1] = res.getDrawable(R.drawable.large_shape);
final TransitionDrawable transition = new TransitionDrawable(background);
ImageView imgview = findViewById(R.id.transitimage);
imgview.setImageDrawable(transition);
Runnable r1 = new Runnable() {
#Override
public void run() {
transition.startTransition(2000);
}
};
Runnable r2 = new Runnable() {
#Override
public void run() {
transition.reverseTransition(2000);
}
};
Handler h = new Handler();
h.postDelayed(r1, 0);
h.postDelayed(r2, 2000);
}
}
}
public void repeat() {
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
if (i>9) { // i is a field,its initial value equals 0
cancel(); // stop timer
}
if (flag) {
transition.startTransition(2000);
} else {
transition.reverseTransition(2000);
}
flag = !flag;
i += 1;
}
public void onFinish() {
repeat();
}
}.start();
}
Call above function in some place.
i am creating a sample app in which i want to change a TextVeiw value every second.
TextView value should change continue every second.
to perform this task i tried this below code:
but this code is not changing text values continue, it only change on app start or screen rotate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Boolean b = true;
final TextView tv = (TextView) this.findViewById(R.id.textView1);
final String[] str = new String[] { "897451", "34232", "33432",
"46867", "54554", "6756", "56r7", "2345u8", "9654", "987650", };
Random generator = new Random();
final int random = generator.nextInt(str.length);
Thread t = new Thread() {
#Override
public void run() {
try {
while (b != false) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// update TextView here!
tv.setText(str[random]);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
and i also tried this code:
but this code crash application
private Timer timer = new Timer();
private TimerTask timerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
final TextView tv = (TextView) this.findViewById(R.id.textView1);
final String[] str = new String[] { "897451", "34232", "33432",
"46867", "54554", "6756", "56r7", "2345u8", "9654", "987650", };
Random generator = new Random();
final int random = generator.nextInt(str.length);
timerTask = new TimerTask() {
#Override
public void run() {
// refresh your textview
tv.setText(str[random]);
}
};
timer.schedule(timerTask, 0, 1000);
}
Try the following code snippet
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
long millis =(long)currentTime();
dateAndTime.setText(getDate(millis, "dd/MM/yyyy hh:mm:ss.SSS"));
h.postDelayed(this, 1000);
}
});
final TextView tv = (TextView) this.findViewById(R.id.textView1);
final String[] str = new String[] { "897451", "34232", "33432",
"46867", "54554", "6756", "56r7", "23458", "9654", "987650", };
// //
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
Random generator = new Random();
final int random = generator.nextInt(str.length);
tv.setText(str[random]);
h.postDelayed(this, 1000);
}
});
// //
I have a strange problem. In my android application I have 10 ms timer running.
after long run(14 hours of run ) any how timer is causing change in device time.
Usually time changes 2 days ahead
public class MainActivity extends Activity {
TextView DateTextView;
TextView BatteryTextView;
String date;
Thread mUpdateUIThread;
public native void WatchDogTimeTest();
int[] m_array ;
Timer mTimerThread;
String StrDdate;
int n = 0;
public IntentFilter s_intentFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DateTextView = (TextView)findViewById(R.id.DatedisplayTxtVw);
BatteryTextView = (TextView)findViewById(R.id.BatteryStatus);
date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
DateTextView.setText(date);
s_intentFilter = new IntentFilter();
s_intentFilter.addAction(Intent.ACTION_DATE_CHANGED);
s_intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
s_intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
s_intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
this.registerReceiver(this.mBatInfoReceiver,s_intentFilter);
CreateMutipleThread();
TimerThread();
WatchDogTimeTest();
}
public void CreateMutipleThread()
{
for(int i = 0 ; i < 100 ; i++)
{
mUpdateUIThread= new Thread() {
#Override
public void run() {
try {
while (!isInterrupted())
{
Thread.sleep(100); //Check for Aux Battery Status After 10 Second
runOnUiThread(new Runnable()
{
#Override
public void run()
{
n++;
if(n >= 10000)
n= 0;
}
});
}
} catch (InterruptedException e) {
}
}
};
mUpdateUIThread.start();
}
}
public void TimerThread()
{
mTimerThread = new Timer();
//Set the schedule function and rate
mTimerThread.schedule(new TimerTask() {
#Override
public void run() {
StrDdate = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
}
},1000,1);
}
public void InvalidateTimer()
{
this.runOnUiThread(new Runnable() {
#Override
public void run() {
printTime();
}
});
}
public void printTime()
{
}
public BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent intent) {
final String action = intent.getAction();
//Date Change Broad cast
if(action.equals(Intent.ACTION_DATE_CHANGED))
{
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
DateTextView.setText(date);
DateTextView.setBackgroundColor(Color.RED);
BatteryTextView.setText(mydate);
}
}
};
}
Any idea how to solve this problem. This is my Test Code
I created a splash screen using the follow code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.splash_layout);
Thread splashThread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (InterruptedException e) {
// do nothing
} finally {
_active = false;
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}
};
splashThread.start();
}
there is an image view in splash_layout, after the splash screen appears for some time duration, and disappears then MyMainActivity starts, the problem is, after the splash disappears and before MyMainActivity starts, I could see previous screen(irrelevant to my app, e.g. desktop with widgets, or previous running app), how to make the transition fluent so that splash screen directly goes to MyMainActivity?
Thanks!
Can you try this i am not sure this is 100% work but try may be helpful..
protected int _splashTime = 3000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}, _splashTime);
}
Try calling finish() after startActivity().
private static final long SPLASH_SCREEN_MS = 2500;
private long mTimeBeforeDelay;
private Handler mSplashHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Create a new Handler.
mSplashHandler = new Handler();
}
#Override
protected void onResume() {
super.onResume();
// The first time mTimeBeforeDelay will be 0.
long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
if (gapTime > SPLASH_SCREEN_MS) {
gapTime = SPLASH_SCREEN_MS;
}
mSplashHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}, gapTime);
// Save the time before the delay.
mTimeBeforeDelay = System.currentTimeMillis();
}
#Override
protected void onPause() {
super.onPause();
mSplashHandler.removeCallbacksAndMessages(null);
}
For your reference, here is a best example for Android - Splash Screen example.
You can try this code:1
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
// interesting music
/**
* Gets your sound file from res/raw
*/
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
Best of luck!