How to remove android activity label (activity title) and label bar? - android

I added android:theme="#android:style/Theme.NoTitleBar" to the manifest file but the app doesn't open. I want to remove the Activity label in which the name of the Activity is written.
Here is part of manifest file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
>

You need to change extends in your MainActivity.
Change this
public class MainActivity extends AppCompatActivity {
}
For this
public class MainActivity extends Activity {
}

Set
android:theme="#style/Theme.AppCompat.Light.NoActionBar"

You can use the
requestWindowFeature(Window.FEATURE_NO_TITLE) Just insert in it immediately after your onCreate function
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);}
You may have to change your extends AppCompatActivity to extends Activity sometimes to work without error

Related

Android.app.Application cannot be cast to

Despite i followed some StackOverflow's tips, I can't get rid of this error:
Android.app.Application cannot be cast to com.dostalgia.airshootingrogatti.GlobalClass
I inserted GlobalClass in manifest file, as already said in S.O.
This is the Global Class Code:
public class GlobalClass extends AppCompatActivity {
private int StatoContatore1;
public int getStatoContatore1() {
return StatoContatore1;
}
public void setStatoContatore1(int statoContatore1) {
StatoContatore1 = statoContatore1;
}
}
And this is the activity (Not the main) that calls and generate error
package com.dostalgia.airshootingrogatti;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Postazione1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_postazione1);
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
}
}
This is my Mainfest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dostalgia.airshootingrogatti">
<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/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=".Postazione1"></activity>
<activity android:name=".GlobalClass"></activity>
</application>
</manifest>
My goal is to use and manage global variables in different activities!
Thanks for your reply!
My goal is to use and manage global variables in different activities!
You are welcome to carefully use static fields. This would not require you to try creating a custom Application subclass, as you appear to be attempting to do here.
That being said, if you really want to do what I think you are trying to do:
Step #1: Have GlobalClass extend android.app.Application, not AppCompatActivity
Step #2: Remove the <activity> element in the manifest for GlobalClass
Step #3: Add an android:name attribute to the <application> element in the manifest, pointing to your GlobalClass class (e.g., android:name="com.dostalgia.airshootingrogatti.GlobalClass")

Using BroadcastReceiver to check Battery Level but App crash?

I am trying to get current battery level using broadcast receiver with intentfilter ACTION_BATTERY_CHANGED having only one TextView in my xml and setting its text property to some string+integer variable that should hold value of BatteryManager.EXTRA_LEVEL. But app crashes everytime it try to boot.
Am i missing something?
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView tvcl=(TextView) findViewById(R.id.tvcl);
private BroadcastReceiver bcr=new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int currentLevel=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
tvcl.setText("Current Battery Level "+ Integer.toString(currentLevel) + "%");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
IntentFilter bcFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(bcr,bcFilter);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The line
TextView tvcl=(TextView) findViewById(R.id.tvcl);
appears to be in the class definition and not in a method.
If so, then it will return null since the ContentView isn't set until OnCreate whereas this definition occurs at object creation time.

Android: Activity not found when starting new activity using intent

I am trying to launch a new activity from within an existing activity however I get an activity not found exception.
Here is a stripped back version of my code and the logcat error. Any help would be really appreciated.
MainActivity
public class MainActivity extends FragmentActivity {
/* (non-Javadoc)
* #see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, TermsAndConditions.class));
}
....
}
NewActivity
public class TermsAndConditions extends Activity{
/* (non-Javadoc)
* #see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_and_conditions);
}
}
Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:screenOrientation="sensorLandscape">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
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:label="#string/app_name" android:name=".TermsAndConditions"></activity>
</application>
</manifest>
Logcat
04-02 16:48:43.255: E/AndroidRuntime(20856): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.app/com.example.app.TermsAndConditions}; have you declared this activity in your AndroidManifest.xml?
<activity android:label="#string/app_name" android:name="com.example.app.TermsAndConditions"></activity>
and clean your project,
Hope this help you.
The problem is, you've told the Main Activity to send the intent but you haven't told the Terms and Conditions activity to receive the intent. You do that by, in the onCreate method in the Ts and Cs page go getIntent() after setContentView

Android ActionBar is hidden with DroidGap (PhoneGap/ Cordova)

i just spent some hours on making the Actionbar visible without success:
With my RegisterActivity i collect some login data and then i start MainActivity. In RegisterActivity i have a Actionbar but not in MainActivity:
AndroidManifest:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17"/>
<application android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity android:name="RegisterActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<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/app_name"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
android:theme="#android:style/Theme.Holo" >
</activity>
</application>
RegisterActivity:
...
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("username", username);
i.putExtra("password", password);
startActivity(i);
...
MainActivity:
...
webview = new WebView(this);
setContentView(webview);
webview.loadUrl(address);
...
Normally there should be an Actionbar but there isn't!
just hope that someone has a solution for my problem?!
Thanks
I solved my problem:
My RegisterActivity extends the Class Activity! My MainActivity extends the class DroidGap from Phonegap2.5.0 (aka Apache Cordova).
I changed MainActivity so that it extends Activity and the result is that the ActionBar is visible :D
When you need DroidGap you will have to insert
super.setBooleanProperty("showTitle", true);
before you call:
super.onCreate(savedInstanceState);
At the end it looks like this:
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.setBooleanProperty("showTitle", true);
super.onCreate(savedInstanceState);
//More Code ....
The correct way of setting the boolean property ShowTitle is using the preferences in the config.xml.
add
<preference name="ShowTitle" value="true"/>
to config.xml to enable the ActionBar on Android.
Are you sure that there is no line:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
in onCreate() method?
Try:
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
...
setContentView(...);
...
or
...
this.requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
...

How to set the title not display?

It can remove title by setting requestWindowFeature(Window.FEATURE_NO_TITLE) in onCreat(). But the title display first, then disappear. How could the title not display? Could it set in manifest file?
Use like this in your manifest file:-
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
just permission in activity which u want to have no title
android:theme="#android:style/Theme.NoTitleBar"
use like this way..
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
If you just want the default theme and no title, you can put this in your manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
You can also set it from the Theme drop-down menu above your layout in eclipse.
You should always use the requestWindowFeature(Window.FEATURE_NO_TITLE) before using the setContentView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.your_layout);
}

Categories

Resources