Displaying text using textView() Android - java

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

Related

TextView get "Real number of lines"

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

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 ..

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. :)

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

Hardcoded Text Issues

I'm working on an application in eclipse emulator "Nexus one" and i'm trying to add buttons onto the format. When I add the buttons they are fine. When I add my own text into the button I get a hardcode warning. So I would carry on and add the "#string/" which gets rid of the errors/warnings, but the problem is that the "#string/" shows up as text on the button.
-So how do I make the buttons say just Profile and Calendar without errors and or the #string/
I have no idea on how to fix this. here is code and pictures:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/Profile"
android:onClick="profile" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:layout_marginTop="20dp"
android:text="#string/Calendar"
android:onClick="calendar"/>
Simple remove #string/ from your android:text attribute. Thus each button would look like this:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Profile"
android:onClick="profile" />
#string/my_string_name is used to reference Strings defined in a strings.xml resource file.
The other (and the "proper") way to fix these errors is to define these strings in XML.
If you edit strings.xml and add lines such as <string name="Profile">Profile</string>, these errors will go away, and you will be on the way to having an app that is capable of being easily translated to other languages.

Categories