Android change specific words on TextView from Java or XML - java

Since Android XML layouts are all rectangular, how can I have just some parts of the TextView with different font sizes and params?
In the example below, the time (which is in grey and has a smaller font size) is always at the end of the text. I don't know how to reproduce this in XML. If I separate the text in two different textviews and the width of both were wrap content it would just appear at the right of the TextView Layout (at the right of every line from the main message, not at the end).
Is it possible to do this layout configuration with XML or do I need to do it programmatically using JAVA? How can I change the params of certain parts of a textview with Java?
With two TextViews and Wrap Content (not what I want):

What I have understood from your question is you want a single textView with multiple style inside this. to do so you can do this:
SpannableStringBuilder text = new SpannableStringBuilder(); // a spannable string builder
text.append("your friend "); // appending first text
SpannableString fname = new SpannableString("Joana"); //making a span that would have different style
fname.setSpan(new StyleSpan(Typeface.BOLD), 0, fname.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // set different style
text.append(fname); // append value
text.append(" his sending you a message ");
SpannableString time_ago = new SpannableString("41 min ago"); //another span
time_ago.setSpan(new RelativeSizeSpan(.7f), 0, time_ago.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
time_ago.setSpan(new ForegroundColorSpan(Color.GRAY), 0, time_ago.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
text.append(time_ago); // append value
textView.setText(text);
XML:
<TextView
android:padding="5sp"
android:textColor="#color/black"
android:textSize="24sp"
android:gravity="start"
android:id="#+id/tvtest"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Related

Disappearing chars at the end of TextView

I am using SpannableStringBuilder to show image inside TextView. However, if the image will stay at the end of the line in TextView it just disappears. If I increase the size of the text it would be visible again. How can I fix it?
This is the code how I added the image inside TextView:
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.setSpan(new ImageSpan(createClusterBitmap(idOfAyat)), ssb.length() - 1, ssb.length(), 0);
itemViewHolder.kuranArabic.setText(ssb);
XML code of item:
<TextView
android:id="#+id/kuranArabic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/_4sdp"
android:fontFamily="#font/scheherazade"
android:gravity="center_horizontal"
android:textAlignment="center"
android:textColor="?attr/color2"
android:textSize="#dimen/_27sdp" />
I have attached two images. The first one is with an image that disappears when stays at end of the text view. The second one is visible when comes at the first position.
First Picture if StackOverflow's link doesn't work
Second Picture if StackOverflow's link doesn't work
P.S. I would appreciate any help. It is one of my first questions, don't judge me so strongly. :)
I have solved my problem by adding a flag called Spannable.SPAN_EXCLUSIVE_EXCLUSIVE to Spannable String Builder.
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.setSpan(new ImageSpan(image), ssb.length() - 3, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
itemViewHolder.kuranArabic.setText(ssb);

How to apply two different font to a TextView?

My case is same as Android - three sentences, two styles, one TextView. The only difference is that I want to set a Custom Font on the whole text. I have included roboto Font as an assets in my project and want to apply that font to the TextView with first part of the text will be roboto_LIGHT and middle part roboto_NORMAL and remaining part roboto_LIGHT again. Any suggestions how it can be done ?
Text needed in following format. Custom text with different styles and single textview.
You can use some html tags like <b> for bold with the following code:
textView.setText(Html.fromHtml("Login to the <b>Web Wallet</b> on PC or MAC"));
Then you can define your custom font family as in How to create custom font family with bold font
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="#font/roboto_LIGHT" />
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="#font/roboto_NORMAL" />
</font-family>
The weight 700 is the font for bold style. And then you should apply that created font family to the TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontFamily="#font/my_custom_font"/>
Or you can add 3 TextViews in a ConstraintLayout and give them some constraints to be in the positions you want otherwise.

Edit text view with image and text wrapped around image

I am building a rich text editor.
I have implemented text formatting like bold italic etc and also paragraph formatting like blockQuote. Now I would like to add images in editor and text should wrap around it.
I have implemented all these using SpannableString() and Spanned() and StyleSpan().
I can add image to a line using ImageSpan(), but that add it inline and its just there in place of a character
, what I want is to insert it in paragraph and rest of text should wrap around it. I am able to add image at the beginning of text by following code.. but I cannot align it center and right.
SpannableString string = new SpannableString("Text with icon and padding");
string.setSpan(new IconMarginSpan(bitmap, 30), 0, string.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
How to do it ? any example? or what procedure to follow?
you can fulfill your requirement like below:
you can set your image in imageview and your text in Html.fromHtml("here put your text")
gradle file:
compile 'com.github.deano2390:FlowTextView:2.0.5'
your XML layout:
<uk.co.deanwild.flowtextview.FlowTextView
android:id="#+id/ftv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="400dip"
android:padding="10dip"
android:src="#drawable/android2"/>
</uk.co.deanwild.flowtextview.FlowTextView>
your java file:
FlowTextView flowTextView = (FlowTextView) findViewById(R.id.ftv);
Spanned html = Html.fromHtml("here put your text");
flowTextView.setText(html);
you may get help by this link https://github.com/deano2390/FlowTextView also
I hope it help you..!
As per details given by you, It seems you might be using native EditText or you are trying with your custom EditText. Please provide this details to get exact solution of your query.
Apart from that, To build Rich Text Editor with the features mentioned by you (like Bold, Italic, Quote and Images etc), you can use this library RichEditor for Android: https://github.com/wasabeef/richeditor-android. It provides many features which may interest you.
Try with:
EditText text = (EditText)findViewById(R.id.text);
text.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.check_box), null);

How to jump line on android

I'm develop an android app but I don't know how to jump line or more while set the text of text view so how can I do this ?
On html I use \n but I don't know How to do it from java
Like this
Android
A
n
d
r
o
i
d
(Every space meanning new line)
Note that this text will appear on text view
Before using \n, you must set the android:maxLines attribute in XML. For example, in XML for the above text, you could say:
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:maxLines = "7"
android:text = "A\nn\nd\nr\no\ni\nd"
/>
Or if you wanted to do it from Java, you could do:
textView.setMaxLines(7);
textView.setText("A\nn\nd\nr\no\ni\nd");

Create a Custom ImageButton Class with FrameLayout and TextView

I am a newbie, so, any help is appreciated. I read a lot of posts here at StackOverflow and also I searched for my doubt in Google, but it's hard to find a good answer.
Here is what I am trying to do:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageButton
android:background="#layout/roundcorners"
android:id="#+id/hug"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/hug">
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="#000000"
android:textSize="15dip"
android:textStyle="bold"
android:paddingBottom="5dip"
android:clickable="true"
android:text="Hug">
</TextView>
</FrameLayout>
Above you guys can see the XML version of what I need.
The point is... I will have many of these FrameLayouts at run time. Every information to fill out the buttons will come from a database.
So, I need to create a Java Class where I can use a loop through all the registers from my database and instantiate a new FrameLayout Class (this class must have an ImageButton and a TextView as you can see from above XML) and just pass parameters, like this:
for (int i = 0; i < mArray.length; i++) {
button = new MyNewImageButton(name, src, text);
}
The above is just to simplify. What I mean is that I will pass parameters from my database when creating an Instance of this class that I am planning to create. Of course, every single button created will be added to the layout.
So... my question is: I know how to do this using XML, but I am REALLY having a hard time to create a class to do this.
Any thoughts? Any help is appreciated.
P.S.: Sorry if I made any mistake in my English, ok? I am a Brazilian. Someday my English will be flawless! Sorry if this question was already answered.
sorry to answer my own question to make another question. I tried to use the comments but there's a limitation in the number of characters, so, I am really sorry.
Hey guys and #blessenm. Well... I tried to use inflater and I came up with the following code:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// *******************************************
setContentView(R.layout.main);
//this is my main screen
//it's a linearlayout vertical orientation
ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal);
//these two new LinearLayouts will be one above the other,
//just like two lines
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
//inside of each linearlayout I set the orientation to horizontal
//so, everytime a picture is inflated from xml, it will fill in one
//linearlayout
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
//setting linearlayout parameters, so they fill the whole screen
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//the first two inflated xml imagebuttons I add to LinearView1
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
//the next two inflated xml imagebuttons I add to LinearView2
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
//after the above, we should have a grid 2X2
//after linearlayouts are filled, I add them to the main screen
parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1));
parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1));
However this is not working. In the errorlog I get the following message:
"Unhandled event loop exception".
Any ideas about what I am doing wrong?
Thanks!
If you are just trying to create a view from the xml and add it to the layout. Just use the LayoutInflater.
Inside the activity use something like
FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
R.id.YOUR_VIEW_XML,null);
layout.addView(frame);
If you are trying to create a class extend the frame layout or the the view. Create a constructor which takes your parameters and assign's the required values.
EDIT:
To Acess Elements Inside
If you have set id's to those element, you can access them by
TextView text = (TextView)frame.findViewById(R.id.yourtextview);
Or you can use the child index like
TextView text = (TextView)frame.getChildAt(0);
It sounds like you are looking for a way to create a view class that will be an ImageButton and a TextView wrapped with a FrameLayout.
In this case, you could look into creating your own View class. Probably a View class that extends FrameLayout. See this dev article for more information about how to create a custom view. http://developer.android.com/guide/topics/ui/custom-components.html
Specifically the "Compound Controls" section: http://developer.android.com/guide/topics/ui/custom-components.html#compound

Categories

Resources