I wished to make one of my activities to look like a dialog and used a Theme.AppCompat.Dialog theme for it, but it made it's action bar to look bad (see below).
Now background is cut to the length of the title string and I cant't find any theme property to fix it.(
What can be done to avoid it?
Related part of styles.xml:
<style name="DeviceListTheme" parent="Theme.AppCompat.Dialog">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
I start the activity using the following code:
Intent intent = new Intent(this, DeviceListActivity.class);
startActivityForResult(intent, REQUEST_CONNECT_DEVICE);
First, when I encountered this problem, I tried using supportRequestWindowFeature(Window.FEATURE_NO_TITLE); but this didn't work for me and had no effect.
The alternative method to remove the bar at the top of your dialog activity would be to create a custom style and apply it to that activity.
In styles.xml, create a new style like so:
<style name="MyCustomDialog" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Now, in AndroidManifest.xml, add in android:theme="#style/MyCustomDialog" to your activity.
Of course, MyCustomDialog can be renamed to anything you want.
Use DialogWhenLarge instead of standard Dialog style:
<style name="MyDialogTheme" parent="#style/Theme.AppCompat.Light.DialogWhenLarge">
...
</style>
I solved the same problem this way:
Activity:
public class MyActivity extends android.support.v4.app.FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
setTitle("View task");
}
}
Theme:
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
Manifest:
<activity android:name=".MyActivity" android:theme="#style/Theme.MyDialog"/>
Result:
Its Working
<style name="AppThemeDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerStyle">#style/holoSpinner</item>
</style>
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
setTitle("Batches");
}
Related
I change the theme from the design section from the dropdown menu like material dark, holo, etc. But when I run the app on the device the app doesn't show any changes, hence I understood the code does not change so what is the code I need to write in style section to change the theme?
AndroidStudio only shows how it will look if you will apply that theme in your app programmatically by yourself.
In res/values/styles.xml you can add following code to change theme:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
<item name="android:colorControlActivated">#color/colorPrimaryDark</item>
<item name="android:colorControlHighlight">#color/colorPrimary</item>
<item name="android:colorControlNormal">#color/colorPrimary</item>
<item name="android:windowBackground">#color/colorBackground</item>
</style>
</resources>
you can change theme by modifying parent attribute
You should edit the application manifest and add
android:theme attribute to either application or activity tag with the theme you want.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
if(mode.equals("dark")){
setTheme(android.R.style.DarkTheme);
}else if(mode.equals("light")){
setTheme(android.R.style.LightTheme);
}else{
setTheme(android.R.style.AppTheme);
}
}
I'm a bit puzzled. I'm using a DayNight theme in my app (using AppCompatDelegate.setDefaultNightMode()), but can't get it to work in my MainActivity.
The MainActivity (which extends FragmentActivity) looks like it's never set to dark theme - it always remains in light theme.
I tried setting the theme directly in my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
super.onCreate(savedInstanceState);
// create main activity.
}
But this is not working.
I have set all of the colors in my layout files properly using ?attr/colorReference. Does anyone know what is going wrong here?
EDIT:
My styles.xml is as follows:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!--Default typeface and colors:-->
<item name="android:typeface">monospace</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorAccentDarker">#color/colorAccentDarker</item>
<item name="colorAccentDarker_80percent">#color/colorAccentDarker_80percent</item>
<!--Show people's own wallpaper background-->
<item name="android:windowShowWallpaper">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
MainActivity (which extends FragmentActivity)
FragmentActivity has no idea of AppCompat. AppCompatDelegate is only used by AppCompatActivity or you have to wire it manually to your other activities.
You can extend AppCompatActivity instead of FragmentActivity.
I am trying to add materiel design in my project. Whan I try to apply new theme I am facing some problem. Action bar is hided when I apply this theme to my activity.
Style:
<style name="MaterialBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/GreenActionBar</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:popupMenuStyle">#style/PopupMenu</item>
<item name="android:dropDownListViewStyle">#style/DropDownListView</item>
<item name="android:textAppearanceLargePopupMenu">#style/PopupMenuTextAppearance</item>
</style>
Manifest:
<activity
android:name=".Summary"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/summary"
android:launchMode="singleTop"
android:theme="#style/MaterialBaseTheme"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden" />
Class:
public class Summary extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.route_summary);
super.onCreate(savedInstanceState);
// parseView();
// updateUI();
}
}
Here Action bar is hided for this activity.
When I try to extend AppCompatActivity in class I am getting flowing error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coc.Summary}:
java.lang.IllegalArgumentException: AppCompat does not support the current theme features
Please let me any idea to resolve my problem.
Please add other styles for theme
<style name="MaterialBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/**GreenActionBar**</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:popupMenuStyle">#style/**PopupMenu**</item>
<item name="android:dropDownListViewStyle">#style/**DropDownListView**</item>
<item name="android:textAppearanceLargePopupMenu">#style/**PopupMenuTextAppearance**</item>
</style>
use this
public class MainActivity extends AppCompatActivity
Why doesn't this style I have result in the action bar not being displayed?
My activity is descended from appcompat. I assume these are all the correct properties.
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="Theme.AppCompat.Light">
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
it is taking effect, cause the status bar gets removed. But the action bar doesn't seem to get affected.
I can still successfully remove the action bar like this:
public class MainMenuScreen extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu_screen);
but i have to admit I'm a little bit disappointed that I can't do it from xaml.
Can you show your manifest? Where do you associate the theme with the activity?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="BaseTheme" />
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/cobalt_light</item>
<item name="colorPrimaryDark">#color/cobalt</item>
<item name="colorControlHighlight">#color/cobalt_lighter</item>
<item name="colorAccent">#color/cobalt_light</item>
</style>
</resources>
You dont need to extends from parent Theme.AppCompat.Light
I'm writing an app which will be on embedded device, and I need to remove ActionBar alongside the TitleBar and ContentOverlay.
I found a solution and inserted the following in my styles:
<style name="NoActionBar" parent="#android:style/Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
and also add the line to my activity in AndroidManifest:
android:theme="#style/NoActionBar"
and in the end I got no title bar, no content overlay, however the ActionBar is still there. Please advice. I use android:theme="#android:style/Theme.Holo.Light" and my targetSdkVersion is 18.
Small mistakes in XML can cause very strange problems,
which are not always reported accurately by the build process.
I tend to shy away from changing XML, when I can do it in java.
Java gives you more control.
programmatically remove action bar
----------------------------------
from normal activity
getActionbar().hide();
getActionbar().show();
if your using support activity
getSupportActionBar().hide();
getSupportActionBar().show();
and from the Fragment you can do it
getActivity().getActionbar().hide();
getActivity().getActionbar().show();
remove title bar
----------------
public class ActivityName extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
ActionBar ContentOverlay
------------------------
1) Request for actionbar overlay programmatically by calling
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
// this 'requestFeature()' must be requested from your activity 'onCreate()' method
// before calling for 'setContentView(R.layout.my_layout)':
2) set the actionbar color by calling setBackgroundDrawable() like so:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00212121")) );
Try changing your style's parent like this :
parent="#android:style/Theme.Holo.Light.NoActionBar"
Example
<style name="NoActionBar" parent="#android:style/Theme.Holo.Light.NoActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>