Ricardo Melo
Ricardo Melo

Reputation: 440

Android resizes TextView

Well, I'm trying to do a Search System for teachers in my app, and I already did it, but with a layout problem. When I set texts for my TextViews, they resize based in the word lenght, and I don't want this.

This is my code for search:

            String prof = s.toString();
            int i=0;                
            pesquisa.clear();
            for(i = 0; i<professores.size();i++){
                if(professores.get(i).contains(prof)){
                    pesquisa.add(professores.get(i));
                }
            }
            for(i=0;i<cont;i++){
                txt.get(i).setText(""); 
            }
            if(pesquisa.size() > cont){
                for(i =0; i<cont;i++){              
                    txt.get(i).setText(pesquisa.get(i));            
                }   
            }else{
                for(i =0; i<pesquisa.size();i++){               
                    txt.get(i).setText(pesquisa.get(i));            
                }
            }

And a TextView layout:

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="10dip"
        android:layout_weight="1"
        android:background="@drawable/bgtxtview"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

And this is bgtxtview:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>     
        <shape>
            <solid
                android:color="#b0b0b0" />
            <stroke
                android:width="1dp"
                android:color="#505050" />
            <corners
                android:radius="5dp" />
            <padding
                android:left="2dp"
                android:right="2dp"/>

        </shape>
       </item>
   </selector>

Sooo, what can I do to fix that?

An image with the layout.

!Image Layout[https://i.sstatic.net/qU2Uu.jpg]

Upvotes: 1

Views: 1965

Answers (1)

Marcin S.
Marcin S.

Reputation: 11191

You should set width to 0dp instead of match_parent in your TextView.

'The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout.'

Upvotes: 1

Related Questions