Vivek Shankar Vivek Shankar - 6 months ago 10
Java Question

How to limit a text to a single line from java source file?

I can find how to limit a sentence to a single line that is defined in my java code. My text is passed through an intent. Here's what I'm experimenting with-

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);

Intent intent=getIntent();
String message=intent.getStringExtra(MyActivity.EXTRA_VALUE);
TextView text=new TextView(this);
text.setTextSize(40);
text.setText(message);

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF,R.id.imageView);
p.addRule(RelativeLayout.ALIGN_TOP);


RelativeLayout layout=(RelativeLayout) findViewById(R.id.content);
text.setLayoutParams(p);
layout.addView(text);

}


My corresponding xml file is-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="shanky.thefirst.DisplayMessageActivity"
tools:showIn="@layout/activity_display_message">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/imageView"
android:src="@drawable/ic_launcher"/>

</RelativeLayout>


My output is-

Welcome

Vivek!

instead of-
Welcome Vivek!

Answer

This is what you want. Just do:

text.setSingleLine();

it will set your textview to single line

Comments