Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, May 2, 2016

Android: Last line of textview cut off

Android: Last line of textview cut off


I have a horizontal LinearLayout containing a TextView followed by a Spinner next to it. This LinearLayout is dynamically inflated multiple times in a fixed vertical LinearLayout contained within a RelativeLayout.

The problem is that since I switched from Theme.light to Theme.holo.light, the last line of the TextView gets cut in half. This happens when the dynamic text is long and spans more than one row.

enter image description here

I have been able to fix this by adding bottom padding to the horizontal LinearLayout containing the TextView and Spinner.

This does not feel like a fix, but more of a hack. Can someone please give me some advice on how to properly fix this?

I have also read some other questions, but none seem to help.

Horizontal Linear layout:

                        

Relative layout where above layout is dynamically inflated at Linear Layout with id ll2_7:

                                                                                                                

EDIT: Here is the complete layout xml for above:

                              

Answer by Dheeresh Singh for Android: Last line of textview cut off


try with removing android:paddingBottom="20dp"

from

   

Answer by Rynardt for Android: Last line of textview cut off


After trying a million different things, I think I have the answer.

I applied a LayoutGravity to the TextView item:

android:layout_gravity="fill"  

Seems to solve all clipping issues I had. Hope this helps someone with the same problem.

Answer by Adel Ammari for Android: Last line of textview cut off


I have this same problem, and its very annoying.

It only happens with Arabic text.

If you make the label multi-line and adding a \n at the end of your string, it would fix it, but the problem is that there would be a big gap between this label and the object below it, due to the fact that this field now has a new empty line below it.

A custom control can be done to get around that. But overall, this is an annoying bug.

Answer by JesperB for Android: Last line of textview cut off


If you have this problem and your TextView is inside a RelativeLayout, try switching the RelativeLayout for a LinearLayout.

That fixed the problem for me

Answer by Graeme for Android: Last line of textview cut off


When this occurs, you should ensure that the TextView is not growing larger than it's container -

If a TextView is set to wrap_content and it's container (or an ancestor container) doesn't leave room for the TextView to grow into it can be occluded.

If that's not the case, it's also possible the onMeasure() of the TextView sometimes doesn't correctly measure the tails of letters, non-latin characters or the effects from text being italic. You can correct for this by setting a global style for your TextView so it will be picked up without needed to change your entire code base:

Ensure that you're application/activities use a custom theme like so:

      

The answer by @Rynadt was really helpful in getting to the above stage. Setting the gravity of the Text inside the View ensures on some devices that occlusion never takes place (The text is correctly fitted inside the view), on others a helping hand with padding of an sp value, ensures that the tails et al are accounted for with a TextSize specific value.

Answer by Jusid for Android: Last line of textview cut off


I've encountered the same cut-off issue as shown at the screenshot. It is caused by the baseline alignment in the horizontal LinearLayout. TextView and Spinner have different baselines due to font size difference. To fix the issue it is needed to disable baseline alignment for the layout by setting:

android:baselineAligned="false"  

or in the code:

layout.setBaselineAligned(false);  

Answer by Anuj Sharma for Android: Last line of textview cut off


Best workaround for this is to add a dummy View of desired height (i.e. this will add padding itself) at the bottom of your view.

           

Like in my case I added one more table row at the bottom of the view. Hope this could help someone.

Answer by Ameer for Android: Last line of textview cut off


I added some dummy space after text by adding

textView.setText(firstString+"\n");  

I tried all other solution.But this was the only solution worked for me

Answer by Seppl for Android: Last line of textview cut off


I found a different solution by extending TextView and adding a custom Class like this:

 public class AdaptingTextView extends TextView {        public AdaptingTextView(Context context, AttributeSet attrs, int defStyle) {          super(context, attrs, defStyle);      }        public AdaptingTextView(Context context, AttributeSet attrs) {          super(context, attrs);      }        public AdaptingTextView(Context context) {          super(context);      }        @Override      protected void onSizeChanged(int w, int h, int oldw, int oldh) {          super.onSizeChanged(w, h, oldw, oldh);            // set fitting lines to prevent cut text          int fittingLines = h / this.getLineHeight();          if (fittingLines > 0) {              this.setLines(fittingLines);          }      }    }  

Answer by Thorinside for Android: Last line of textview cut off


I had the same problem, and found that simply adding

android:includeFontPadding="false"  

the final line of text no longer had its descenders clipped.

Answer by kazon for Android: Last line of textview cut off


I finally fixed it!

I try to add String to the TextView in Service and then call scrollTo(), the last line be cut off!

The scrollTo() should be call in "Runnable", like:

private ScrollView mScrollView;  public void scrollToBottom()  {      mScrollView = (ScrollView) findViewById(R.id.debug_textview_scrollview);      mScrollView.post(new Runnable()      {          public void run()          {              mScrollView.fullScroll(View.FOCUS_DOWN);          }      });  }  

I think it because in the monent of call scrollTo() in service, the update of TextView is not ready.

Answer by Spookysister for Android: Last line of textview cut off


My solution was close to the accepted one, but I had to change it to

   android:layout_gravity="fill_vertical"  

instead. Otherwise the other rows would have been stretch as well with added line breaks at random places. For example, the biggest row had 4 lines, so another row was changed from

this is a testphrase

to

thi  s is  a testph  rase  

Answer by Jim Baca for Android: Last line of textview cut off


You can use a global layout listener for a TextView in any type of ViewGroup.

    final TextView dSTextView = (TextView)findViewById(R.id.annoyingTextView);  dSTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {        @Override      public void onGlobalLayout() {          dSTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);            float lineHeight = dSTextView.getLineHeight();          int maxLines = (int) (dSTextView.getHeight() / lineHeight);            if (dSTextView.getLineCount() != maxLines) {              dSTextView.setLines(maxLines);          }        }  });  

You can read more about it here

Answer by Mahesh Iyer for Android: Last line of textview cut off


I think there is very little you can do to get this working by altering the layouts. As I have found that some methods work only in some cases. I think it depends on the entire layout hierarchy and is not a one-size-fits-all solution. I have also noticed that it happens especially when you have a different font that you want to set to the TextView.

A sure shot method that I have experimented and tested is that you can set the font attributes in code after the view is inflated. I am assuming that you have a font in the assets/fonts folder that you want to you.

For eg in a Fragment:

@Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,          Bundle savedInstanceState)  {      View view = inflater.inflate(R.layout.fragment_view, container, false);      TextView tv = (TextView)view.findViewById(R.id.text_view);      tv.setText("Insert text that needs to be displayed");      AssetManager assetManager = getContext().getAssets();      Typeface typeFace = Typeface.createFromAsset(assetManager, "Fonts/OpenSans-Light.ttf");      tv.setTypeface(typeFace , 0); // 0 is normal font      tv.setPadding(10,  0, 10, 0); // This is not mandatory  }  

And in an Activity:

@Override  protected void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(Resource.Layout.main_activity);      TextView tv = (TextView)this.findViewById(R.id.text_view);      tv.setText("Insert text that needs to be displayed");      AssetManager assetManager = getContext().getAssets();      Typeface typeFace = Typeface.createFromAsset(assetManager, "Fonts/OpenSans-Light.ttf");      tv.setTypeface(typeFace , 0); // 0 is normal font      tv.setPadding(10,  0, 10, 0); // This is not mandatory  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.