Android FEATURE_CUSTOM_TITLE giving error - android

I have the following code which results in error message that I *can not combine FEATURE_CUSTOM_TITLE with other title features* (which I am not to my knowledge...)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String customTitleStr = getIntent().getExtras().getString("Title");
Boolean customTitleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if ( (customTitleSupported) && (customTitleStr != null) ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.override_titlebar);
final TextView myTitleText = (TextView) findViewById(R.id.myTitleText);
if ( myTitleText != null ) {
myTitleText.setText(customTitleStr);
}
}
I have tried place setContentView(R.layout.); both before and after this.requestWindowFeature but no change.
My manifest targets this:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
/>
For reference (don't know if related), I also have trouble theming my titlebar. (Simply ignored.)
Just in case it is relevant, some code snippets for my theming:
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/replace__logo__app_android"
android:label="#string/MicAppName"
android:theme="#style/MicTheme"
android:name="com.examples.example.MicApp"
>
styles.xml:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
// ...
<style name="MicTheme" parent="AppTheme">
<item name="android:windowTitleSize">44dip</item>
<item name="android:windowTitleBackgroundStyle">#style/MicWindowTitleBackground</item>
<item name="android:windowTitleStyle">#style/MicWindowTitle</item>
</style>

Please try with this code.
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CustomTitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
}
for more information about how to set custom title bar please follow below tutorials.
http://www.edumobile.org/android/android-programming-tutorials/creating-a-custom-title-bar/
http://www.powenko.com/en/?p=214
http://7thursdays.wordpress.com/2012/10/14/how-to-make-a-custom-title-with-android/
hope this helps you.
EDIT
here is the working screenshot.

Related

Android Window title showing after requested not to show

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, BaseActivity.class);
startActivity(i);
}
and my next activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_base);
}
I still see blue bar with application name at top.
I tried changing my application manifest to use a no-title theme and the program crashed because the activities inherit from AppCompatActivity.
How do I inherit from AppCompatActivity and get rid of the application title name up top?
It's the actionbar, you can use the theme Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar or a theme which descents from them.
To make disappear is the action bar also this should work:
getSupportActionBar().hide();
Try this...
If you don't want the action bar in your activity then,
Create a style
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style for the activity in Manifest for which you don't need action bar,
<activity android:name=".BaseActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
Do this in your onCreate() method.
//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);
this refers to the Activity
OR
You can modify your AndroidManifest.xml:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
You can use this for each activity in your project.
OR
also change from style.xml
<style name="generalnotitle" parent="general">
<item name="android:windowNoTitle">true</item>
</style>
Hope this helps you.

How to create a single activity without headers

I'm developing an application where I set up an activity for the settings.
I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex.
Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers?
Thanks in advance for the answer.
Greetings.
To create a single settingActivity you can try the below pseudo code:
public class SettingsUI extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
}
}
}
Then settings_preference.xml code:
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="sample_key"
android:title="#string/sample_title"
android:inputType="number"
android:summary="#string/sample_summary"/>
<EditTextPreference
android:key="sample2_key"
android:title="#string/sample2_title"
android:inputType="number"
android:summary="#string/sample2_summary"/>
</PreferenceScreen>
In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />
And goto AndroidManifest file, please apply above style to your activity something like this.
<activity android:name=".SettingActivity" android:theme="#style/AppTheme1"></activity>
if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/mainColor</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
... ...

Getting the error expected resource of type attr

I am trying to use the Home Key Locker https://github.com/shaobin0604/Android-HomeKey-Locker
I want to be able to detect and prevent homescreen button click on the lock screen. Majority of the answers mentioned that it cannot be disabled.
This is the HomeKeyLocker Class :
package com.example.harshilshah.screenonoff;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.FrameLayout;
import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
public class HomeKeyLocker {
private OverlayDialog mOverlayDialog;
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
}
}
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
public static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.type = TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F; // transparent
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
protected final void onCreate(Bundle bundle) {
super.onCreate(bundle);
FrameLayout framelayout = new FrameLayout(getContext());
framelayout.setBackgroundColor(0);
setContentView(framelayout);
}
}
}
This is the styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="OverlayDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
I am getting an error Expected resource of type Attr in the following lines
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);//ERROR
How do i solve this?
I just had this issue. What caused it for me was that I imported the android.R class, instead of the R class already defined in my app. So it wasn't using the styles.xml defined in my app's values, where the OverlayDialog was implemented, but the standard one defined in the Android system that obviously didn't have it. Once I deleted import android.R and added import com.example.myapp.R it worked just fine.

getActionBar() returning null

I need my actionbar to be ready before setContentView because it is used by the navDrawerFragment but at this point:
public class BaseActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
Log.d("", getActionBar().toString());
setContentView(R.layout.activity_base);
}
It is returning null
My theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/light_blue</item>
<item name="colorPrimaryDark">#color/dark_blue</item>
<item name="colorAccent">#color/dark_blue</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:positiveButtonText">#color/white</item>
<item name="android:windowActionBar">true</item>
</style>
and the declaration at manifest:
<activity
android:name=".controller.activity.BaseActivity"
android:label="#string/app_name" >
</activity>
Your BaseActivity must extends from ActionBarActivity and not Activity
public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
Use getSupportActionBar(); to get the ActionBar
Your problem is either one of these two things, or both. At least they should be, unless I'm insane..
Either the problem is that you are calling
getActionBar()
before you set the contentView
So change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_base);
Log.d("", getActionBar().toString());
}
OR
It is that you need to call
getSupportActionBar()
Try both and tell me which works!
You're using the support library (AppCompat), so you have to call getSupportActionBar()

Action Bar Back Button is not showing in Android

Hi I have created an activity which extends ActionBarActivity & using material theme in my application. In the Action Bar, Back button is not showing.
I didn't find why it is not showing. Any help ?
public class RegistrationActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_background_light));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!--Support Library compatibility-->
<item name="actionBarStyle">#style/MyTheme.ActionBarStyle</item>
</style>
<!-- ActionBar styles -->
<style name="MyTheme.ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar">
<!--Support Library compatibility-->
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
AndroidManifest.xml
<activity
android:name=".RegistrationActivity"
android:label="#string/title_activity_registration" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeScreenActivity" />
</activity>
Thanks in advance.
add the property
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
to show the "back button"
If the Jorgesys's solution not worked for you. Try overriding the onOptionsItemSelected method.
public class MyActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
onBackPressed();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
}
there might be a problem with your toolbar theme:
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light"

Categories

Resources