Search code examples
androidtextword-wrap

Word Wrapping issue in Android


float ratio = 2.666666666666667f; // 1440/540

TextView textView = new TextView(this);

textView.setText("Ayet-i kerimelerin nazil olmasından sonra Fahr-i Kâinat Efendimiz s.a.v.’in daha fazla ibadet ile meşgul olması, nail olunan büyük ihsan ve ikramlara şükretmesiyle alakalıdır. Nitekim Hz. Aişe r. anha validemizin sorusu üzerine bunu doğrudan ifade de buyuruyorlar. Bizim açımızdan da şükrün edası gayret gerektirir. İbadet ve taatlerimizde, işlerimizde…\n\nFetih sure-i şerifi ile Fahr-i Kâinat Efendimiz s.a.v.’in ümmetinden olarak bizler de nice fetihlerle, gayret ve çabamıza karşılık ilahî mağfiret ve yardımlarla müjdeleniyoruz.\n\nMüminler olarak Cenab-ı Mevlâ’dan asla ümit kesmeden, azami gayret ve çaba göstererek mücadelemize devam ettiğimizde, müjdelendiğimiz fetihler de müyesser olacaktır inşallah.");
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,14*ratio);
textView.setBackgroundColor(Color.parseColor("#ff0000"));
textView.setTextColor(Color.parseColor("#333333"));

RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int)(510*ratio), RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int)(15*ratio);
textView.setLayoutParams(params);

root.addView(textView);

As seen in the image below sometimes some texts may be wrapped down to the next line where it shouldn't. The word circled in the image appears to have moved down to the next line whereas it could easily fit in the remaining space in the current line. This situation might be the result of the calculation of android text engine. The project i am working on is sensitive in that regard. What can i do to prevent this from happening?

enter image description here


Solution

  • Finally i solved the issue:

    if(Build.VERSION.SDK_INT >= 23)
       textView.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);