How to Auto Size text by height programmaticaly? - java

I have a vertical LinearLayout with a height of 32dp and i want to fit two TextView of 16dp each. The length of the text is different and i want it to always nicely fit in a single line.
I have tried with an AutoTextView and with a classic TextView + AutoSizing by TextViewCompat but none of these really work. My text shrink a bit but always finish to be crop at the bottom.
Here the base code
LinearLayout adressTextLayout = new LinearLayout(activity);
adressTextLayout.setOrientation(LinearLayout.VERTICAL);
adressTextLayout.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//icon_size_big = 32dp
(int) (activity.getResources().getDimension(R.dimen.icon_size_big))));
boolean noAdress = true;
//First line with Country, Region and department
if(!mondialAdress.isEmpty()){
noAdress = false;
TextView tx = Utils.createAutoFitTextView(activity, mondialAdress, true, (int) (activity.getResources().getDimension(R.dimen.icon_size_big)/2));
tx.setTypeface(null, Typeface.ITALIC);
adressTextLayout.addView(tx);
}
//Second line with adress, postal code and city
if(!localAdress.isEmpty()){
noAdress = false;
TextView tx = Utils.createAutoFitTextView(activity, localAdress, true, (int) (activity.getResources().getDimension(R.dimen.icon_size_big)/2));
adressTextLayout.addView(tx);
}
//Default line in case we didn't found any adress
if(noAdress){
adressTextLayout.addView(Utils.createAutoFitTextView(activity, activity.getResources().getString(R.string.hint_provide_information), true, 0));
}
Here is the createAutoFitTextView() function with TextViewCompat
public static TextView createAutoFitTextView(Activity activity, String text, boolean singleLine, int maxHeight) {
TextView textView = new AutofitTextView(activity);
textView.setText(text);
//MATCH_PARENT by default
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, maxHeight > 0 ? maxHeight : ViewGroup.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
//Auto Sizing by TextViewCompat
TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
if(singleLine){
textView.setSingleLine(true);
}
return textView;
}
Here the function with AutoFitTextView
public static AutofitTextView createAutoFitTextView(Activity activity, String text, boolean singleLine, int maxHeight) {
AutofitTextView textView = new AutofitTextView(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, maxHeight != -1 ? maxHeight : ViewGroup.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
textView.setText(text);
if(singleLine){
textView.setSingleLine(true);
}
return textView;
}
Here a the result :
With TextViewCompat
With AutoFillTextView:

Related

Programmatically create fixed width columns in android

I'm trying to create a table in which each of the columns is as wide as the widest textview.
The first row I'm trying to create will be filled with names.No matter what I try I cannot seem to get all the columns to be the same width. Please note that I do not want to stretch the columns to the width of the screen, if I have to scroll horizontally, that's okay.
So basically if the widest name is 100dp wide, I want every column to be that wide. I figured by setting the weight in my TextViews I would get evenly spaced columns, but that does not seem to be the case.
Here is the code I have so far to create the first row in my table.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_scores);
game = (Game) getIntent().getExtras().getSerializable("game");
TableLayout detailedScoreLayout = findViewById(R.id.detailedScoreLayoutId);
// First create the row with all the player names.
detailedScoreLayout.addView(createNameRow());
}
}
private TableRow createNameRow() {
TableRow row = new TableRow(this);
for (GamePlayer player: game.getPlayers()) {
row.addView(createName(player.getName()));
row.addView(createVerticalDivider());
}
return row;
}
private TextView createName(String name) {
TextView nameText = new TextView(this);
nameText.setText(name);
formatText(nameText, 24);
return nameText;
}
private void formatText(TextView text, int textSize) {
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.MATCH_PARENT);
int margin = intToDp(12);
int halfMargin = intToDp(6);
params.setMargins(margin, halfMargin, margin, halfMargin);
text.setLayoutParams(params);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
text.setTextColor(Color.BLACK);
text.setGravity(Gravity.CENTER);
text.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
private View createVerticalDivider() {
View div = new View(this);
div.setLayoutParams(new TableRow.LayoutParams(intToDp(1), TableRow.LayoutParams.MATCH_PARENT));
div.setBackgroundColor(Color.BLACK);
return div;
}
Any idea what I'm missing here?
why don't you try to use a library like
https://github.com/evrencoskun/TableView.
it is easy to customize and use.

Android Java Vertical scroll view size

I have a very simple activity with a vertical scroll view (mainscreen.xml below).
This vertical scroll view has a linear layout child view.
I dynamically add text views as children to this linear layout (populateLinlayWithTextViews() below).
Later, after these text view children have been added, I can scroll the vertical scroll view (onScrollChange() below).
And I can measure the visible part of the vertical scroll view (getHeight()).
My problem is that I need to know earlier than at scroll time what is the maximum height of the visible part of the vertical scroll view.
mainscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollview"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:id="#+id/linlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
MyActivity.java
protected void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
Resources resources = getResources();
int n_statusBarHeight = resources.getIdentifier( "status_bar_height", "dimen", "android"); // 36px
populateLinlayWithTextViews();
// HERE
ScrollView sv = sv = findViewById(R.id.scrollview);
int n_svVisiblePartHeight = sv.getHeight(); // 0px
View.OnScrollChangeListener onScrollChangedListener
= new View.OnScrollChangeListener()
{
#Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
ScrollView sv = sv = findViewById(R.id.scrollview);
int n_svVisiblePartHeight = sv.getHeight(); // 680px
}
}
sv = findViewById(R.id.scrollview);
sv.setOnScrollChangeListener(onScrollChangedListener);
}
public void populateLinlayWithTextViews()
{
LinearLayout linlay = findViewById(R.id.linlay);
int n_viewHeight = 81; // px
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
n_viewHeight);
for(int n_i=0 ; n_i<20 ; n_i++)
{
TextView tvNew = new TextView(m_app);
tvNew.setText(n_i + " - test");
tvNew.setTextColor(Color.rgb(0, 0, 0)); // black
tvNew.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
tvNew.setLayoutParams(params);
linlay.addView(tvNew, linlay.getChildCount());
}
}
When I scroll the vertical scroll view, I can see that n_svVisiblePartHeight's value is 680px.
But I need to know this value earlier // HERE.
But // HERE, the value I get is 0px.
Can you help me?
EDIT 1
I need to know this height because:
example 1: I would like to know initially the index of the text view that will be at the bottom of the (visible) screen after I have populated
the linear layout with text views ;
example 2: given this height X I'm looking for, let's say that I would like 10 text views to be visible initially, I would like to determine X/10 which will be the height of the text views, so that I can put it dynamically in n_viewHeight.
EDIT 2
I just need to know the height of the red arrow below, a soon as I can in the lifecycle of the activity and preferably before I add text views to the linear layout or right after (// HERE):
EDIT 3
I don't know if theoretically that's true:
n_scrollViewVisibleHeightInPx =
n_displayHeightInPx
- n_actionBarHeight
- n_navigationBarHeightInPx
+ n_statusBarHeightInPx;
... but practically n_scrollViewVisibleHeightInPx is the height of the space designated by the red arrow in the image above.
To get these various heights, below are the methods I used:
private int returnStatusBarHeightInPx()
{
int n_statusBarHeightInPx = -1;
Resources resources = null;
int n_idStatusBarHeight = -1;
resources = getResources();
n_idStatusBarHeight = resources.getIdentifier( "status_bar_height", "dimen", "android");
if(n_idStatusBarHeight > 0)
{
n_statusBarHeightInPx = getResources().getDimensionPixelSize(n_idStatusBarHeight);
}
else
{
n_statusBarHeightInPx = 0;
}
return n_statusBarHeightInPx;
}
private int returnNavigationBarHeightInPx()
{
int n_navigationBarHeightInPx = -1;
Resources resources = null;
int n_idNavigationBarHeight = -1;
resources = getResources();
n_idNavigationBarHeight = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if(n_idNavigationBarHeight > 0)
{
n_navigationBarHeightInPx = getResources().getDimensionPixelSize(n_idNavigationBarHeight);
}
else
{
n_navigationBarHeightInPx = 0;
}
return n_navigationBarHeightInPx;
}
private int returnActionBarHeightInPx()
{
TypedValue typedValue = null;
int n_actionBarHeightInPx = -1;
typedValue = new TypedValue();
if(getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true))
{
n_actionBarHeightInPx =
TypedValue.complexToDimensionPixelSize(
typedValue.data,
getResources().getDisplayMetrics());
}
return n_actionBarHeightInPx;
}
private int returnDisplayHeightInPx()
{
DisplayMetrics displayMetrics = null;
int n_displayHeightInPx = -1;
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
n_displayHeightInPx = displayMetrics.heightPixels;
return n_displayHeightInPx;
}
Why do you want to know it in "//here". You do not use that variable there. Your code does not explain, why it is not possible, to define the variable on scroll time.

How to calculate the height of a scrollView using ViewTreeObserver?

I have a ScrollView with some layouts in it, called the fragment_about_sl.xml.And the class associated to it is called AboutSLFragment.java.I want to calculate the height of the scrollView to get height ratio for an image.I researched the web and found this code.
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int scrollViewHeightInPixels = layout.getMeasuredHeight();
//This is equal with %45 weight you tried before
int height = (scrollViewHeightInPixels * 45) / 100;
ViewGroup.LayoutParams pagerParams = viewPager.getLayoutParams();
pagerParams.height = height;
}
});
As shown in the above code.It just have said scrollView instead of getting it from findViewById().And also in the below code,it just says layout.getMeasuredHeight() instead of specifying the name of layout.
int scrollViewHeightInPixels = layout.getMeasuredHeight();
The question is that I want to know what is meant by layout here(above).What should I put there,is it the id of the scrollView?If so I developed a code myself and I want to know whether it is correct.Please help me I am new to android.The id of the scrollview I used is aboutslscrollview.
final View view = rootView.findViewById(R.id.aboutslscrollview);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int scrollViewHeightInPixels = view.getMeasuredHeight();
//This is equal with %45 weight you tried before
int height = (scrollViewHeightInPixels * 45) / 100;
ViewGroup.LayoutParams pagerParams = viewPager.getLayoutParams();
pagerParams.height = height;
}
});

Android sliding tabs - 2 line text on tabs

I came across this image online and I was wondering: is it possible to have a two line text view for one of my tabs within my sliding tabs project?
Hello tab error
DeafultTabView code
protected TextView createDefaultTabView(Context context) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setTextColor(context.getResources().getColor(R.color.black));
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
textView.setWidth(size.x / 2); // (size.x / number of textviews);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// If we're running on Honeycomb or newer, then we can use the Theme's
// selectableItemBackground to ensure that the View has a pressed state
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
textView.setAllCaps(true);
}
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
textView.setPadding(padding, padding, padding, padding);
return textView;
}
Use this in the TextView returned from createDefaultTabView() method
myTextView.setText(Html.fromHtml("Line one <br/>Line two"));
set text like this (Generic condition)
private void setCustomText(TextView textView, String[] msg, boolean isSingleLine){
if(isSingleLine)
textView.setText(msg[0]);
else
textView.setText(TextUtils.join("<br/>", msg));
}

Android Issue with activity context

So I have a helper class that reuses a lot of code through out the application, one of the methods is shown below:
public void setTitleTextSize(final int id){
infoButton = ((Activity) context).findViewById(R.id.info_button);
ViewTreeObserver customTitleScale = infoButton.getViewTreeObserver();
customTitleScale.addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
int infoWidth = infoButton.getMeasuredWidth();
if(infoWidth !=0){
if(Build.VERSION.SDK_INT >= 11.0){
TypedValue tv = new TypedValue();
((Activity) context).getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
actionBarWidth = ((Activity) context).getResources().getDimensionPixelSize(tv.resourceId);
}
DisplayMetrics metrics = new DisplayMetrics();
Display Screen = ((Activity) context).getWindowManager().getDefaultDisplay();
Screen.getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int titleWidth = screenWidth - infoWidth - actionBarWidth;
TextView titleText = (TextView) ((Activity) context).findViewById(R.id.title_text);
titleText.setText(id);
TextPaint paint = titleText.getPaint();
Rect rect = new Rect();
String text = String.valueOf(titleText.getText());
int textLength = text.length();
paint.getTextBounds(text, 0, textLength, rect);
if(rect.width() > titleWidth){
float scale = (float) titleWidth / (float) rect.width();
float textSize = titleText.getTextSize();
float scaleSize = (float) (textSize * (scale*0.8));
titleText.setTextSize(TypedValue.COMPLEX_UNIT_PX, scaleSize);
}
infoButton.getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});
}
I use this particular method on all of my activities.
The problem I've got is I don't want to display the infoButton on every activity but when I add View infoButton = findViewById(R.id.info_button); infoButton.setVisibility(View.GONE); to the activity, the screen is just black.
So I was thinking on how to do this and the only thing I can thing of is to pass a boolean into the method stating whether the info view is visible or not. I suppose I'd do an if statement saying `if true then display it, if false then don't but I can't figure out how to do this.
Any help would be amazing thanks.
Try to use method infoButton.getVisibility()
it will return you the integer to get visibility status of you widget.
I figured out the issue I was having, My logic wasn't correct for the if statement if(infoWidth !=0). The code wasn't running if the activity didn't have an info button because the infoWidth would always be zero. So I changed the logic to if(infoWidth !=0 || !displayInfoButton) which works perfectly.

Categories