Adeel Faisal
Adeel Faisal

Reputation: 33

Break the line if there is no space

If i am adding multiple text views in android it gets hide i need to break when screen finished As an example my code is something like this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/layout5"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="last checking text"/>

</LinearLayout>

and the result seems to be

this is the screenshot

I need the text to break down when screen finished down and break to another line. this is only the example code i am doing this thing programmatically, so please dont answer of adding more linear layout.

Upvotes: 1

Views: 1774

Answers (4)

Rifat Ullah
Rifat Ullah

Reputation: 74

If you want your textView appearing with multiple line with a scrollable properties, then no need to use multiple textView. Check the below solution:

<?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"
    <!--`enter code here`-->
    android:background="#000000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#40000000"
        android:maxLines="100"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:scrollbarThumbVertical="@android:color/transparent"
        android:scrollbars="vertical"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold" />

</LinearLayout>

And bellow code on your Activity:

TextView txtView = (TextView) findViewById(R.id.TextView_id);
txtView.setText("your text here");
txtView.setMovementMethod(new ScrollingMovementMethod());
    

Upvotes: 0

Jyoti JK
Jyoti JK

Reputation: 2171

If you want to use number of textviews at run time, You can try using GridView as

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

And Gridview item as TextView

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/text"
  />

You can perform action in onItemClickListener() //based on the position or textview text, choose action using switch case or something else.

   GridView gridView = (GridView) findViewById(R.id.grid_view);
   gridView.setAdapter(yourCustomAdapterObject);
   gridView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       //here view is your textview
       switch (position) {
           case 0: //action
               break;
           case 1://action
               break;
           ....
           }
       }
  }); 

Upvotes: 0

Vishesh Chandra
Vishesh Chandra

Reputation: 7071

For flexible layout you can go with FlexboxLayout and details you can get from Android Developers Blog and for Open source FlexboxLayout dependency you can reach to Github.

Upvotes: 3

Praveen Rawat
Praveen Rawat

Reputation: 324

If You want to create TextView programmatically and make Listener for each . Here is the Code . First remove all textview in layout.

TextView tv[] = new TextView[subCategory.length];
    for (int i = 0; i < subCategory.length; i++) {
            tv[i] = new TextView(this);
            tv[i].setText(subCategory[i]);
            tv[i].setId(i);
            layout5.addView(tv[i]); 
            tv[i].setOnClickListener(onclicklistener);
        }

And For Listner

OnClickListener onclicklistener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == tv[0]){
            //do whatever you want....
        }
    }
};

Upvotes: 0

Related Questions