android set custom attributes in styles.xml and get in program - android

I am trying to get a button custom attribute to return true/false using custom attributes defined in styles.xml. My example is trivial with only two buttons but I can't get it to work.
My layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".CustomAttr">
<Button
android:id="#+id/button_1"
style="#style/red_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1!" />
<Button
android:id="#+id/button_2"
style="#style/blue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2!" />
</LinearLayout>
<resources>
styles.xml looks like:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<attr name="is_red" format="boolean"/>
<style name="red_button" >
<item name="android:background">#ffff0000</item>
<item name="is_red">true</item>
</style>
<style name="blue_button" >
<item name="android:background">#ff0000ff</item>
<item name="is_red">false</item>
</style>
</resources>
And the code looks like:
public class CustomAttr
extends AppCompatActivity
implements View.OnClickListener {
private static final String TAG =
CustomAttr.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_attr);
findViewById(R.id.button_1)
.setOnClickListener(this);
findViewById(R.id.button_2)
.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
int [] searchAttr = {R.attr.is_red};
TypedArray attrs
= v.getContext()
.obtainStyledAttributes(null, searchAttr);
boolean isRed = attrs.getBoolean(0, false);
Log.d(TAG, String.format("%s:%b"
, (id == R.id.button_1) ? "button 1" : "button 2"
, isRed )
);
}
}
Everything compiles fine and the button colors are working and I'm not getting any warnings. But the boolean isRed in the onClick method always returns false.
I've been looking through the net and docs all day and this looks like it should work -- but it doesn't. What am I doing wrong?
Thanks
Steve S.
********* EDIT Fri Sep 21 10:17:01 PDT 2018 *********
As noted below, this is a prototype for an app with about 250 different buttons in a gridview. There are about 250 of basicly 4 different types and I can set almost everything in each button using one of 4 different styles. I was already using the tag and text fields and really needed a way to detect the button's type (1 0f 4). I finally created a custom button view with it's own attribute set. The code for the working prototype custom view button with its attribute on github. Thanks!
Steve S.

You haven't used the is_red attribute in your xml. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CustomAttr">
<Button
android:id="#+id/button_1"
custom:is_red="true"
style="#style/red_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1!" />
<Button
android:id="#+id/button_2"
custom:is_red="false"
style="#style/blue_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2!" />
</LinearLayout>

Related

Change Text color depending on Theme

How do i change the text color depending on the theme in Android Studio with setTextColor()?
So that when dark mode is enabled it changes the Text to white and when white mode is enabled it changes to black
this may be not exactly what you want but it might help you.
it doesn't matter what layout you use i'm just going to provide dummy code so you can understand.
Blockquote
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:text="Dark theme text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_gravity="center"
android:textColor="#color/colorPrimaryDark"
android:text="Dummy text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
note : In order to change the color depending upon the current theme light or dark, it has to be dynamic.
now implement Dark theme:
create a new resource file and call attributes.xml, inside this file we will be declaring some attributes that we need for setting colors for widgets and layout.
and think of these attributes as controllers for colors in our layout.
add this snippet to attributes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="metaColor" format="color"/>
<attr name="color" format="color"/>
<attr name="textColor" format="color"/>
</resources>
After that, we need to create two theme, light and dark in styles.xml file
inside styles.xml, here we make use of our attributes, add this code snippet:
<style name="Light" parent="AppTheme">
<item name="textColor">#000000</item>
<item name="metaColor">#606060</item>
<item name="color">#ffffff</item>
</style>
<style name="Dark" parent="AppTheme">
<item name="textColor">#ffffff</item>
<item name="metaColor">#aeaeae</item>
<item name="color">#000000</item>
</style>
then go to activity_main.xml
update it with this code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:textColor="?attributes/textColor"
android:text="Dark theme text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="?attributes/metaColor"
android:text="Dummy text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
now go to MainActivity and add this inside of onCreate function like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Dark);
setContentView(R.layout.activity_main);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Light);
setContentView(R.layout.activity_main);
}
and here is a really cool repository to use to create your custom themes and change them dynamically with ripple animation:
https://github.com/imandolatkia/Android-Animated-Theme-Manager

Button with style defined in XML works; not same if style set in code

I'm trying to add a style to a button in Xamarin Android from code, because the buttons will be generated dynamically and need to change colors based on other system events.
Using the code shown here, I expect that the two rows, one rendered from XML and the other from code, will look exactly alike. Instead, I see that the style ButtonDefaultTheme is only applied to the first row.
I know setting the style from code is possible based on this answer and this one. I think I could accomplish what I need using a layout inflater as in this answer, but I'm very curious why the code I have won't work.
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
LinearLayout roomButtonLayout = FindViewById<LinearLayout>(Resource.Id.roomButtonLayout);
LinearLayout roomInfo = new LinearLayout(this)
{
Orientation = Orientation.Horizontal,
LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent, 1f)
};
roomInfo.SetGravity(GravityFlags.CenterVertical);
roomInfo.SetPadding(20, 10, 0, 10);
TextView roomNameDisplay = new TextView(this)
{
Gravity = GravityFlags.Left,
TextSize = 20, // defaults to scaled pixels
Text = "Room 100",
LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent, 1f)
};
roomInfo.AddView(roomNameDisplay);
Button button2 = new Button(new ContextThemeWrapper(this, Resource.Style.ButtonDefaultTheme), null, 0)
{
Text = "View2"
};
roomInfo.AddView(button2);
roomButtonLayout.AddView(roomInfo);
}
}
In Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_height="wrap_content"
android:id="#+id/roomButtonLayout">
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="0dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:gravity="left"
android:textSize="20sp"
android:text="Room 100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="View1"
style="#style/ButtonDefaultTheme" />
</LinearLayout>
</LinearLayout>
In styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="ButtonDefaultTheme" parent="android:style/Widget.Holo.Button">
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:minWidth">100dp</item>
</style>
</resources>
Result:
Minimum API level needed: 19
Compiling using: 21 in actual app, 23 in test app. Same visual results in both.

Android Preference Fragment Text Color

I want to change the text color in android preferences fragment. I am currently using a custom theme to change the checkbox image, background, onClick highlighting and it all works great...besides the text color. I don't want to use a default theme, I want to have my own theme so it all looks how I want to but just change the text color, can someone please help.
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="selectedTextStyle">
<item name="android:textSize">18sp</item>
</style>
<style name="buttonTextStyle">
<item name="android:textSize">20sp</item>
</style>
<style name="PreferencesTheme" >
<item name="android:windowBackground">#drawable/lists_background</item>
<item name="android:listViewStyle">#style/listViewPrefs</item>
<item name="android:checkboxStyle">#style/MyCheckbox</item>
</style>
<style name="MyTextAppearance" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/black</item>
</style>
<style name="listViewPrefs" parent="#android:Widget.ListView">
<item name="android:listSelector">#layout/list_selector_master</item>
<item name="android:textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyCheckbox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">#drawable/btn_check</item>
</style>
</resources>
Manifest:
<activity
android:name="com.package.SettingsActivity"
android:theme="#style/PreferencesTheme"
android:configChanges="keyboard|orientation" >
</activity>
Activity:
import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class SettingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
Here are the two TextView objects from preference.xml (layout for a Preference):
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
So looks like you can override textAppearanceLarge and textColorSecondary in your theme to achieve a color change. Here's an example:
<style name="AppTheme">
<item name="android:textAppearanceLarge">#style/MyTextAppearance</item>
<item name="android:checkboxStyle">#style/MyCheckBox</item>
</style>
<style name="MyCheckBox" parent="#android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">#android:drawable/btn_star</item>
</style>
<style name="MyTextAppearance" parent="#android:style/TextAppearance.Large">
<item name="android:textColor">#ff0000</item>
</style>
I think an easy and clear way to do that is next:
res/values/styles.xml
<style name="SettingsFragmentStyle">
<item name="android:textColorSecondary">#222</item>
<item name="android:textColor">#CCC</item>
<item name="android:background">#999</item>
...
</style>
In the Activity that contains PreferenceFragment subclass inside onCreate:
setTheme(R.style.SettingsFragmentStyle);
Just found an answer that gets the job done.
This file is default layout for the preference list item:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Layout for a Preference in a PreferenceActivity. The
Preference is able to place a specific widget for its particular
type in the "widget_frame" layout. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize"
android:background="?android:attr/selectableItemBackground" >
<ImageView
android:id="#+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
You can use it as base and create your own custom layout. This layout must be applied within each Preference like this
<Preference android:key="somekey"
android:title="Title"
android:summary="Summary"
android:layout="#layout/custom_preference_layout"/>
You can change the whole layout outside the list, you just need to have a layout file that contains a listview where the preferences will be populated like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
To override the default layout for the PreferenceFragment, override the method onCreateView and change the inflated view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.custom_options_layout, null);
}
Based off these answers:
Creating a custom layout for preferences
How to add a button to PreferenceScreen
Android: How to adjust Margin/Padding in Preference?
To me nothing of the above methods didn't work. I ended up extending Prefernces class of the kind I used, in my case CheckBoxPrefernces:
public class MyPreferenceCheckBox extends CheckBoxPreference {
public MyPreferenceCheckBox(Context context) {
super(context);
}
public MyPreferenceCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyPreferenceCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected View onCreateView(ViewGroup parent) {
ViewGroup root = (ViewGroup) super.onCreateView(parent);
((TextView)root.findViewById(android.R.id.title)).setTextColor(parent.getResources().getColor(R.color.red));
((TextView)root.findViewById(android.R.id.summary)).setTextColor(parent.getResources().getColor(R.color.red));
return root;
}
}
And the use in the prefs.xml:
<com.my.package.name.MyPreferenceCheckBox
android:title="#string/my_title"
android:defaultValue="true"
android:key="#string/my_key"
android:summary="On/Off" />
This answer showed me that path:
Haven't got enough rep to comment or upvote, but I just wanted to add that mharper's suggestion about TextAppearanceMedium also worked for me in regards to changing the text colour. If somebody knows why this is, please do explain it.
So just adjust the accepted answer like so:
<style name="AppTheme">
<item name="android:textAppearanceMedium">#style/MyTextAppearance</item>
</style>
<style name="MyTextAppearance" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#ff0000</item>
</style>
At least if nobody can explain why this is, it might stop somebody having the same difficulty I did trying to change the text colour.
I was able to change text color in PreferenceFragment with this:
styles.xml
<resources>
<style name="SettingsTheme" parent="android:Theme.Holo">
<item name="android:textColorPrimary">#color/light_blue_font_color</item>
<item name="android:textColorSecondary">#color/white_font_color</item>
<item name="android:textColorTertiary">#color/white_font_color</item>
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="light_blue_font_color">#33b5e5 </color>
<color name="white_font_color">#ffffff </color>
</resources>
AndroidManifest.xml
<activity
android:name="xx.yy.zz.SettingsActivity"
android:theme="#style/SettingsTheme" >
</activity>
If you're using compat library, just add:
<item name="colorAccent">#ff0000</item>
to your style
I work with min API 11 and compile whith 23.
set theme and after set background for this theme.
if you work with > API 14 you can use Theme_DeviceDefault.
This is the simplest solution I find :
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTheme(android.R.style.Theme_Holo);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if(view != null){
view.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.background_dark));
}
}
}

Android: Change text color in ListView for singleChoice

I have a listview set to use singleChoice. All I want to do is change the default background color to white and the text color to black. I cannot figure out how to do this. Here is my xml layout:
<ListView
android:id="#+id/lvSpeeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/llToolbar"
android:layout_below="#id/rgSpeedUnits"
android:textColor="#android:color/black"
android:choiceMode="singleChoice"
android:background="#ffffff"
android:cacheColorHint="#00ffffff"
android:clickable="true"
android:divider="#ff000000"
android:dividerHeight="1dp"
android:focusable="true"
android:scrollingCache="true" />
EDIT: I should have pointed out that I want to change this only using xml layout files and NOT in code. I already know how to do this in code. Using a custom layout other than android.R.layout.simple_list_item_single_choice forces you to implement an adapter, bind, write more code, and so on. From viewing a lot more posts, it does not appear possible to change the text color using only xml. In fact, it doesn't seem possible to change anything on a row as the underlying layout android.R.layout.simple_list_item_single_choice is not accessible.
for a list view Use android selector
like this
and save it with anyname.xml and giveits reference to background of your list
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/about_btn_hover"></item>
<item android:drawable="#drawable/about_btn"></item>
</selector>
and for changing text color add color folder in your res directory
create an xml save it textchangecolor.xml
and add the following lines to it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/whiteColor"></item>
<item android:color="#color/bluetxt"></item>
</selector>
and give its refernce to the textcolor
try to this:
insert to this code in adapter in getview mathod:
LinearLayout mRowLayout = (LinearLayout) vi
.findViewById(R.id.list_item_layout);
final TextView title = (TextView) vi
.findViewById(R.id.list_item);
title.setText(Massage[position]);
mRowLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
title.setTextColor(Color.RED);
});
here list_item code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list_item_layout"
android:orientation="vertical" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/row_single"
android:layout_margin="5dp">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/black"
android:padding="10dp"
android:textSize="16sp"
android:layout_toLeftOf="#+id/arrow"
android:id="#+id/list_item"/>
</RelativeLayout>
</LinearLayout>
The best solution I could find was to use styles.xml and set a theme for my dialogs.
styles.xml
<style name="DialogTheme" parent="AppTheme">
<item name="android:textColor">#color/text</item>
<item name="android:textColorAlertDialogListItem">#color/text</item>
+ other styles
</style>
In Java Built Dialogs:
ContextThemeWrapper theme;
theme = ContextThemeWrapper(view.getContext(), R.style.DialogTheme);
AlertDialog.Builder builder = new AlertDialog.Builder(theme);
In XML built Dialogs:
<myLayout
....
android:theme="#style/DialogTheme" />

Calling android dialog without it fading the background

I have this nice dialog view I set my UserInputDialog class to:
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/nameMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What is your name Captain?"
>
</TextView>
<EditText
android:id="#+id/nameEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK">
</Button>
<Button android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
I want my dialog to show up but for the background to not fade out. Is this possible? Cause the view that calls this dialog has a neato background I would like to be shown as a backdrop to the dialog.
I found this online:
<style name="doNotDim" parent="#android:style/Theme.Dialog">
<item name="android:backgroundDimAmount">0</item>
</style >
but not sure how to apply that to my dialog? I have a class called public class UserInputDialog extends Dialog implements OnClickListener. It sets its content view to the layout described above.
I think I am doing this all right, just not sure how to add that style so I can NOT fade the background.
Secondary question: Can you get new looks on your dialog (by say having an image or icon display with your text there) by using Themes?
You can also remove the dim effect by code with the following:
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Create a res/values/styles.xml file and add this to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.DoNotDim" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
And apply the theme to your activity.
<activity android:name=".SampleActivity" android:theme="#style/Theme.DoNotDim">
Create a custom style and place it in your values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomStyle" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
To set a style to an individual dialog you can use
Dialog dialog = new Dialog(context, R.style.YourCustomStyle);
For displaying dialog like TrueCaller do this:
In styles.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle" parent="android:Theme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
In AndroidManifest.xml do this:
<activity android:name=".SampleActivity" android:theme="#style/myBackgroundStyle">

Categories

Resources