I'm trying to put a picture in my app but not really sure how.
Do I need a new activity for it? If so, how that activity will understand when to stop and go to the real program?
To clarify what I'm trying to do: When you open any app probably I think that they will have what I'm talking about. For example, the Youtube app when opened there will be a screen showing a the logo of Youtube before it opens the real app. I think this screen takes about 3 to 5 seconds.
Use splash activity as it was sais below. Here's example:
public class SplashActivity extends Activity {
private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 2; // Time in seconds to show the picture
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splash_screen); //your layout with the picture
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}}
And don't forget to add to the manifest
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It called Splash Screen
You should to create new activity which has a timer. After 1-2 seconds it launches your main activity.
How to implement Splash Screen in android
Related
I need some help. I have a problem with my app. I want to put a splash screen at the beginning. I have done it before. I have made the code, the layout, and all works perfectly in a new project! When I put the code I run it on my phone and the layout in my application, the application runs perfectly without any errors. But when I open it on my phone, it stops and it doesn't open it!!! Can you suggest something??
my android manifest.xml:
android:name=".activities.SplashScreenActivity"
android:label="#string/splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Assuming you want to launch Main activity from your SplashScreenActivity.
In your SplashScreenActivity's onCreate():
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
The first 2 lines launch Main activity through an Intent, and the 3rd line kills SplashScreenActivity so you cannot go back to it from MainActivity.
public class SplashScreenActivity extends AppCompatActivity {
private int SLEEP_TIMER = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
View imageView = findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);
imageView.startAnimation(animation);
getSupportActionBar().hide();
LogoLauncher logoLauncher = new LogoLauncher();
logoLauncher.start();
}
private class LogoLauncher extends Thread {
public void run() {
try {
sleep(1000 * SLEEP_TIMER);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(SplashScreenActivity.this,LoginActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}
This question already has answers here:
How do I make a splash screen? [closed]
(31 answers)
Closed 6 years ago.
I want to make a logo (an own activity)show in an own activity 3 seconds before the main activity loads, when starting my android app. Which is the simplest approach for doing this?
I have searched through this forum, I could only find one answered question around this topic, but unhappily it was unusefull for me.
I think what you're referring is how to implement a Splash screen,
Create a new empty activity, I'll call it Splash for this example;
public class SplashScreen extends Activity {
// Sets splash screen time in miliseconds
private static int SPLASH_TIME = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// run() method will be executed when 3 seconds have passed
//Time to start MainActivity
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent );
finish();
}
}, SPLASH_TIME);
}
}
Make sure you've set Splash activity as the launcher activity in your Manifest file :
<activity
android:name=".Splash"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have an activity that starts the system browser via Intent. Short before it does that I do a HTTP GET to some other URL. This GET will be answered as soon as the user finishes his task in the browser (logging in using OAuth).
I'd like to be able to close down the browser and / or get my application's activity back to the front.
I do not want to use a WebView because I'd like to avoid the perception that I might be trying to spy on passwords.
Any idea how to solve this? Is it possible at all?
Thanks a bunch!
Daniel
Make sure OAuth opens up URL which is something like yourapp://success
Next you add a intent filter to handle this custom protocol and address. More details are at http://developer.android.com/guide/topics/intents/intents-filters.html#ifs
Here's what did the trick in my project.
The application manifest is pretty standard:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Here's a code snippet of the background thread that sends the intent to re-show the activity:
public class AlarmThread extends Thread {
private int mSleepTime;
public AlarmThread(int sleepSeconds) {
super("AlarmThread");
mSleepTime = sleepSeconds * 1000;
}
#Override
public void run() {
Log.i("thread", "started sleeping for " + mSleepTime + " milliseconds");
try {
Thread.sleep(mSleepTime);
} catch (InterruptedException e) {
// ignored
}
Log.i("thread", "creating intent to bring activity to foreground");
Intent intent = new Intent(MainActivity.getContext(), MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.getContext().getApplicationContext().startActivity(intent);
}
}
Note that the trick is in the MainActivity.getContext().getApplicationContext().startActivity(intent); part (last line above).
In the MainActivity, I added the getContext method:
public static Context getContext() {
return mInstance;
}
And the member 'mInstance' is set in the 'onCreate':
private static MainActivity mInstance = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Other code....
mInstance = this;
}
i'm just starting out with Android and i think i'm missing something.
It seems like in Android you decide at development time which activity will be the first to be displayed in your application.
i would like to write my application in such a way that some kind of a centralized controller starts executing and it decides which activity should be first
(for example, based on some data obtained from somewhere)
is that possible to do, and if so, how?
thanks.
Most folks do it by launching an activity that just picks up the config it needs and then starts up the "real" activity. One hiccup is that the activity first launched will be on the task stack, but if you set android:noHistory="true" for the initial activity the process should be invisible to the user.
The below method can be used for showing tutorial screens on first app launch.
AndroidManifest.xml:
<activity android:name=".activities.LaunchActivity"
android:noHistory="true"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".onboarding.OnboardingActivity"/>
<activity android:name=".activities.MainActivity"/>
LaunchActivity.java:
public class LaunchActivity extends Activity {
public static final String FIRST_APP_LAUNCH = "com.your.package.name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstAppLaunch()) {
setFirstAppLaunch(false);
startActivity(new Intent(this, OnboardingActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
private boolean isFirstAppLaunch() {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
return preferences.getBoolean(FIRST_APP_LAUNCH, true);
}
private void setFirstAppLaunch(boolean value) {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_APP_LAUNCH, value);
editor.apply();
}
}
I wonder What so tough in this. in the main Activity in the onCreate Method after checking the data starting another activity without setting the view content of Main Activity.
public class check extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
void myonclick(View view)
{
Intent mIntent = new Intent(this,check2.class);
startActivity(mIntent);
}
}
class check2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Toast.makeText(
this,
"Welcome to second page", Toast.LENGTH_LONG).show();
finish();
}
}
Hi. This is my code when I run this. When I click a button it will show error in emulator: The Application Check has stopped unexpectedly.
Have you declared both the activities in manifest file ?
The main activity should have the following intent-filter tag
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The second activity to be declared as
<activity android:name="check2">
Also calling finish() in the second activity would immediately return the control to the first activity.
In the Manifest File declare Two Activities like
<activity android:name=".LoginForm" android:label=" Login"/>
Here FrontPage is first file name
Here LoginForm is Second file name
Then When FrontPage file onclick the button the event will fire
code for that
Intent userintent = new Intent(FrontPage.this, LoginForm.class);
startActivity(userintent);
finish();