I'm trying to create splash screen without action bar.
Firstly before created splash screen, action bar in main activity and when I create splash screen, action bar comes to splash screen and main activity is full screen. I searched method like getwindow(), getActionBar(), but when I use these method program says to me unfortunately stopped. So what I'm missing?
How can I avoid actionBar in splash screen?
My code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_item);
Thread th=new Thread(){
public void run(){
try {
sleep(4000);
Intent intent=new Intent(MainActivity.this,SplashScreen.class);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
th.start();
}
MANİFEST:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.SPLASHSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
First, import the support library in the top of your app:
import android.support.v7.app.ActionBarActivity;
and change your code as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_item);
getSupportActionBar().hide();
}
Using AppCompat for being supported for all versions:
<activity
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Put that before your setContentView(...), should get the job done.
For Android 3.0 or higher use ActionBarAPI#hide
For lower versions you will need to use Android Support Library.
Use ActionBar as
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
Ref docs
Also if your requirement in static, then you can choose a theme for your activity that dos not have actionbar such as
android:theme="#android:style/Theme.Holo.Light.NoActionBar"
You can do this as:
<activity android:theme="#android:style/Theme.Holo.Light.NoActionBar"
android:name=".name_here"
android:label="#string/app_name" >
use a style without actionbar, also in your splash screen activity java extend Activity and make the splash screen your MAIN activity and in this activity you call an Intent to open your MainActivity after some seconds
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:theme="#android:style/Theme.Holo.Light.NoActionBar"
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Example:
In your SplashScreen activity write this code. This will open your MainActivity after 2 seconds.
new Handler().postDelayed(new Runnable()
{
public void run()
{
Intent localIntent = new Intent(SplashScreen.this, MainActivity.class);
SplashScreen.this.startActivity(localIntent);
SplashScreen.this.finish();
}
}, 2000L);
This is the simplest method.
#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_main);
}
where R.layout.activity_main is replaced by your layout activity e.g. activity_splash
Related
It's my project situation now.
MainActivity is LAUNCHER
SplashActivity called MainActivity onCreate()
When I think about it, it looks like there is no problem.
but after app starting,
The MainActivity screen is briefly visible before SplashActivity call.
Surprisingly, I did not see it on other devices, only galaxy s8.
Of course, I know it is not a general structure. But I can not understand it because I have been working normally.
white color is cold start style and splashActivity.
red color is mainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some getIntent code
startActivityForResult(new Intent(this, SplashActivity.class), RESULTCODE_);
setInitLayout();
}
manifest
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
minSdkVersion 21
targetSdkVersion 28
If you use style with android:windowBackground for splash activity, don't call setContentView(). Thats all!
It is not a good idea to use a splash screen that way . This should be strictly avoided.
With this approach you may also lead the problem of blank white page appears during splash launching and this what exactly happened to you !
i advice you to read this article and try to make your splash screen in the right way to avoid such behavior !
You have placed MainActivity class to be the launcher activity in your manifest. I am assuming that your splashscreen activity is called SplashActivity. If you want it to be shown before as the SplashScreen and then MainActivity to come later on, change your manifest code to be:
<activity android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
<activity android:name=".IntroActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
Then create your SplashActivity as suggested by Ismail in the link that he has provided
simply add handler for limited seconds of time and finish current activity
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//actvity transaction
}
},3000);
Use this thread code for splash activity it will better work -
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread() {
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainAct);
finish();
}
}
};
thread.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
and your manifest class will be look like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.diskapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".Activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.MainActivity" />
</application>
</manifest>
Use this thread code for splash activity. It will better work :
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread() {
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainAct);
finish();
}
}
};
thread.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
I have starting building an app in Android studio. I have established the MainPage as the launcher activity in the manifest.xml.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainPage">
android:screenOrientation="portrait"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".courseSelect" />
<activity android:name=".profile1" />
<activity android:name=".stats1" />
<activity android:name=".ReviewRounds" />
<activity android:name=".ReferFriends" />
<activity android:name=".RangeMode" />
</application>
I have double checked that the run configuration is set to 'Default' and yet the app is running a different activity, entitled courseSelect. It is also not running some code correctly on the NumberPicker. Even though I've set the picker to have a min, max, and default, the picker only shows 0 and will not scroll. The two issues seem to be related somehow, in terms of what activity is being run.
this is the courseSelectCode:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;
import co.ceryle.segmentedbutton.SegmentedButtonGroup;
public class courseSelect extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_select);
//Hole Picker
NumberPicker holePicker = (NumberPicker)findViewById(R.id.holePicker);
holePicker.setMaxValue(18);
holePicker.setMinValue(1);
holePicker.setWrapSelectorWheel(false);
holePicker.setValue(1);
SegmentedButtonGroup sbg = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup);
sbg.setOnClickedButtonPosition(new SegmentedButtonGroup.OnClickedButtonPosition() {
#Override
public void onClickedButtonPosition(int position) {
// if(position == 0)
}
});
}
}
I tried to set the run configuration specifically to the MainPage activity and it still opens in the courseSelect Page.
EDIT: on request, here is my MainPage.java code:
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_select);
Window g = getWindow();
g.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
...
}
edit the Mainfest.xml, in order to enforce portrait layout there already:
<activity android:name=".MainPage"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...which makes this code merely useless (styles.xml can also be used for window styles):
Window g = getWindow();
g.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
also update setContentView( R.layout.activity_course_select ); to the proper resource.
because it starts the MainPage Activity, but then inflates the wrong XML file.
one "suggested edit" before was to swap the order of setContentView() and the paragraph below ...which I've rejected, because setting it in Manifest.xml appeared more organized (less code).
First of all There is a mistake in your manifest file
you wrote screenOrientation attribute outside the opening tag
<activity android:name=".MainPage">
android:screenOrientation="portrait"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
it should be
<activity
android:name=".MainPage"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and you used wrong xml to setContenView
Try this and error was in 9th line because your code line is outside of the tag:
<activity android:name=".MainPage">
android:screenOrientation="portrait" // error
Do this :
<activity android:name=".MainPage"
android:screenOrientation="portrait"> // After doing this no error
and do also this thing :
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_select); // error
do this :
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MAIN_PAGE_ACTIVITY_NAME); // no error
Your launcher activity is MainPage but you are calling the layout of courseselect activity inside MainPage activity's onCreate method on this line
setContentView(R.layout.activity_course_select);
Change it to your MainPage layout
setContentView(R.layout.yourMainPageLayout);
ManinActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I tried
requestWindowFeature(Window.FEATURE_NO_TITLE);
feature but it just won't work ! i don't able to find out why.
but when i use
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
it just worked !!! but i don't want to use Theme can i do it pragmatically ?
There are two ways to do that
Set below lines to your styles.xml
<item name="windowActionBar">false</item>
Or
Set below lines in your ActionBarActivity file.
getSupportActionBar().hide();
#Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
}
Im very new to android development so please bear my ignorance.
I created a Splash screen before loading my main activity. Splash is working fine but what causes the problem is the main activity, it keeps on instantiating.
Splash.java
public class Splash extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
final AdController optinController = new AdController(
getApplicationContext(), "SECTION_ID");
final Splash splash = this;
optinController.loadOptin((Activity) splash, "SECTION_ID",
new AdOptinListener() {
public void onAdOptin() {
// once optin process is complete, continue to main app activity
launchMain();
}
}
);
}
public void launchMain() {
finish();
Intent myIntent = new Intent(Splash.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
}
}
MainActivity.java
public class MainActivity extends Activity {
private AdController myController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myController = new AdController(getApplicationContext(), "SECTION_ID");
myController.loadNotification();
}
}
In the manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I used android:launchMode="singleInstance" but still it keeps on reinstantiating.
Please help. Thanks in advance.
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//|Window.FEATURE_INDETERMINATE_PROGRESS
setContentView(R.layout.splash);
final Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
finish();
}
},1000);
}}
It will work , i din get you exactly by instatiating.Also do no use single instance,you are not supposed to require it here.
Yes, following is my snippet of manifest file , just use a timer for your splash screen and move to your main class after specific time.
activity android:name=".Splash"
android:screenOrientation="portrait" android:label="#string/app_name">
i think you have to remove the intent flag Intent.FLAG_ACTIVITY_SINGLE_TOP
In manifest.xml, set category as LAUNCHER which activity you want to launch first and to Other set as DEFAULT as given in below example.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
I am using
Window w = getWindow();
w.setTitle("My title");
to change title of my current Activity but it does not seem to work.
Can anyone guide me on how to change this?
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
Just an FYI, you can optionally do it from the XML.
In the AndroidManifest.xml, you can set it with
android:label="My Activity Title"
Or
android:label="#string/my_activity_label"
Example:
<activity
android:name=".Splash"
android:label="#string/splash_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you want it one time & let system handle the rest (not dynamic) then do like this in your manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
<intent-filter android:label="#string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
setTitle(getResources().getText(R.string.MyTitle));
There's a faster way, just use
YourActivity.setTitle("New Title");
You can also find it inside the onCreate() with this, for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("My Title");
}
By the way, what you simply cannot do is call setTitle() in a static way without passing any Activity object.
This worked for me.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
getActivity().setTitle("My Title");
//...
}
If you have multiple activities, you can set it like this in AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="#string/category_numbers"
android:theme="#style/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family"
android:theme="#style/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors"
android:theme="#style/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases"
android:theme="#style/category_phrases" />
<activity
android:name=".ExperimentActivity"
android:label="#string/category_experiment"
android:theme="#style/category_experiment" />
</application>
I'm using Android Studio 3.0.1.
WIth an Activity:
setTitle("Title Text");
Inside a fragment:
getActivity().setTitle("Title Text");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
If you want to set title in Java file, then write in your activity onCreate
setTitle("Your Title");
if you want to in Manifest then write
<activity
android:name=".MainActivity"
android:label="Your Title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In Kotlin, this way:
this.title = resources.getText(R.string.fast_learning)
I have a Toolbar in my Activity and a Base Activity that overrides all Titles. So I had to use setTitle in onResume() in the Activity like so:
#Override
protected void onResume() {
super.onResume();
toolbar.setTitle(R.string.title);
}
The code helped me change the title.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
ActivityName.this.setTitle("Your Activity Title");}
Inside a MainActivity:
public class act1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
setTitle("First Activity");
}
}
setTitle("Whatever apps");
in MainActivity.java is simplier
I would say.
If you want to change Title of activity when you change activity by clicking on the Button. Declare the necessary variables in MainActivity:
private static final String TITLE_SIGN = "title_sign";
ImageButton mAriesButton;
Add onClickListener in onCreate() and make new intent for another activity:
mTitleButton = (ImageButton) findViewById(R.id.title_button);
mTitleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SignActivity.class);
String title_act = getText(R.string.simple_text).toString();
intent.putExtra("title_act", title_act);
startActivity(intent);
finish();
}
});
SecondActivity code in onCreate():
String txtTitle = getIntent().getStringExtra("title_act");
this.setTitle(txtTitle);
If you're using onCreateOptionsMenu, you can also add setTitle code in onCreateOptionsMenu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
setTitle("Neue Aktivität");
return true;
}
setTitle("Welcome Activity");