I have created a splash screen with code,
Activity:
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(Splash.this));
}
public class MyView extends View {
Bitmap splashBmp;
public MyView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
try {
int x = getHeight();
int y = getWidth();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
splashBmp = BitmapFactory.decodeResource(getResources(), R.drawable.splash);
splashBmp = Bitmap.createScaledBitmap(splashBmp, (y),(x), true);
canvas.drawBitmap(splashBmp,0, 0, paint);
} catch (Exception e) {}
}
}
}
<style name="splash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowDisablePreview">false</item>
</style>
AndroidManifest file:
<activity
android:name=".Splash"
android:noHistory="true"
android:theme="#style/splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The problem is app always takes 3 or 4 seconds to launch splash screen(without white screen) after the launcher icon click. How to solve this??
I have tried both xml and Canvas.
Here is most robust way to start splash with 0 second delay.
You have to set an theme only to your splash activity. You will set an
background instead of using layout on Splash Screen.
<activity
android:name=".SplashActivity"
android:theme="#style/SplashTheme">
...
</activity>
Put this style in your styles.xml
<style name="SplashTheme" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">#drawable/splash_bitmap</item>
</style>
Here splash_bitmap can be any drawable png, jpg, or xml drawable.
Here is mine you can make yours inside res>drawable directory.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" />
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_launcher" />
</item>
</layer-list>
Related
Trying to create simple application with fullscreen video playing.
Navigation bar hides, but activity is not filling the screen.
What am I missing?
activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="my.test.app2.VViewer">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout >
public class VViewer extends AppCompatActivity {
private VideoView myVideo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vviewer);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_FULLSCREEN
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
);
}
#Override
protected void onResume ()
{
super.onResume();
myVideo=(VideoView)findViewById(R.id.videoView);
myVideo.setVideoPath(MP4FileName);
myVideo.start();
}
theme for activity
<style name="AppTheme.FullscreenTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
you can try this :
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
My code works fine for 16:9 screen devices. Problems only on 18:9 devices.
I don't understand why, but adding
<meta-data android:name="android.max_aspect" android:value="2.1" />
to application section of android manifest file solves the problem.
I want to remove title and ActionBar on top of my splash activity. I tried the below code,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mSplashThread.start();
but its not working
anyone can help , why this does not work?
// try this way
//add to AndroidManifest for SplashScreen
<activity
android:name="<YOUR_PACKAGENAME.ACTIVITY>"
android:theme="#style/AppTheme.NoActionBar"
.........../>
//add this styles to styles.xml
<style name="AppTheme.NoActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowDisablePreview">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#null</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
//add this code to Activity
public class SplashScreen extends AppCompatActivity {
.
.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
.
.
.
}
}
I have the code as below in my Android app.
My res/values/styles.xml:
<style name="FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
res/drawable/background_splash.xml:
<item
android:drawable="#color/colorPrimaryDark"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/image1"/>
</item>
AndroidManifest.xml:
<activity
android:name=".SplashActivity"
android:theme="#style/FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
SplashActivity.java:
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
I have the image1 with appropriate resolution/size present in the res folder. But the splash screen does not display the image covering full screen but displays with reduced size only some part of the screen. Can you please advice what needs to be done. I went through some of the suggestions present on some sites but it did not help.
In your res/drawable/background_splash.xml, change android:gravity
you can use fill, in my case I used top|fill_horizontal for horizontal fill only, choose the combination you want.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/White"/>
<item>
<bitmap
android:gravity="top|fill_horizontal"
android:src="#drawable/splash"/>
</item>
</layer-list>
use parent of theme
parent="#style/Theme.AppCompat.Light.NoTitleBar.Fullscreen"
code for activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
tools:context=".SplashActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</android.support.constraint.ConstraintLayout>
Code for Splash.java
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashActivity.this,MainActivity.class);
startActivity(i);
finish();
}
},2000);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image" />
</LinearLayout
And use
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
in your Activity code.
Make sure your splash xml file has no padding of any sort. If any, delete.
I want to open activity as dialog
public class MoveActivity extends Activity {
private ListView list;
private DocAdapter adapter;
private ArrayList<Dictionary<String, String>> mlist;
DatabaseHelper helper;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move);
list = (ListView) findViewById(R.id.list);
helper = ApplicationClass.getDatabaseHelperInstance();
helper.open(DatabaseHelper.readMode);
mlist = helper.getDocList();
helper.close();
adapter = new DocAdapter(this, mlist);
list.setAdapter(adapter);
}
}
For using action bar in Dialog, you have to create a custom theme in your style.xml with parent as "Theme.AppCompat.Light".
<style name="PopupTheme" parent="Theme.AppCompat.Light">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>
And add this style in your manifest with activity tag:
<activity
android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden|locale"
android:screenOrientation="portrait"
android:theme="#style/PopupTheme" >
and last add this code in your activity before setConytentView(layoutID);
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = this.getWindow().getAttributes();
params.alpha = 1.0f;
params.dimAmount = 0.5f;
this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// This sets the window size, while working around the IllegalStateException thrown by ActionBarView
this.getWindow().setLayout(600,900);
setContentView(R.layout.dialog_move);
}
to Start activity as dialog i defined in AndroidManifest.xml
<activity android:theme="#android:style/Theme.Dialog" />
in the manifest file define theme for activity as dialog.
<activity android:theme="#android:style/Theme.Dialog" />
I want to make a splash screen in my application, for that i need to know how to show an image in full screen. This could me made by XML or Java code ? And how?
For now i just made this:
public class SplashScreen extends Activity {
private static final int STOPSPLASH = 0;
private static final long SPLASHTIME = 5000;
private Handler splashHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
Intent intent = new Intent(SplashScreen.this, jetpack.class);
startActivity(intent);
break;
}
super.handleMessage(msg);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
How can be this splash_screen.xml ?
Thank you for your help.
The best way to show a full screen splash activity is by putting this line in your manifest under activity tag
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"
You can use other themes as well
Theme.Black.NoTitleBar.Fullscreen
Theme.NoTitleBar.Fullscreen
<activity
android:name="com.example.SplashActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
For Latest Api Lollipop and above
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Define this theme for the splash activity and provide it in manifest
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image" />
</LinearLayout>
and add the below code before setContentView(R.layout.splash_screen);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
If R.layout.splash_screen included an image with height and width set to fill_parent or match_parent (depending on version). It will fill the screen
<style name="FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
just using super.onCreate and setConntentView
code :
/** Hiding Title bar of this activity screen */
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
/** Making this activity, full screen */
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);