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));
Related
I have a standard TextView and TextSwitcher:
The TextView:
<android.support.v7.widget.AppCompatTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:textColor="#3F51B5"
android:textSize="40sp"
android:gravity="bottom|center_horizontal"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:background="#android:color/black">
The TextSwitcher:
<TextSwitcher
android:id="#+id/questionTextSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"/>
The text of the TextView changes dynamically, and sometimes the text is a couple lines.
The black background is the TextView, and the white is the TextSwitcher:
When there is a longer text set it looks like this:
When there is a smaller text it looks like this:
I want the TextView to be placed at the bottom of the TextSwitcher - but that isn't happening?
try to match the parent height and also use the layout_gravity attribute.
Im trying to let my textview run from left to right in an infinite way.But somehow it looks really weird I even cant explain it in words so I decided to record and upload it.
Here is the video : https://www.youtube.com/watch?v=gj3FF7fEutk&feature=youtu.be
So thats how I wrote my Textview :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:layout_marginTop="20dp"
android:textColor="#color/white"
android:id="#+id/textView_title_full"
android:layout_centerHorizontal="true" />
And this TextView is in a RelativeLayout declared like this :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="170dp"
android:id="#+id/BELOW_FULL"
android:layout_gravity="bottom"
android:background="#drawable/color_below_player_full"
android:layout_below="#+id/viewpager_albumart_full"
android:layout_alignParentBottom="true">
where the RelativeLayout is also in a Relativelayout like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#0E0E0E">
I of course used the txt_song_title.setSelected(true); method.
How it looks like what I want to achieve :https://www.youtube.com/watch?v=o4uAA4pik68
Thank you all very much !
EDIT:
All parts where I do sth with my TextView :
public void init_textviews()
{
...
txt_song_title = (TextView)findViewById(R.id.textView_title_full);
txt_song_title.setSelected(true);
}
//init_textviews() is called in my onCreate method
txt_song_title.setText("Some Long text"); // called in onCreate after init_textviews()
And lastly:
public static void update_GUI_full (Context context) //Called when it is a button is clicked
{
...
txt_song_title.setText("longTexthere");
...
}
Working side by side with #Ahmet Kazaman we've found that there seemed to be a problem when combining a marquee TextView with another TextView periodically updated using a Handler.
After some tests we've found that this strange behaviour only happens when the TextViews are iside a RelativeLayout and everything woeks as expected when the enclosing layout is a LinearLayout. Thus, the solution (more a workaround) is to change the enclosing layout to be a Linearlayout.
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. :)
So I have this TextView in android/java that I would like to position randomly along the horizontal axis where it is located. This is the code I have so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="100dp" //This is the width I want to randomize
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/n5"/>
My goal is that, by randomizing the 100dp in the android:layout_width= line, I can move my TextView over by a certain random amount. Would anyone know how to do this?
Thanks!
To answer your question directly:
1. give the View an id
<View android:id="#+id/picture_stream"
android:layout_width="100dp"
android:layout_height="wrap_content" />
2. change width programmatically in onCreate:
...
View view_instance = (View)findViewById(R.id.nutrition_bar_filled);
LayoutParams params=view_instance.getLayoutParams();
params.width=newOne; //use Math.random() to create the value.
view_instance.setLayoutParams(params);
See also "view layout width - how to change programmatically".
You could also use margin/padding instead of "pushing" it with another view.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/movingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/n5"/>
</LinearLayout>
Set the TextView as the sole item inside a horizontal Linear Layout and then programatically set a random left padding for the textview.
TextView movingTextView = (TextView) this.findViewById(R.id.movingTextView);
movingTextView.setPaddingRelative((int) 1+Math.random()*100,0,0,0);
iam newbie in android i am inserting text dynamically in CheckBox so i dnt have idea about length of text and i also have to insert a button in horizontal of Checkbox please let me know how can i insert text in multiple lines.
this is the checkbox array.
<LinearLayout
android:id="#+id/risktablerowcheckbox_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="#drawable/location_header_deactivedd"
android:visibility="gone">
<CheckBox
android:id="#+id/risktablerow_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#android:color/transparent"
android:drawablePadding="2dp"
android:layout_marginTop="8dp"
android:textSize="14sp"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/risktablerowbutton_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/riskinfo_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/info"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:layout_alignBottom="#+id/risktablerowspinner_layout"/>
</RelativeLayout>
String[] checkBoxArray = {"","","Reason For Risk Assesment Not Completed","","","Is the client prepared to provide details of theft-attractive stock or property?","","","If Other Scpecify","",""};
CheckBox riskCheckBox ;
riskCheckBox = (CheckBox)riskTableRow.findViewById(R.id.risktablerow_checkbox);
riskCheckBox.setText(checkBoxArray[j]);
You are setting the layout width of the parent linear layout and the checkbox to wrap_content. Thus the text in the checkbox has no width constraints.
If you set the linearlayout to match parent, it will be as wide as the screen, you can then set the checkbox to have a width of match_parent and a height of wrap_content and the text should automatically go to the next line.
You may also want to include some right margin on the checkbox to ensure there is some space on the right before the text is bumped to the next line.