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.
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")
}
}
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)
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
}
}
});
I am trying to make the rows of my listview such that clicking on any one expands that row in place and shows extra options. I use an extra layout (let's call it expandView) contained with the row layout (call it rowView) with height set to zero initially and use a valueanimator to expand it to the final height when rowView is clicked.
Problem is, I have no idea what the final height is going to take until the user actually clicks the rowView. So I cannot specify some fixed end value for the animation, nor can I sum up the heights of expandView's children on rowView click since they too return zero when expandView's height is zero.
I really want to avoid hardcoding the height values for expandView or it's children here. Any pointers?
Figured it out. This answer given here helped. I have to call call measure() followed by getMeasuredHeight() to get the final height of my expandView.
i'm having a hard time squeezing my alert dialog to just wrap my content.
the dialog's layout xml is structured like this:
+ linear layout (main)
++ text view
++ linear layout (placeholder)
i use the placeholder to attach a singe edittext-derived field to it later, during oncreate, by calling addView() on it, so my onCreate basically looks like this:
View v = LayoutInflater.from(context).inflate(input_layout, null);
LinearLayout placeholder = (LinearLayout)view.findViewById(input_placeholder)
SoftEditText text = new SoftEditText(context)
// set text attributes - input type, ime action, watchers and listeners
placeholder.addView(text);
setCancelable(true);
setButton(BUTTON_NEGATIVE, ....);
setTitle(title_label);
// view.forceLayout() - didn't help much
setView(view);
super.onCreate(instance);
// getWindow().getAttributes().height=LayoutParams.WRAP_CONTENT no go either
and the result is: my alert dialog is twice the height it's supposed to be. my linearlayout's are set to WRAP_CONTENT height, and if i paint their background, i see they are of correct size (that is nearly half the dialog's height). the rest of the dialog is black, so there is no component that forces or expects this size.
I set the text's maxLines to 1, but that again hasn't helped at all. this seems to be a trivial layout problem but i can't figure out what more should i call to get the dialog squeeze to simply wrap the content.
thanks bunches for any tip.
set maxLength and you can use ellipsize to indicate the text in continuation..