Disappearing chars at the end of TextView - java

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

Related

Android change specific words on TextView from Java or XML

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" />

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

Android RatingBar shows extra remainder

I'm using the standard Android RatingBar. It seems to behave quite weird. For some reason I can't select only full stars. It always shows a little bit more or less. Also the .setNumStars property doesn't seem to work. I'm using the following code:
RatingBar ratingbar = new RatingBar(this);
ratingbar.setLayoutParams(lp);
ratingbar.setVisibility(View.VISIBLE);
ratingbar.setNumStars(5);
ratingbar.setStepSize(1);
ratingbar.setRating(1);
linearLayoutReviews.addView(ratingbar);
When I select 1 star it selects one star:
When I select 2 stars:
When I select 3 stars:
When I select 4 stars:
And when I select 5 stars:
So for some reason it shows more than 5 stars, and there will always be a remainder of the next star selected
EDIT
When I set .setNumStars to 6 I can select them one by one, but than ofcourse, I have six
EDIT
This is the xml of the LinearLayout
<LinearLayout
android:id="#+id/linearLayoutReviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5"></LinearLayout>
You have added RatingBar multiple times LinearLayout that's why its showing previous RatingBar in your LinearLayout .
So remove all current RatingBar from LinearLayout first and then add new RatingBar.
I have added one line in your code see below: it may help you:
RatingBar ratingbar = new RatingBar(this);
ratingbar.setLayoutParams(lp);
ratingbar.setVisibility(View.VISIBLE);
ratingbar.setNumStars(5);
ratingbar.setStepSize(1);
ratingbar.setRating(1);
//In your code add the lines given below
linearLayoutReviews.removeAllViews();
linearLayoutReviews.addView(ratingbar);
I have fixed it by making a new linearlayout and placing it below the old linearlayout. I added the ratingbar to the new linearlayout

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

Android TextView Wrap

Here is my problem. How to make text don't wrap?
I already tried to make text smaller. Didn't work. Tried to do singleline="true". Here is what he do(2 screen shot 6-7 textview)
1 Screenshot) Here is I made it in the Eclipse
2 Screenshot) Here is how it showen in the emulator
Try setting android:ellipsize to none for each TextView. For more details see documentation on this attribute.
From xml:
<TextView ... android:ellipsize="none" />
From code:
TextView textView = new TextView(context);
...
textView.setEllipsize(null);
try giving android:maxLines=1... singleLine property should also work...

Categories

Resources