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.
Related
I have a button with centralized icon+text, and I need to change the app:icon and android:text properties when some event occurred. I know that there is setText() method to change the text, but is there a way to change the icon?
XML:
<Button
android:id="#+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="#dimen/default_margin"
android:layout_marginVertical="#dimen/bottom_bar_content_vertical_margin"
android:backgroundTint="#color/light_green"
android:text="#string/next"
android:textAllCaps="false"
app:icon="#drawable/ic_baseline_next_plan_24"
app:iconGravity="textStart" />
This is a function that is called after appropriate event, and I need to change icon in that function. icon and text are ids of desirable drawable and string:
private void setBottomButton(int icon, int text) {
button.setText(getString(text));
}
You can use the methods:
setIcon
setIconResource
Example:
button.setIcon(drawable)
button.setIconResource(iconId)
Here the doc.
If you check the docs, you'll see the code equivalent for each XML attribute.
Check this link : enter link description here
Searching for drawableLeft shows:
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)
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 am using a built-in theme for my Android app:
<style name="AppTheme" parent="android:Theme.Black">
<!-- Customize your theme here. -->
</style>
I am happy with that theme, except I want to change the background color of a button. Here is how it looks by default:
Here's what happens when I add a background color to this button (android:background="#color/play_bg"):
Hey!? It basically changed all the button's size, padding and margins!
So I managed to get the expected result using the backgroundTint property (android:backgroundTint="#color/play_bg"):
Unfortunately, this is only supported since version 21 of the API, which is not acceptable for me.
So two questions:
Why does changing the background messes with the rest of the button's properties?
How do I get the expected result without backgroundTint?
And a bonus question: How can I get the expected result programmatically (I have dynamic buttons in my app, so this would be very useful)?
You can change this color in your Java File. When your main class loads you can take an object of this button and then change color.
Here is how you define this button in Manifest file :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="#+id/btn1"
... />
Now in your Java file when you are adding this XML layout you need to
Button b = (Button)findViewByID(R.id.btn1);
b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);
You may also use COLOR:
COLOR.RED
The code below sometimes does not work for me :-
b.setBackgroundColor(int color)
In my case I will be doing in this process
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#color/play_as"
android:padding="8dp"
android:text="Button" />
Or you can use this link which is more easy way of creating the buttons
The problem I am having is specific to Android 5.0 (including 5.0.1/5.0.2) on both phones and my tablet (Nexus 9). Earlier versions of Android work fine.
In my app I want to set a global font that overrides all text. The way I've been accomplishing this prior to 5.0 was using this method. This method of font overriding doesn't seem work on any version of Lollipop that I've tried but works perfectly in 2.x and 4.x. I'm also running this code in a BaseApplication class I have so the font is only initialized in the onCreate() of my BaseApplication.
It seems like this was a bug in the Developer Preview and reported here. I tried the fix suggested in post #16 to use the TTX Tool to convert your font file to .ttx and back to .otf but that didn't seem to fix the issue like it did for others. I also verified that my .otf font file is valid and isn't corrupted according to the OTS sanitizer tool.
I also have a custom TextView that I can set the font via the layout. An example would be:
<com.myapp.widgets.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing text view font"
myapp:fontName="my-font.otf" />
This CustomTextView uses setTypeface(typeFace) in the view's initialization to set the font. This works on 5.0 but isn't really a possible solution I can use since I would need to go through every layout and change TextView, EditText, etc to use the CustomTextView and doesn't work with Dialog text wither.
So setting a single TextView's font with my CustomTextView class works fine in all version of Android, just not setting it globally.
I've also looked through the styles source code to see if I can find any differences between the Material and Holo theme but nothing seemed like it would change the android:typeface. My initial thoughts with this were that the Material theme somehow is overriding the android:typeface attribute over my app theme but I wasn't able to find anything.
Any thoughts or input would be greatly appreciated, thanks!
What I ended up doing to resolve this was use Calligraphy to set global fonts. https://github.com/chrisjenx/Calligraphy
Simply just add this code to the onCreate() of my custom Application class.
// Custom font file located in the "assets/fonts/"
String customFont = "Helvetica-Neue.otf";
CalligraphyConfig.initDefault(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/" + customFont)
.build()
);
Update as of SDK v26
If you are only supporting v26+ or using the support library, there now us a built in way to handle custom fonts throughout your app. Here is a link to the Android Developers page. The basics are as follows:
1) Add your custom font (.otf or .ttf) to your res/font directory
2) Create a new font family with the fonts files you added: res/font/your_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Normal -->
<font
android:font="#font/your_font_normal"
android:fontStyle="normal"
android:fontWeight="400"
app:font="#font/your_font_normal"
app:fontStyle="normal"
app:fontWeight="400"/>
<!-- Italic -->
<font
android:font="#font/your_font_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="#font/your_font_italic"
app:fontStyle="italic"
app:fontWeight="400"/>
<!-- Bold -->
<font
android:font="#font/your_font_bold"
android:fontStyle="normal"
android:fontWeight="700"
app:font="#font/your_font_bold"
app:fontStyle="normal"
app:fontWeight="700"/>
</font-family>
3) If you want to apply this font app-wide, create a style in your base theme style, usually located in: res/styles/style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Other app styling -->
<item name="android:fontFamily">#font/your_font_family</item>
</style>
4) If you only want to apply the font to a single or handful of views you can use the android:fontFamily attribute in XML or use:
val typeface = ResourcesCompat.getFont(context, R.font.your_font_family)
textView.typeface = typeface
This all seems to work on all versions of Android that I've tested.
i was also following this method which worked until Android 4.4 like a charm, but didn't work in Lolipop. In my case, the problem was solved (after some googling) by converting the ".ttf" font file to ".otf" I used this web for the conversion. Now it works in all versions. Hope it helps
I'm trying to set the imageview's layout_marginTop to one value for different density/screen sizes. In my values-mdpi folder I have the following line in dimensions.xml
<dimen name="marginTop">10dp</dimen>
In the MainActivity
ImageView image = (ImageView) findViewById(R.id.s_image);
But there is no setmargin method for imageview. Is there a way to do this?
You don't need to do that in code, you can do it in your XML file where the image view is defined. See this page for more details.
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
You're on the right track. It's probably easiest to refer to your dimension value within the xml (rather than set this up in java code).
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="#dimen/yourMarginTopValue" />
Try try to put android:adjustViewBounds="true" to
It's probably better to do it in XML since you seem to already have the ImageView defined in XML.
However, the layout_* XML attributes refer to the LayoutParams of the parent layout, not the view itself. To change them in code, access them with getLayoutParams(), do your modifications and call requestLayout() to schedule a re-layout pass. For example:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)imageView.getLayoutParams();
lp.topMargin = 123;
imageView.requestLayout();