How set Theme in main activity api 7? - android

I offer my users a light and dark option for theming.
The error in the Log:
Caused by: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
The error occurs in setContentView(R.layout.activity_main); in the MainActivity.
MainActivity
public class MainActivity extends SherlockFragmentActivity {.....public static int globalTheme;
Context context;
protected void onCreate(Bundle savedInstanceState) {
context = getApplicationContext();
mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = mySharedPreferences.edit();
editor.putBoolean("proxy", false);
editor.commit();
if (mySharedPreferences.getString(Preferences.PREF_THEME, "1").trim().equals("1"))
globalTheme = R.style.Sherlock___Theme;
else
globalTheme = R.style.Sherlock___Theme_Light;
setTheme(globalTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
I did not add something in the manifest, because the themes should be changed dynamically
AndroidManifest.xml
<uses-sdk android:minSdkVersion="7"
/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="com.belasheuski.activities.MyApplication"
>
<activity
android:name="com.belasheuski.activities.MainActivity"
android:label="#string/name_main"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This error occurs on API 7. On API 15 all works very well.

I'm setting this in this way ...
setTheme(isLightTheme ? R.style.MyApp_Light : R.style.MyApp);
where the style definition are looking like ...
<style name="MyApp.Light" parent="#style/Theme.Sherlock.Light.ForceOverflow">
<item name="overlayedActionBarBackground">#color/overlayedActionBarLight</item>
...
</style>
<style name="MyApp" parent="#style/Theme.Sherlock.ForceOverflow">
<item name="overlayedActionBarBackground">#color/overlayedActionBarDark</item>
...
</style>
That works pretty fine. So I think you took the wrong style definition from ABS.
Cheers!

The error message tells you what is wrong, your theme must be "Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative."
This is occuring on API 7 but not on API 15 because API 15 has native ActionBar support, so that is used, while this is not the case on API 7, and hence the need for the theming.

Going from the error:
You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative."
It seems that your custom style does not inherit from a Sherlock theme. Check the styles.xml file in your values folder and ensure they inherit from one of those.

Related

Why progress dialogue is not showing title and message on marshmallow?

In my app, I am integrating progress dialogue and I am using below code for creating progress dialogue
public static void showProgressDialogue(final Context context, String title, String mainMessage) {
progressDialog = new ProgressDialog(context);
progressDialog.setTitle(title);
progressDialog.setMessage(mainMessage);
progressDialog.setIndeterminate(false);
progressDialog.show();
}
And I created 2 emulators one for lollipop and other for marsh mallow. It is showing title and message on lollipop emulator but not showing title and message on marsh mallow emulator. Further I have tried on real device as well. On marsh mallow (Motorolla droid turbo) device it is not showing title and message. For sake of giving complete understanding i am attaching images as well
Above images is taken of lollipop emulator
Above image is taken from marsh mallow emulator
I have googled a lot and could not fine suitable answer that solve my query. Even in one place I found that by changing 'textColorPrimary', it will work. But in my case it did not work. And below is my style.xml code
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:textViewStyle">#style/LoyaltyTextView</item>
<item name="editTextStyle">#style/LoyaltyEditText</item>
</style>
Any help?
As requested, I am posting my manifest as well
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".views.activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<activity android:name=".views.activities.MainActivity"/>
<activity android:name=".views.activities.LoginActivity"/>
<activity android:name=".views.activities.SignUpActivity"/>
<activity android:name=".views.activities.ViewAsGuest"/>
<activity android:name=".views.activities.BusinessActivity"/>
</application>

Activity does not show title bar in Android Studio

I have create demo application in Android Studio with two Activity. In First Activity Show Title Bar in preview as well as in emulator but on second Activity does not show Title Bar in Emulator but it display in preview. I have used API 22 in Android Studio.
Please help me....thanx in advn
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/BaseTheme">
<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=".DrawingActivity"
android:label="#string/app_name"
android:theme="#style/BaseTheme"
android:screenOrientation="portrait" />
</application>
Theme style file like this....
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="BaseTheme" parent="AppTheme">
<item name="colorPrimary">#color/action_bar_backgound</item>
</style>
I have also try this...
First Activity Work's fine so I have set that in Second Activity as a setContentView(R.layout.activity_main);
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonAction(View v) {
Intent intent = new Intent(MainActivity.this, DrawingActivity.class);
startActivity(intent);
}
}
DrawActivity.java
public class DrawingActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
What you are facing is a bug related to the API 22(5.1) SDK.
Kindly refer the following links for the details on the bug in which the Action Bar is not displayed:
https://code.google.com/p/android/issues/detail?id=159793
https://code.google.com/p/android/issues/detail?id=159780
What you can do for now
Either revert back to API 21 if possible or,
Select API 21 in the compile with (render with) option in Android Studio.
Hope this helps.

getActionBar() is always returning null

I have gone through lots of similar posts in Stackoverflow, but nothing seems to resolve my issue.
I am trying to get the ActionBar and invoke setDisplayHomeAsUpEnabled(true).
I can see the black ActionBar (as the theme is black background) with the Activity name displayed as title. But in my activity code, if I try to do getActionBar() it always returns null.
I tried the code in onResume() and onCreate() (after inflating the layout). Everywhere it returns null. Is there anything I am missing?
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityMain"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is my styles definition. I am using Theme.AppCompat.Light.DarkActionBar theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Also, I am using following dependency:
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support:appcompat-v7:22.2.0'
and my activity class definition is:
MainActivity extends AppCompatActivity
Try using getSupportActionBar(). The latest appcompat-v7 seems to disable the systembar.
If your class defenition is MainActivity extends AppCompatActivity, you should try using getSupportActionBar()
If your class defenition is MainActivity extends Activity, you can try using getActionBar() (this call requires API 11)

Android 5.0 Back icon parent activity

I'm migrating my projects to new Material design / Android 5.0 Lollipop now.
In previous Android versions it was easy to create Activity with back ActionBar button (arrow) using android:parentActivityName in AndroidManifest.xml. But it seems that in doesn't work any more on new API with support libraries com.android.support:appcompat-v7:21.+.
Below is my code and screenshots from previous and updated to Lollipop support example project:
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="org.serge.androidprobe.app.MainActivity"
android:label="Activity1:Parent" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="org.serge.androidprobe.app.SecondActivity"
android:parentActivityName="org.serge.androidprobe.app.MainActivity"
android:label="Activity2" >
</activity>
</application>
Before migration to Lollipop:
public class MainActivity extends Activity {/**/ }
public class SecondActivity extends Activity {/**/ }
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"/>
</resources>
After migration to Lollipop:
public class MainActivity extends android.support.v7.app.ActionBarActivity {/**/ }
public class SecondActivity extends android.support.v7.app.ActionBarActivity {/**/ }
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat"/>
</resources>
Note: before migration to Lollipop I haven't used support/appcompat libs at all.
How to make back arrow visible & clickable on the top-left corner using new v21 of support/appcompat libraries?
In yours onCreate try calling
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
You then handle the click by checking android.R.id.home in onOptionsItemSelected

android activity call requestWindowFeature first and show the title

My Activitys use a theme which set android:windowNoTitle = true.But I have to show the title in some activities. I want show the title in my activity by call requestWindowFeature(), but it's not work! Any help would be great.
Thanks.
ps:
My activitys hava to be switched between two themes.The homeActivty use the actionbar and others no title.These activtys are use the same themes.So when before use requestWindowFeature() it's been hiding, and i just want show it now.
Here is my codes:
Defined theme:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowAnimationStyle">#style/activityAnimation</item>
<item name="android:windowNoTitle">true</item>
<!-- <item name="android:background">#color/background_holo_light</item> -->
</style>
And use the theme in application:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Get and change the theme if necessary:
int mThemeId = mSettingPreferences.getInt(MySettings.THEME_ID, -1);
if (-1 != mThemeId) {
this.setTheme(mThemeId);
}
// do something to show the title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.yourlayout);
You don't need to apply any Theme if you use this.
you have to use this method before your setContentView method...
requestWindowFeature(Window.FEATURE_NO_TITLE);
OR
And yes you can add theme in Manifest file in which Activity you don't want to Title..Then use this..
<activity
android:name=".activity name"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:screenOrientation="potrait" >
</activity>
first change this:
if (mThemeId != -1) {
this.setTheme(mThemeId);
}
and after that you can use
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
<activity
android:name=".HistoryImage"
android:screenOrientation="sensorPortait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
</activity>
<activity
android:name=".HistoryVideo"
android:screenOrientation="sensorPortait" >
</activity>
<activity
android:name=".HistoryVideo_2"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:screenOrientation="sensorPortait" >
</activity>
you must call requestWindowFeature(); before setContentView()
The activities in which you don't want to show the title bar. just do this to hide the title bar. befor setcontentView requestWindowFeature(Window.FEATURE_NO_TITLE);
I hope this will help you
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_page);

Categories

Resources