TextView get "Real number of lines" - java

I have the following TextView
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:text="Large text"
android:ellipsize="end" />
When I calling textView.getLineCount() I always get 3. But I want to get "Real number of lines" which hidden because I'm using maxLines. How can I do that?

In xml code, add this to TextView:
android:inputType = "textMultiLine"

Use TextView.getLayout() to access information about the actual text currently being laid out.
TextView tv = findViewById(R.id.textView);
int realLines = tv.getLayout().getLineCount();

Related

Android EditText android:text not working

EditText
android:textSize="18.0sp"
android:textColor="#color/red"
android:id="#id/et_server_url"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_marginLeft="50.0dip"
android:layout_marginTop="10.0dip"
android:layout_marginRight="50.0dip"
android:text="#string/serverurl"
android:layout_weight="0.8"
android:layout_toRightOf="#id/iv_logo"
android:layout_below="#id/et_email"
android:layout_centerHorizontal="true"
android:enabled="false" />
In this code, Text is not displaying .
Note: I have to change this only in the layout. I didn't have any java files.so
try
android:background="#null"
or
android:background="#android:color/transparent"
in programmatically
setBackgroundDrawable(null);
or
setBackgroundColor(getResources().getColor(android.R.color.transparent))
I think you are using this edit text inside a RelativeLayout. So, remove this property and add height.
Remove this:-
android:layout_weight="0.8"
And Add this:-
android:layout_height="wrap_content"
You will be good to go.
And use,
EditText et_server_url = findViewById(R.id.et_server_url);
et_server_url.setEnabled(false); //To make it disabled
and
et_server_url.setEnabled(true); //to make it enabled
try to use this code
<EditText
android:id="#id/et_server_url"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/et_email"
android:layout_centerHorizontal="true"
android:layout_marginLeft="50.0dip"
android:layout_marginRight="50.0dip"
android:layout_marginTop="10.0dip"
android:layout_toRightOf="#id/iv_logo"
android:background="#drawable/background_editext"
android:fontFamily="sans-serif"
android:inputType="none"
android:paddingLeft="20.0dip"
android:paddingRight="20.0dip"
android:text="#string/serverurl"
android:textColor="#color/red"
android:textSize="18.0sp" />
you're hiding yout EditText that is why you can't see or edit it
bro your code working you can check android:text="#string/text" to android:text="text"
use hardcore string and check coz i am copy your code its working fine ...
and generally edittext use android:hint not android:text
and you can also set editable="false" in android:hint using...
check its may help you...otherwise tell me ..

Line drop of a second TextView

I have two textview in a same line, the two textview do not have static content, the problem comes when the second textview is loaded with data, this takes the second line but not from the left completely.
The code is this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="uno"
android:visibility="visible"
android:textSize="#dimen/msg_text_primary"
android:textColor="#color/from"
android:maxLines="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/text_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="titulo"
android:ellipsize="end"
android:textSize="#dimen/msg_text_primary"
android:textColor="#color/from"
android:maxLines="2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/add"
android:layout_toEndOf="#+id/add"
android:layout_marginLeft="2dp"
android:layout_alignParentLeft="false"
android:layout_toLeftOf="#+id/add"
/>
</RelativeLayout>
The picture:
if you really must keep your RelativeLayout I would suggest replacing it as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="uno"
android:visibility="visible"
android:textSize="#dimen/msg_text_primary"
android:textColor="#color/from"
android:maxLines="1"
/>
<TextView
android:id="#+id/text_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="titulo"
android:ellipsize="end"
android:textSize="#dimen/msg_text_primary"
android:textColor="#color/from"
android:maxLines="2"
android:layout_below="#id/add"
/>
</RelativeLayout>
Explanation: I removed the aligns on the first TextView as alignParentStart is the same as alignParentLeft and by default in a RelativeLayout it should align to the top (also why removed alignParentTop) and left unless you are using RTL; Removed the alignParentTopbecause as you are using a RelativeLayoutit will overlap the first TextView, also removed the layout_toRightOf(layout_toEndOf) on the second as that means it will start drawing text from where the first one ends, removed the margin because you want it be all the way left and removed layout_alignParentLeft and layout_toLeftOf as previously mentioned.
Now if you do not need a RelativeLayout I would suggest using a LinearLayout with vertical orientation instead.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- insert your code here-->
</LinearLayout>
It sounds like you want to have your second TextView's text start to the left of the first, but then wrap below it if there's a second line.
This is not possible out-of-the-box using TextView, regardless of whether your parent view is a RelativeLayout, a LinearLayout, or anything else.
Probably the best solution is to simply use a single TextView and to combine the text in your Java code before you set it to the TextViews. In other words, instead of writing something like:
String text1 = "first part";
String text2 = "of the text I want to put in my layout";
textView1.setText(text1);
textView2.setText(text2);
Write this:
String text1 = "first part";
String text2 = "of the text I want to put in my layout";
textView.setText(text1 + " " + text2);
Even better, consider using String Resources instead of concatenating your text manually:
String text1 = "first part";
String text2 = "of the text I want to put in my layout";
textView.setText(getString(R.string.mystring, text1, text2));

how to define correctly textview width

Hi everyone i got a little problem about the width of my textview
it looks like this
as you can see, my textview is bigger than my text :/
here is my code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="destinataire"
android:id="#+id/tv_destinataire"
android:layout_gravity="left|top"
android:layout_weight="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="envoyeur"
android:id="#+id/tv_envoyeur"
android:layout_gravity="right|top"
android:layout_weight="1"
android:layout_marginRight="25dp"
android:layout_marginTop="10dp"
android:padding="10dp"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
</LinearLayout>
i would like something like this, if the message contain some word well "wrap_content" whereas is long long message something like maximum 70% of width :
thank you :)
You can reach what you said adding some logic directly on your activity and not in the XML file. You can set the width by Java code with something like:
TextView t = (TextView)findViewById(R.id.myTextView);
where myTextView is the id that you declared in your XML.
Now go ahead and write some logic...
If is necessary:
t.setWidth(200);
Note that 200 is only an example, you can calculate the width you need before.
If I were you, I might use the relative layout and add maxWidth limit to TextView containing text, and as the dialog going, just place the TextView below the last TextView and use alignStart/alightParentStart and alignEnd/alignParentEnd to indicate who's speaking.
when a new message arrives
prepare your relative layout parameter, add layout rules
set text, maxwidth limit, layout parameter and other style you want for your text
add the view to a scrollable relative layout container
Sorry for my bad English in case of you have any reading problem. :)

Displaying text using textView() Android

Unsure of how to display text in the xml file, i have code written to output text in the java file but nothing happens when the application is run
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText("Welcome to the Dungeon");
*
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_text_view"
android:text="#string/my_text_view"/>
Use this:
<TextView android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:layout_gravity="center"
android:textColor="#656565"
android:textSize="20dp" />
And then setText
why are you setting text twice in xml and java, just add once as:
but whatever try to provide : fontsize, and gravity(center) of layout, either programmatically or in xml

Custom button programmatically android

I'm creating a list of buttons in Android with the same icon for each of them and then set the text programmatically. So my list has:
ImageView (always the same) + Text (label for the icon which I set programmatically).
How can I create a something like the icon below but where I can change text dinamically?
Thank you!
You can use android:drawableLeft attribute in the TextView. Here's the sample:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:gravity="center_vertical"
android:layout_margin="16dp"
android:drawableLeft="#drawable/ic_launcher"
/>
And the result
I would suggest you using the following
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:drawableLeft="#drawable/button_icon"
... />
If you are looking for more customized button, then probably you should learn more about Custom Views:
https://developer.android.com/training/custom-views/index.html
What I understand from your question is that you want to change/set the text of the button programmatically.
for this use the following code in onCreate event:
Button mybutton = (Button) findViewById(R.id.your_button_name);
mybutton.setText("Your Text Here");

Categories