I am displaying one activity in this one image view is there and only image is display to the user. user will not allow to do any task such as like or comment.
the screen will on 5 seconds hold.
after that controls on that screen will be enabled and workable
even user does not allow to go back from the screen till the 5 seconds will not be completed.
how do i implement this functionality
I have written the code
public class AdvertActivity extends Activity implements OnClickListener {
ImageView ivEAdvertUserImage,ivEAdvertImage;
TextView tvEAdvertDislike,tvEAdvertLike,tvEAdvertHeading;
EditText etEAdvertComments;
Button btnEAdvertSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert);
InitUI();
tvEAdvertLike.setOnClickListener(this);
tvEAdvertDislike.setOnClickListener(this);
btnEAdvertSubmit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tvEAdvertLike:
tvEAdvertLike.setTextColor(color.Blue);
break;
case R.id.tvEAdvertDislike:
tvEAdvertDislike.setTextColor(color.Blue);
etEAdvertComments.setEnabled(true);
btnEAdvertSubmit.setEnabled(true);
break;
case R.id.btnEAdvertSubmit:
Intent in =new Intent(AdvertActivity.this,EAdFragment.class);
startActivity(in);
finish();
break;
default:
break;
}
}
public void InitUI(){
ivEAdvertUserImage=(ImageView)findViewById(R.id.ivEAdvertUserImage);
ivEAdvertImage=(ImageView)findViewById(R.id.ivEAdvertImage);
etEAdvertComments=(EditText)findViewById(R.id.etEAdvertComments);
tvEAdvertDislike=(TextView)findViewById(R.id.tvEAdvertDislike);
tvEAdvertLike=(TextView)findViewById(R.id.tvEAdvertLike);
tvEAdvertHeading=(TextView)findViewById(R.id.tvEAdvertHeading);
btnEAdvertSubmit=(Button)findViewById(R.id.btnEAdvertSubmit);
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advert);
InitUI();
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// Will be called after 5000ms
tvEAdvertLike.setOnClickListener(this);
tvEAdvertDislike.setOnClickListener(this);
btnEAdvertSubmit.setOnClickListener(this);
}
}, 5000);
}
This way your Listeners will be set after 5 seconds. Before that, the buttons etc. will have no effect if someone clicks them.
(Can not test my code right now, but it should work)
Related
this is my Splash Screen, If I press home or multitasking/appswitch button when Intent is started app crash, in logcat is FATAL EXEPTION: Thread-1277. Can I kill/delete this Intent when player press home button?
public class SplashScreen extends Activity {
private static int loadingTime = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.loading_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, loadingTime);
}
}
The following code tracks whether the SplashActivity is at least partially showing. If yes, it will continue to MainActivity. If not (activity is finished by pressing Back button, activity is stopped by pressing Home button) nothing happens.
This solution uses Fragments so the timing is preserved across e.g. screen orientation changes (it will always take specified time no matter how many times you rotate your device - the timer will not reset).
public class SplashActivity extends Activity {
// tracks when the activity is at least partially visible (e.g. under a dialog)
private boolean mStarted = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your current code
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_startup);
if (savedInstanceState == null) {
// first time onCreate, create fragment which starts countdown
getFragmentManager()
.beginTransaction()
.add(SplashFinishFragment.newInstance(), SplashFinishFragment.TAG)
.commit();
} else {
// fragment already set up from first onCreate after screen rotation
}
}
#Override
protected void onStart() {
// the activity becomes at least partially visible
mStarted = true;
super.onStart();
}
#Override
protected void onStop() {
// the activity is no longer visible
mStarted = false;
super.onStop();
}
public boolean isStarted2() {
// there is already hidden method isStarted() in the framework
// you can't use it and are not allowed to override it
return mStarted;
}
public static class SplashFinishFragment extends Fragment {
private static final String TAG = SplashFinishFragment.class.getSimpleName();
private static final int DELAY = 1000; // one second delay
private static final Handler mHandler = new Handler(); // one main thread anyway
private final Runnable mRunnable = new Runnable() {
#Override
public void run() {
if (getActivity() == null) {
// this should never happen, there is no activity, so no fragment
Log.e(TAG, "No activity!");
return;
}
SplashActivity a = (SplashActivity) getActivity();
if (a.isStarted2() || a.isChangingConfigurations()) {
// if activity is even partially visible or is rotating screen right now, continue
Intent i = new Intent(a, SettingsActivity.class);
a.startActivity(i);
}
// in any case close splash
a.finish();
}
};
public static SplashFinishFragment newInstance() {
return new SplashFinishFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the countdown will continue (not reset) across screen rotations
setRetainInstance(true);
// try running the main activity after specified time
mHandler.postDelayed(mRunnable, DELAY);
}
#Override
public void onDestroy() {
// if the fragment gets destroyed (e.g. activity closes) do not launch main activity
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}
}
This was tested on a virtual Galaxy S2. It works when Home or Back button is pressed. It doesn't work when Recent Apps button is pressed. I don't know your use case but personally I would expect the app to continue launching while I browse recent apps.
I'm making an activity where I'm planning to display a progress bar and move to a second activity after five seconds. I used Thread.sleep to do this.
Here is the code for the activity with the spinner.
public class WaitScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.waitscreen);
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
try{
for(int i=0;i<5;i++){
Thread.sleep(1000);
}
Toast.makeText(getApplicationContext(), "Spinner complete!", Toast.LENGTH_LONG).show();
}
catch(InterruptedException e){
}
Intent i = new Intent("emergency.app.NEWACTIVITY");
startActivity(i);
}
}
My main activity only has a button that leads to this page on being clicked. Problem is, it displays the main activity for five seconds, then this activity very briefly and then the third activity. How do I make the thread start after WaitScreen is fully loaded? As you can see, I tried using the onStart method to do the trick. Or if you could tell me another way to introduce a five second delay, that would be helpful too!
Use a Handler to post a Runnable, like so:
public class WaitScreen extends Activity {
private Runnable task = new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Spinner complete!", Toast.LENGTH_LONG).show();
Intent i = new Intent("emergency.app.NEWACTIVITY");
startActivity(i);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waitscreen);
Handler handler = new Handler();
handler.postDelayed(task, 5000);
}
}
The Runnable task will execute after 5 seconds.
http://developer.android.com/reference/android/os/Handler.html
I have a splash screen which displays for 2-3 sec before it disappears.
I want to add a Fade in Effect when the next activity is loaded. I saw an Example in the Facebook Hacker Example and i am using it.
It uses a finish(); to end that activity to so from the DashboardActivity if some one clicks back it doesnt return back to the SplashAcitivty. But using this doesnt create the Fade in Effect as show in the API demos Examples.
public class SplashActivity extends Activity {
private long splashDelay = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
/*
* this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
* WindowManager.LayoutParams.FLAG_FULLSCREEN);
*/
setContentView(R.layout.activity_splash);
TimerTask task = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent().setClass(SplashActivity.this,
MainActivity.class));
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
Use a handler for this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,
MainActivity.class));
overridePendingTransition(R.anim.fade, R.anim.hold);
finish();
}
}, splashDelay);
Can it possible to make a button on screen which automatically visible and gone every 5sec interval?
By using this
b.setVisibility(View.VISIBLE);
we can visible and
b.setVisibility(View.GONE);
we can hide it.But I can't manage to make it by usin the time interval.
Any idea?please share.
There are a few different ways, one is a Handler and Runnable:
public class Example extends Activity {
private Handler mHandler = new Handler();
private Runnable alternate = new Runnable() {
public void run() {
// Alternate visible and not
mHandler.postDelayed(alternate, 5000);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler.postDelayed(alternate, 5000);
}
}
Use this
new CountDownTimer(9000000, 5000) {
public void onTick(long millisUntilFinished) {
if(b.getVisibility() == View.GONE)
b.setVisibility(View.VISIBLE);
else
b.setVisibility(View.GONE);
}
public void onFinish() {
//Restart timer if you want.
}
}.start();
hi i am doing one app here when i click button that time i need to display image.that image should be visable in 5sec.after 5 sec after that image should need to be invisable.i trieb but i am not getting after 5 sec how to invisable that image.any one suggest me. i using below code.
Demo1 .class
public class Demo1 extends Activity {
/** Called when the activity is first created. */
Button b1;
ImageView i1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.homebutton);
i1=(ImageView)findViewById(R.id.imageView1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
i1.setVisibility(View.VISIBLE);
}
});
}
}
use handler.postDelayed method to delay some operation to some time, so for your operation use following:
i1=(ImageView)findViewById(R.id.imageView1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
i1.setVisibility(View.VISIBLE);
Handler.postDelayed(new Runnable(){public void run(){ i1.setVisibility(View.INVISIBLE);}, 5000);
}
});