So I have two TextViews in a RelativeLayout in my xml file. When they are localized to different languages, there will be a chance that the two views will overlap with each other.
In this case I want to put them into two lines instead of just one line. How to detect the overlap and reset their position so on is on top of the other in java code?
I did this with a LinearLayout and by using the textview's auto wrap feature Iwas able to detect if the textview wrapped. By using a ViewTreeObserver and looking at the number of lines for the textview I then programmatically set the view to Gone.
final TextView text = findViewById(R.id.textview1);
text.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int lineCount = text.getLineCount();
Log.d(this, "LineCount = " + lineCount);
if (lineCount > 1) {
text.setVisibility(View.GONE);
}
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
Just have to make sure your layouts don't ellipsize or that you set the singleLine or Maxlines to true or 1. I set Max line on the one textview I want gone to 2 so that it will wrap if it won't fit.
Just put your layout in a language spefic layout folder.
Your standard layout in res/layout
another in res/layout-ar (example for arabic)
Related
I have two TextViews next to each other inside ConstraintLayout. Each of them has two words.
The words inside of them can be changed by the current locale.
Currently each of them takes equally space on the width by using android:layout_weight="1" and android:layout_width="0dp"
If the text in any of them is too long to fit one line, I want both of them to expand to two lines. I need the text itself to be expanded, not the TextView - that can be done only if the text has at least two words.
Is there a way to do that?
You will need for layout to proceed then determine if the left TextView has two lines and, if it does, adjust the right TextView to also occupy two lines. The following code is one way to do this. You can place this in onCreate().
val layout = findViewById<ConstraintLayout>(R.id.layout)
layout.doOnNextLayout {
val textView1 = findViewById<TextView>(R.id.textView1) // Left view
val textView2 = findViewById<TextView>(R.id.textView2) // Right view
if (textView1.lineCount == 2) { // If left view has 2 lines, add newline to the right view.
textView2.text = textView2.text.toString().replace(" ", "\n")
}
}
I have to embed a button "inline" within a multiline textview, like so:
I'm not sure how to do this in Android, as there are no layout elements or attributes (that I'm aware of) that allow aligning a Button where text ends in a multiline TextView. And I really don't want to use a WebView for this and do it with html / css, as this screen is already heavy enough. Any ideas how to accomplish it?
ClickableSpan is the answer to your problem!
Android strings accept unicode and html characters, which includes arrows
You can create a textview(tvDescription) with the maxLines=5 and ellipsize=end
I'm assuming the maximum lines you wan to show is 5
android:ellipsize="end"
android:maxLines="5"
Add a view(btnIndicator currently button. You can change as per your requirements) which always stays on the bottom right of the textView. This view contains the text (View More and View Less). Suggest you to use constraint layout which will make your task easy.
Assuming you have got the reference to Textview as tvDescription.
And the view at the bottom right as btnIndicator. Add onClickListener to this view.
btnIndicator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int maxLines = tvDescription.getLineCount();
if (maxLines == 5) {
maxLines = Integer.MAX_VALUE;
btnIndicator.setText("View Less")
}
else {
maxLines = 5;
btnIndicator.setText("View More")
}
/*
* Here you can append the text VIEW MORE(when max lines is 5)
* or ignore if it is already expanded
* */
tvDescription.setMaxLines(maxLines);
}
});
I want to create textview programmatically and display horizontally if it fits on device screen else display in next row.
I have the screen's available width and some text. Based on that text I want to create textview and display it. I also have additional text which i want to display in first row if it fits there. Otherwise, it should go on the next row. The only problem is that I am not able to get the width of textview based on its text. I can't use wrap_content as it will not give me its width value.
In the activity that includes your textview, implement this interface;
global::Android.Views.ViewTreeObserver.IOnGlobalLayoutListener
This will make you implement OnGlobalLayout method.
You will also need to add;
rootLayout = FindViewById<RelativeLayout>(Resource.Id.rootLayout);
rootLayout.ViewTreeObserver.AddOnGlobalLayoutListener(this);
Root layout here can be your root layout that includes your text views.
On the method you implemented,
public void OnGlobalLayout()
{
if (textView != null)
{
int height = textView .Height;
}
}
if you call textView.Height or textView.Width, it will return the true width and height of your textview after the view is created, instead of the wrap content -1 value.
I've been making all of my views dynamically, and now I've come to the point where I want to add an EditText for people to write in.
I've been able to accomplish this for the most part, but it doesn't look right. I have a linear layout that I'm adding a relative layout to. I'm making the relative layout have a white background, then adding the EditText. Problem is, it always adds it to the direct center of the relative layout, and options to align it vertically to the top have so far failed.
I also need to be able to pull the text from it later when a separate button was pressed (I know how to make the button work, it's the pulling text from it part I'm a bit iffy on). Here's my code so far:
public void addEditText(LinearLayout L){
EditText myEditText = new EditText(c);
myEditText.setSingleLine(false);
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(scWidth1, 300);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER_VERTICAL);
l1.addView(myEditText);
L.addView(l1);
}
l1.setGravity(Gravity.CENTER_VERTICAL); places the EditText in center vertical of the parent container i.e RelativeLayout, remove that line.
Friends,
I have 3 text views in a LinearLayout which has a bounded width of 300dp. Each text view has a layout_weight of 1 so the screen will be divided evenly among the three TextViews.
[text view 1][text view 2][text view 3]
Most of the time the text fits in one line in each of the TextView but there are some occasion when it does not. Is there any way I could determine that the Text will need two lines and set the number of lines parameter of the TextView to 2?
If one of the TextViews needs 2 lines than all three TextViews should be set to two lines
The short answer is yes- TextView has a getLineCount() method that will tell you how many lines of text are the TextView.
Unfortunately there's a caveat- the TextView must be layed out and measured before this method works properly.
If you are setting the text dynamically (e.g. via setText()), then you will need to create a callback for when the layout pass happens like so:
mTextView1.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (mTextView1.getLineCount() > 1) { // or textView2, etc.
// Adjust all text view heights
}
}
});