Related
Basically, I have an app with two Activities.
#1 - MainActivity
This has a solid black background and a button.
When the button is pressed TransparentActivity should be presented.
#2 - TransparentActivity
I want this to be transparent (so the phones normal UI can be seen through).
I've tried using the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent);
setContentView(R.layout.activity_trick);
}
But it causes the app to crash with an NullPointerException.
Try1:
Make super.onCreate(savedInstanceState); call after setTheme(android.R.style.Theme_Translucent);. So do as:
setTheme(android.R.style.Theme_Translucent);
super.onCreate(savedInstanceState);
Try 2:
If that doesn't work, I find the following way easiest to make my activity transparent:
<activity android:name=".your.activity.declaration.here"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
Basically add android:theme="#android:style/Theme.Translucent.NoTitleBar" to your activity declaration in manifest. I can see that you are trying to do a similar thing programatically but by specifying it in manifest never crashed for me. If it does, then there might be other reasons.
Hope it helps.
AppCompatActivity hasn't the Theme_Translucent(maybe the Theme_Translucent is null),you should create your own style.
Here is a pic of android applicationbar i like ( I mean the part where settings is written on, not sure if applicationbar is the right name :) )
http://oi50.tinypic.com/ehwwpc.jpg
Some apps i have downloaded also have the exact same bar so im guessing its a predefined theme but when i make a project (theme.holo) then my applicationbar is just totally black.
So my question is how do i get the same project bar?
Thank you!
Edit:
I found that all apps are opensoruce and i looked out settings apps manifest for android 4.0.4, same as mine.
Here it is: https://android.googlesource.com/platform/packages/apps/Settings/+/android-4.0.4_r2.1/AndroidManifest.xml
Whats strange is that it uses the same theme i do: android:theme="#android:style/Theme.Holo"
... but still the titlebar is different. There must be another attribute somewhere that defines the titlebar? Does anyone have an idea? :)
The title of the activity can be defined in the Manifest file, and it should be android:label property. For example:
<activity
android:name=".MainActivity"
android:label="#string/TitleBarText" />
TitleBarText is the android string resource that contains the name of the title.
If you wish to do it from code, you can just do the following in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
setTitle("Title Bar Name");
setContentView(R.layout.activity_main);
}
To create a custom title bar, refer to: this and this
I need to make one of my activities called MyNoStatusBarActivity.java a full-screen activity.
I have added in the Manifest :
<activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:name=".MyNoStatusBarActivity"
android:noHistory="true"
android:label="#string/app_name"
android:configChanges="touchscreen|keyboard|keyboardHidden|navigation|orientation">
...
</activity>
And in the onCreate of my activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Doing all this just hides the TITLE bar and not the STATUS bar.
Also if I use the manifest theme on the application level instead of my activity, it works but for all activities of my app which is not what I want. I want the no status bar only for one particular activity.
Help will be really appreciated. Thnx!
P.S : I am ok is this behavior is no longer available in Honeycomb/ICS. I need this just for 2.2 and 2.3
EDIT
Tried all the suggestions mentioned in other solutions in SO but no success yet. Theme works when applied to the application level but not on the activity level.
I am using HTC WildFire-S, android ver 2.3.5
In your manifest xml, at application level put this:
<application
...
android:theme="#android:style/Theme.NoTitleBar" >
</application>
and in the activity that you want to make full screen put this:
<activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
...
</activity>
Remove all the setWindowsFeature or setFlag methods in onCreate.
I think you just need to take the android:theme="#android:style/Theme.NoTitleBar.Fullscreen out of the mainfest, stick with code only call, it's always worked for me. Which test device are you using?
Also make sure these calls come before you call to setContentView(R.layout.xxx) like this
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.xxx);
Check all of your code for android:theme declarations, ie manifest and individual layouts xml files. as it sounds like declaring it at app level is working (as it's overriding any subsequent theme declarations) but there is something overriding your activity level request.
Try searching your whole project for "android:theme" and remove anything that you're not 100% sure you need, test it that way
I put this in my activity:
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
And in my manifest I don't modify, this is mine, without modifications:
android:theme="#style/AppTheme"
In my activty xml:
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
This will do the trick:
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);
}
This will fix your problem
just change in your manifest #android:style... for #style/..
<application
...
android:theme="#style/Theme.AppCompat.NoActionBar">
...
</application>
I'm having an odd problem.
I am making an app with targetsdk 13.
In my main activity's onCreate method i call getActionBar() to setup my actionbar. This works fine when running on the Android 3.2 emulator, but when using Android 3.0 and 3.1 the getActionBar() method returns null.
I find this extremely odd, and i cannot see any reason why it would do so.
Is this a bug with the emulators or is there something i need to do, in order to ensure that my application has an actionbar?
SOLUTION:
I think I've found a solution for this problem.
I wasn't using the setContentView to set a layout for the activity. Instead I was using fragmentTransaction.add(android.R.id.content, mFragment, mTag) to add a fragment to the activity.
This worked fine in 3.2, but in earlier honeycomb versions the action bar is apparently not set if you don't use the setContentView in the onCreate() method.
So I fixed it by using the setContentView() method in my onCreate() method and just supplying it with a layout that contained an empty FrameLayout.
I can still use the fragmentTransaction.add(android.R.id.content, mFragment, mTag) method the same way as before.
It's not the prettiest fix, but it works.
Can use getSupportActionBar() instead of getActionBar() method.
If you are using the support library
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
use getSupportActionBar() instead of getActionBar()
* Update:
The class ActionBarActivity now is deprecated:
import android.support.v7.app.ActionBarActivity;
I recommend to use:
import android.support.v7.app.AppCompatActivity
if you are using android.support.v7.app.AppCompatActivity
public class HomeActivity extends AppCompatActivity {
Then you should be using android.support.v7.app.ActionBar
ActionBar ab = getSupportActionBar();
If you are using android.support.v4.app.FragmentActivity
public class HomeActivity extends FragmentActivity {
then you should be using android.app.ActionBar
ActionBar ab = getActionBar();
If you are using android.support.v7.app.ActionBarActivity
public class HomeActivity extends ActionBarActivity {
you should be using android.support.v7.app.ActionBar
ActionBar ab = getSupportActionBar();
You have to define window type as actionbar before activity render its view.
use
requestWindowFeature(Window.FEATURE_ACTION_BAR);
before calling setContentView() method.
I faced the above issue where getActionBar() method returns null. I was calling the getActionBar() after setting the setContentView() and still its returning a null.
I resolved the issue by setting the min-sdk version in Android Manifest file that was missing initially.
<uses-sdk android:minSdkVersion="11" />
ActionBar needs application or activity's Theme to have an app title. Make sure you have not styled your application or activity as Theme.NOTITLE.
<application
android:name="com.xxx.yyy"
android:debuggable="false"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/Theme.NoTitle"> // remove this line if you have this in your code
<activity
android:name="com.xxx.yyy.Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#style/Theme.NoTitle" // remove this line if you have in your code
android:windowSoftInputMode="adjustResize|stateHidden" >
import android.support.v7.app.AppCompatActivity;
then
extends AppCompatActivity
then use
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This answer is late but might be helpful to anyone who arrives from Google: You might well need to declare
<item name="android:windowActionBar">true</item>
in your styles.xml. It seems false can be the default. You also need to be on API 11 or higher.
More details can be found in the documentation here. Specifically, quote:
Tip: If you have a custom activity theme in which you'd like to remove
the action bar, set the android:windowActionBar style property to
false. However, if you remove the action bar using a theme, then the
window will not allow the action bar at all, so you cannot add it
later—calling getActionBar() will return null.
I had the same problem and one of the solutions was to use setContentView() before calling getActionBar().
But there was another thing that fixed the problem. I specified theme for the application to be #android:style/Theme.Holo.Light.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
...
</application>
I think any theme, which has <item name="android:windowActionBar">true</item> in it, can be used.
The main reason for that is using themes that are not supporting ActionBar:
In manifest file add the following either in your target activity or application element (if you want to unify the theme over whole application)
Examples of themes that are supporting action bar "Theme.AppCompat.Light" or "Theme.Holo.Light" ...
android:theme="#android:style/Theme.Holo.Light"
It is better to put all styles in styles.xml and use it everywhere using "#style/themName" so the previous one will be
android:theme="#style/AppTheme"
and styles.xml will have the following:
<style name="AppTheme" parent="Theme.AppCompat.Light">
Hints:
There is some themes that can not be used in old SDKs like "#android:style/Theme.Holo.Light.DarkActionBar" is not supported before SDKs version 14.
To allow your app to support minimum specific version of SDK you could add the following under <app> element:
<uses-sdk android:minSdkVersion="14" />
To specify min SDK version in AndroidStudio, you could by using app's Gradle file.
android{
defaultConfig{
minSdkVersion 14
targetSdkVersion 21
}
}
I ran into this problem . I was checking for version number and enabling the action bar only if it is greater or equal to Honeycomb , but it was returning null. I found the reason
and root cause was that I had disabled the Holo Theme style in style.xml under values-v11 folder.
go to the AndroidManifest.xml and replace
android:theme="#style/AppTheme"
by
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
Use getSupportActionBar() instead of getActionBar()
In my case, I had this in my code which did not work:
#Override
protected void onCreate(Bundle savedInstanceState) {
context = getApplicationContext();
requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Then I played with the order of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
}
And it worked!
Conclusion: requestWindowFeature should be the first thing you call in the onCreate method.
I had the same issue. It solved by chaning App theme in styles.xml
Before
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
After
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
One thing I wanted to add since I just ran into this, if you are trying to getActionBar() on an Activity that has a parent, it will return null. I am trying to refactor code where my Activity is contained inside an ActivityGroup, and it took a good few minutes for me to go "oh duh" after looking at the source of how an ActionBar gets created in source.
I solve it by this changes:
change in minifest android:theme="#android:style/Theme.Holo.Light" >
add to class extends ActionBarActivity
add import to class import android.support.v7.app.ActionBarActivity
To add to the other answers:
Make sure you call setActionBar() or setSupportActionBar() in your onCreate() method before calling the getActionBar():
Define some Toolbar in your activity.xml, then in the onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
// Now you can use the get methods:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In my case I simply had to extend AppCompatActivity instead of Activity
supportActionBar?.setDisplayHomeAsUpEnabled(true)
Full activity example class:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
//class LocationFound : Activity() { <-----Does not seem to work with ActionBar in recent versions
class LocationFound : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_location_found)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
} }
On Versions
minSdkVersion 22
targetSdkVersion 29
I know I am late to the party (and new to Android) on this question but I found the information here very helpful and thought I should add the findings of my own endeavours with getting ActionBar to work as I wanted in case others like me come looking for help.
I have a widget which is a floating window with no window title. I use a style theme to implement android:windowIsFloating, android:backgroundDimEnabled and android:windowNoTitle. The widget worked fine until I wanted to add a button that called a fragment pager with several list fragment pages and used the ActionBar. It would crash on the pager activity with a null pointer exception. I narrowed it down to the ActionBar being null. Following the findings of previous people who contributed to this thread I removed my theme from the manifest file and the ActionBar worked fine but now my window now longer floated (it was fullscreen) and it had a page title I did not want.
Further research took me to the Styles and Themes API Training Guide which led me to a solution. I discovered I could add my custom theme to individual activities in the manifest file whereas before I was applying it to the application. All my windows now have the desired appearance.
Try extending your Activity class from ActionBarActivity. This solved it for me. Do something like the following:
public class MyActivity extends ActionBarActivity
{
. . .
In my case the class was extending only from Activity.
This may also help some people.
In my case, it was because I had not defined a context in the menu.xml
Try this:
<menu 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"
tools:context="com.example.android.ActionBarActivity">
Instead of this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
Just check the implementation of source code by command click:
private void initWindowDecorActionBar() {
Window window = getWindow();
// Initializing the window decor can change window feature flags.
// Make sure that we have the correct set before performing the test below.
window.getDecorView();
if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
return;
}
mActionBar = new WindowDecorActionBar(this);
mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
mWindow.setDefaultIcon(mActivityInfo.getIconResource());
mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
}
requestWindowFeature(Window.FEATURE_ACTION_BAR); Fixed my issue as I saw requestWindowFeature(Window.FEATURE_ACTION_BAR) is failing; code is open source use it !!
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
works pretty quickly
If calling this method from Fragment the make sure to call this in onActivityCreated()
I am getting this exception while I am trying to call an activity from another one. The complete exception is
android.content.ActivityNotFoundException:Unable to find explicit activity class {com.x.y/com.x.y.class};
I am doing an intent.setClass("com.x.y","com.x.y.className") where className is the name of my activity class and com.x.y is the package it resides in.
My AndroidManifest.xml has the following content:
<activity android:name="com.x.y.className" android:label="#string/app_name">
Am I missing anything?
Maybe you need to check that you added the new activity to the manifest.xml file
Example:
<activity
android:name=".className"
android:label="#string/app_name" >
</activity>
If other people are encountering something similar and arrive at this post, an issue I had may save you some time. May not be related to the OP's problem but def related to the ActivityNotFound exception.
I was trying to load an activity by using:
Intent intent = new Intent( this, class );
However I continuously kept getting the ActivityNotFoundException even though I had checked and rechecked the code multiple times.
This exception I was getting wasn't actually being caused by the intent but some code I was running inside the loaded activity throwing a RuntimeException. (my issue was caused by Typeface.createFromAsset())
It is possible you are running into a similar RuntimeException in your activity.
To see if this is the case, put your intent code in try catch blocks. Like so:
try {
/* your code */
...
} catch ( ActivityNotFoundException e) {
e.printStackTrace();
}
Run your app again, and check your LogCat, if it's the same issue, you'll get a RuntimeException with a "Caused By:" entry pointing to your actual issue.
I spent a good hour trying to figure this out. Hopefully this may save someone some time.
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.
Delete your activity from the manifest and then add it again. This type do not write type the XML directly. Instead, go to Application > Application nodes > add, choose the Activity, and then browse for the file source.
This worked for me.
intent.setClass takes parameters as "Package Context" and "Class".
an example would be:
intent.setClass(CurrentActivity.this, TargetActivity.class);
also you need to check if the activity is registered in manifest file.
Added a new activity and defined it in manifest.xml, but I was still getting "Unable to find explicit activity class" error. I am using Eclipse. Solution for my problem was "cleaning" the project. From the main menu in Eclipse: Project|Clean.... Then you select your project and clean it.
Hey, you need to use another form of Intent constructor. This will surely solve your issue within a second:
Example:
Intent inte=new Intent(getBaseContext(),"your class name with .class extension ");
startActivity(inte);
This works perfectly and I checked this code, its working properly.
I had an ActivityNotFoundException when I implemented the Activity inside another class (as an inner class):
//... inside the utility class Pref
public static class Activity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
//...
Declared as the following inside the manifest:
<activity android:name=".Pref.Activity"
...
After declaring this as a normal class (public class PrefActicity) and changing manifest accordingly, it worked as usual.
I was using getActivityContext() (instead of Activity.this) for the menu code to save some work, and copy-and-paste it to each activity without editing each time.
I replaced them with Activity.this, and the issue is gone.
I have a feeling a smarter Android guy could work-around not having to do that. Would like to hear what it would be.
Looking at the documentation here what you want is:
intent.setClassName("com.x.y", "className");
Restart the Eclipse and check your Manifestfile again. If you find missing the respective Activity, then add it and try again. It solved my similar issue.
In addition to Mina's answer.
When you declare activity as inner static class then you should write your activity into manifest like ...
<activity android:name=".app.FragmentLayoutSupport$DetailsActivity" />
here .app comes from your package name , it can be .helpers.afdfa$afda
My solution to this error was to add a package name in front of the name in manifest.
I had the following activities:
id.scanner.main.A1
id.scanner.main.gallery.A2
My manifest contained the following:
<activity android:name=".A1" ....></activity>
<activity android:name=".A2" ....></activity>
This solved the problem:
<activity android:name=".A1" ....></activity>
<activity android:name="gallery.A2" ....></activity>
Yeah I got this problem too. I refreshed the project. And then, everything works fine.
when i have same issue. if you are using library class files and writing it into android manifest files write it like and then remove the library projects manifest files this portion>>
then it will work absolutely..
This exception also occurs if you include a library in your app and if the library is calling an activity defined in the library project. In this case we need to merge library's manifest with calling app's manifest.
With ADT version 20, we can do this by adding the below statement in project.properties of calling app.
manifestmerger.enabled=true
Check out the content of the Android Manifest File in the bin folder of the project. When your app is compiled and packaged the Manifest File is copied to the bin folder. In my case the Manifest in the bin folder did not agree with the original Manifest. This is probably a mistake of Eclipse. I manually copied the Manifest to the bin folder and it worked.
you can add this code in manifiest.xml file
action android:name="com.kaushalam.activity101activity.SecondActivity"
category android:name="android.intent.category.DEFAULT"
I got the same case too. After reading thepearson's answer, I revised my Activity and found out that I wrote
public void onCreate(Bundle s)
But in fact it should be
protected void onCreate(Bundle s)
And it works now!
This works if you have an Activity object (which you need to launch):
intent.setClassName(CallingActivity.this, activityToLaunch.getComponentName().getClassName());
Activity you're calling sholdn't contain "sheme" and contain intent-filter:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.sj.myapplication.SecondActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
so in calling code:
Intent intent=new Intent("com.example.sj.myapplication.SecondActivity");
startActivity(intent);
Try using the following:
intent.setClassName("com.x.y", "com.x.y.className");
This works for me
I also ran into ActivityNotFoundException by passing the wrong view into setContentView(), each activity's class file must correspond with the layout xml file this way.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wrongView);
}
as opposed to
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.correctView);
}
I had the same issue. I tried everything but the error, which I sorted out later, was that there was a space left between double quotes and my class name. It has to be:
intent.setClassName("com.x.y","com.x.y.className")
not
intent.setClassName("com.x.y"," com.x.y.className")