Components (text and edit boxes) defined in layout XML looks very different from those i add programatically.
I've tried to apply the same textAppearance style programatically as the ones from my XML has. I tried calling setTheme() after adding the components too. No difference.
TextView tv10 = new TextView(getApplicationContext());
tv10.setText("Back width");
EditText tv11 = new EditText(getApplicationContext());
tv11.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
TextView comes out with small font and gray text, EditText comes out with black background and gray text.
The components must be added programmatically because of options the user choose. Those user choices are defined in the XML and follow the expected color scheme, which is the Android Studio defaults. (Black text on white background)
You shouldn't use Application Context for views. Only Activity context. Also you can pass style as parameter, when create view in code.
As Yamko said, style can be passed in the constuctor
var textView = TextView(context, null, R.style.LoginBodyTextViewStyle)
where the style can be something like
<style name="LoginBodyTextViewStyle" parent="android:Widget.TextView">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">#dimen/text_size_default</item>
<item name="android:textColor">#color/grey</item>
</style>
Related
I created alert dialog with TextInputLayout programmatically in fragment. I need to hide password by default, TextInputLayout has toggle button and it is working as expected hide/show on click. I tried to keep password hidden by default by settingsingleline to true and other hack i got from other SO answers but i still not able to get desired result. Any idea why i am not getting desired result?
TextInputLayout passwordParent = new TextInputLayout(getActivity());
passwordParent = new TextInputLayout(getActivity());
TextInputEditText passWord= new TextInputEditText(getActivity());
passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
passWord.setBackgroundResource(R.drawable.background);
passWord.setBackgroundColor(getResources().getColor(R.color.white));
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
passwordParent.addView(passWord);
Background XML Selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_remove_red_eye_black_24dp" android:state_checked="true"/> // Visible Eye Icon
<item android:drawable="#drawable/ic_visibility_off_black_24dp"/> // Hide Eye Icon
</selector>
Edit: It's working by adding InputType.TYPE_CLASS_TEXT into input type like this passWord.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); but the icon still stay the same, its still showing wrong eye icon.
Try moving addView(passWord) up by two lines, like this :
passwordParent.addView(passWord);
passwordParent.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
passwordParent.setEndIconDrawable(R.drawable.show_password_selector);
How can I edit or disable the 'pin' for editTexts that appears when marking text? I was able to edit the underline & the cursor, but not that 'pin' because I didn't know how to call it.
Image of the pin (pink) added.
I think the colourAccent influences the colour of that pin, try changing the colour of colourAccent in color.xml
This 'pin' is called "text select handle"
To change it you need to use these attributes:
text_select_handle_left and text_select_handle_right
Add these drawables with customized design/color to your drawable folder and add to style
<style name="MyTheme" parent="#style/MyCustomTheme">
<item name="android:textSelectHandle">#drawable/text_select_handle_middle</item>
<item name="android:textSelectHandleLeft">#drawable/text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/text_select_handle_right</item>
</style>
I have a TextView that uses an app wide style set in XML that I would like to keep in the XML definition. Now if I find a special string in a message I want to set on the TextView I would like to underline it. Now this is pretty simple using the UnderlineSpan class and setting the stand and end. The problem is when I have a style set in XML the span is completely ignored, if I remove the style the underline is correct. How can I have a style applied in XML and be allowed to apply custom spans?
EDIT:
My style in the styles.xml file is this:
<style name="Heading3Style">
<item name="android:textAllCaps">true</item>
<item name="android:textColor">#color/black_red</item>
<item name="android:textSize">#dimen/text_heading_1</item>
<item name="font">ROCKWELL_STD_BOLD</item>
</style>
The font attribute is a custom attribute that is pulled by a class extending TextView to load a custom font from XML. My layout file has this definition:
<com.mypackage.views.font.FontTextView
android:id="#+id/titleView"
style="#style/Heading3Style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/standard_margin" />
This is the code where I am trying to apply a span to the text. For simplicity I just used the UnderLine span on the entire string to test but this doesn't work. Once I have the Spannable working I will replace this with a custom Spannable to make changes to only parts of the string.
Spannable spannable = new SpannableString(modifierGroup.getName());
spannable.setSpan(new UnderlineSpan(), 0, modifierGroup.getName().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
titleView.setText(spannable);
This does nothing to the string, it just displays it like normal with the Heading3Style applied but no underline. If I remove the style="#style/Heading3Style from the XML definition everything works as expected.
I was wondering whether there is a simple way to tell Android to always use a custom style to display Dialogs.
Using themes and styles you can change the look and feel of all, say, TextViews using this piece of code when defining a theme in themes.xml:
<item name="android:textViewStyle">#style/Blue.TextView</item>
with Blue.TextView being defined in styles.xml.
Is there some way to do so for Dialogs as well ?
yes, there is. for example,
AlertDialog.Builder b = new AlertDialog.Builder(context, R.style.MyTheme);
or, if you are using a DialogFragment, just call the setStyle() method,
<style
name="Theme_Dialog_Translucent"
parent="android:Theme.Dialog">
<item
name="android:windowBackground">#null</item>
</style>
Below is the working code::
Dialog mDialog = new Dialog(this,R.style.ThemeDialogCustom);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setCanceledOnTouchOutside(true); //But here It is not workin
mDialog.setContentView(R.layout.popup);
I am creating a custom Dialog with the following style:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I put the following construction of the dialog:
public CustomDialog(Context context) {
super(context, R.style.FullHeightDialog);
setContentView(R.layout.custom_dialog_layout);
}
The custom_dialog_layout root is RelativeLayout with layout_margin="20dp", and inside I put LinearLayout with 3 simple Buttons.
I expect that the Dialog stretches to the full height and to the content width, in this case the LinearLayout with the 3 buttons.
It works fine on Android Gingerbread, BUT on Android 4, it never stretches more than ~40% of the screen.
On Android 4, I have to programatically set:
LayoutParams params = getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
To make it work as expected. Even then, it shows non expected results, but nontheless, acceptable.
Is this a Bug of Ice Cream Sandwich or I am doing something wrong?
your problem might come from android:style/Theme.Dialog since it can evolve depending on android version.
solutions :
Define fully your own style with no reference to android:style/Theme.Dialog
refers to android:style/Theme.Dialog but add margins/paddings layout definitions in your style items