Why does Activity display Title yet by default? - android

From some doucment , I know ActionBarActivity display app title by default. I create a Activity with eclipse, I have set android:theme as android:Theme.Light, I find the Activity still display Title by default, why?
package com.example.aa;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<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>
</application>
</manifest>
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
<RelativeLayout 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="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>

Try setting this to your activity declared in the manifest:
android:theme="#android:style/Theme.NoTitleBar"

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
Be certain to put it before the call to setContentView(R.id.x), otherwise it will crash. The reason wasn't immediately obvious to me. 08-13 12:47:33.561 E/AndroidRuntime( 9125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.SplashActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

Related

in Android, getting inflation error when trying to use SwitchCompat

I'm trying to use android.support.v7.widget.SwitchCompat to create a switch in minimum SDK of 10 and I'm getting the error android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.SwitchCompat In looking for solutions, I've found that it may have something to do with which style is declared? So I've changed the style to Theme.AppCompat but I'm still getting the error that it can't inflate. I'm fairly certain that I've got the correct support library added (I'm using Eclipse Kepler) because there are no errors at import android.support.v7.widget.SwitchCompat;. So here's all the relevant code, can anyone see where the problem is?
Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name"
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>
....
Style:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
...
</resources>
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal" >
<android.support.v7.widget.SwitchCompat
android:id="#+id/test1_SW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="25dp"
android:checked="false"
android:text="SwitchCompat"
android:textOff="OFF"
android:textOn="ON" />
</RelativeLayout>
Activity:
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.widget.CompoundButton;
public class SwitchTest extends Activity implements
CompoundButton.OnCheckedChangeListener {
private SwitchCompat test1_SW;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
setContentView(R.layout.switch_test);
// --- one
test1_SW = (SwitchCompat) findViewById(R.id.test1_SW);
test1_SW.setSwitchPadding(40);
test1_SW.setOnCheckedChangeListener(this);
// --- END one
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.test1_SW:
Log.i("test1_SW", isChecked + "");
break;
}
}
}
The problem is your Activity is extending android.app.Activity, and not the support Activity you need to use with the AppCompat libraries. Just change:
import android.app.Activity;
to
import android.support.v7.app.ActionBarActivity;
Or, since I think that's been deprecated in API 22:
import android.support.v7.app.AppCompatActivity;
Once you change those it should work.

Theme.AppCompat.Light.DarkActionBar results in no ActionBar showing

I created a project with a blank activity (and then wrote code to put in a basic fragment - empty linear layout) in Android Studio but there is no ActionBar showing. Why?
Device: Emulator (Nexus 5 size), API 19
The main activity extends FragmentActivity.
The Android Studio generated styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
The activity layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
The fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.test" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".TestActivity"
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>
</manifest>
Instead of extending FragmentActivity, I need the main class to extend AppCompatActivity.
As explained here:
http://developer.android.com/training/basics/fragments/creating.html
If you're using the v7 appcompat library, your activity should instead extend ActionBarActivity, which is a subclass of FragmentActivity (for more information, read Adding the Action Bar).
EDIT
ActionBarActivity is now deprecated. Use AppCompatActivity instead.

Actions Buttons in Android not visible

I'm currently working on my first android app following the official tutorial.
Right now im trying to add Action Buttons to my
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
The application is running without any errors, but the search button is not displayed. ic_action_search icon is properly placed into res/drawable-* and action_search/action_settings is defined in strings.xml.
Am I missing something?
Edit:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.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="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
As per the document
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
You are missing custom namespace in the main_activity_actions.xml. Remove yourapp with the name of app or anything.

custom action bar keeps disappearing when i switch between fragments using the navDrawer

I wanted to add my own custom action bar to the drawer navigation by Google which i have figured out, the problem is, that when I try to open the drawer the Action bar then disappears, along with the icon that allows me to toggle the drawer is also gone for good. it again disappears when i click on my next fragment activity. I also noticed that if i leave the theme of the app back to its default theme everything works fine.
here's my custom theme style class
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:background">#f0f0</item>
</style>
</resources>
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.navigationdrawerexample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar">
<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>
</application>
When it works i use #android:style/Theme.Holo.Light.DarkActionBar for the theme

Incorrect theme is applied to AlertDialog

I am trying to create a new alertbox with a loading symbol in my android application. Working fine. Below is my code.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.custom, null));
progress = new ProgressBar(this);
builder.setMessage("Message");
builder.setView(progress);
//builder.setCancelable(false);
builder.create().show();
My Custom.XML looks like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"/>
</RelativeLayout>
My res/styles.xml is
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="maxWid">
<item name="android:textColor">#000000</item>
<item name="android:layout_marginTop">50dp</item>
<item name="android:layout_marginBottom">50dp</item>
<item name="android:layout_marginLeft">10dp</item>
</style>
<style name="apBtn">
<item name="android:textColor">#000</item>
<item name="android:layout_alignParentLeft">true</item>
</style>
</resources>
res/values-v11/styles.xml is
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>
res/values-v14/styles.xml is
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
My AndroidManifest.cml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.kmc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.myapp.kmc.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myapp.kmc.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.myapp.kmc.MainActivity"
android:label="#string/app_name"
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>
<activity
android:name="com.myapp.kmc.LoginActivity"
android:label="#string/title_activity_login"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="com.myapp.kmc.ListActivity"
android:label="#string/title_activity_list"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name="com.myapp.kmc.CompleteActivity"
android:label="#string/title_activity_complete"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.kmc" />
</intent-filter>
</receiver>
<service android:name="com.myapp.kmc.GCMIntentService" />
<activity
android:name="com.myapp.kmc.RegisterDevice"
android:theme="#android:style/Theme.NoTitleBar"
android:label="#string/title_activity_register_device" >
</activity>
</application>
</manifest>
Testing in android 4.1.2
But when I create a new application and follow this code I am getting an alert box like this
But When I implement the same code in my existing application. I am getting an alert box like this
Since I want the first one in my existing application. Its ruining my time..
Can any one please help me. I am new to android
Thanks in advance
Your existing application uses Theme.Black, that's why dialog has black background. Change theme to Theme.Holo.Light to fix it.
For recommended way to do this, take a look at my other answer.
EDIT: Looks like you're applying the correct theme to your <application> tag, but then you're overriding it by setting Theme.NoTitleBar (which is a descendant of Theme.Black) to each of your activities.
Remove android:theme attribute from <activity> tags and it will work.
If you want to remove title and action bar, apply windowNoTitle and windowActionBar attributes to your theme definition:
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
If you want to use Holo Theme you must set your API level to 14 (that has hardware acceleration enabled as default);
Try to insert:
<uses-sdk android:minSdkVersion="14" />
At your AndroidManifest.xml.

Categories

Resources