Setting custom span on TextView with style applied in XML - java

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.

Related

Adding components programmatically has different style than those in XML layout

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>

How to customize links in TextView?

I'm using links in my Android Studio project activity via setMovementMethod(LinkMovementMethod.getInstance());.
By default links are color of AppTheme's Accent color and have a bottom line (underline). So how to customize links in TextView: remove underlining, change color and text style?
You could override the android:textColorLink attribute to change the text color.
You can define your own custom styles and then apply them to your textview in the layout xml file.
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
See https://developer.android.com/guide/topics/ui/themes.html for more details.

Android - how to disable / change editTexts 'Pin'

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>

Update picture inside of the button (xml) from java code [duplicate]

This question already has answers here:
How to set property "android:drawableTop" of a button at runtime
(8 answers)
Closed 9 years ago.
(java/xml for android 2.1 or higher)
I'm creating button using style:
<style name="MainButton">
<item name="android:textColor">#000000</item>
<item name="android:text">test</item>
<item name="android:drawableRight">#drawable/ic_launcher</item>
<item name="android:drawablePadding">10dip</item>
<item name="android:paddingLeft">10dip</item>
</style>
I know that I can change text (from my java code), for example like this:
Button but = (Button)findViewById(R.id.button2);
but.setText(Html.fromHtml("<b>" + "title" + "</b>" + "<br />" +
"<small>" + "description" + "</small>" + "<br />" +
"<small>" + "DateAdded" + "</small>"));
And it looks very well, but
How can I change my drawable object inside of that button from my java code?
I dont want to use "button.setBackgroundResource();" method.
Any ideas? :>
I think you want a custom selector
Create a custom selector and set it as a drawable resouce in xml.
Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/cell_top_selected" />
<item android:drawable="#drawable/cell_top" />
</selector>
and
use this statements
yourbutton.setSelected(true) // for selection image
yourbutton.setSelected(false) // for alternate image
if you don't want this to
I dont want to use "button.setBackgroundResource();" method.
If you don't want to use setBackGround Resouce then
Take a TextView instead, Super class of Button is TextView, by default TextView don't have any background drawable resource.
make another xml with the same attributes but change '#drawable/' resource. and on the onclick method set the background resources of the button to this new xml
button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
You can either use resource ids or drawables
If I have understood the question correctly and I am assuming you want to completely change the button and draw your own then you will have to create your own Button. This can be done by creating a new class and then extending it with Button and then override the onDraw() method of the View class. Here in this method you can draw what ever you like i.e. background, text, borders..etc.
For example
class NewButton extends Button{
protected void onDraw (Canvas canvas){
//Draw here
}
}
I hope this helps.

Change the Dialog style using a theme

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);

Categories