How to set LineSpacing programmatically? - android

i am new to Android Studio . I want to increase or decrease textView's line spacing on click. currently i am using this one and it works but i want when user click + than line spacing increased and decreased for - .
here is my code.
textView.setLineSpacing(0,1.1f);
i have tried this but not works
private int textSpace = (int) 1.0f;
private int diff = (int) 0.1f;
and than this
textSize = textSpace+diff;
textView.setLineSpacing(0,textSize);
same for minus, but its not working , please help

Here is a checked example. I hope everything is well described
// Here put ids of your views
TextView textView = findViewById(R.id.textView);
Button plusButton = findViewById(R.id.plus);
Button minusButton = findViewById(R.id.minus);
// The multiplier may be unchanged
final float multiplier = textView.getLineSpacingMultiplier();
// Set the appropriate offset
final float offset = 1f;
plusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() + offset, multiplier));
minusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() - offset, multiplier));

I suppose you want to achieve something like this:
private int textSize = 20;
button.setOnClickListerner {
textSize += diff;
textView.setLineSpacing(0, textSize);
}

Related

Detect new line in TextFormField - Flutter

I have a Card widget with TextFormField as a child. I want to increase the height of the card each time a user gets to a new line. The card is wrapped in a Container and I have a height variable to determine the height.
How can I detect new line in TextFormField? Also whenever the text wraps to the next line.
I think you need to create a function to check the TextFormField specific line.
I think this an be useful for you "https://stackoverflow.com/questions/45900387/multi-line-textfield-in-flutter"
To update height without pressing enter:
onChanged: (String e) {
int sizeIncreaseConstant = 30; //the fontSize 20 + buffer
int widthOfCharacter = 17; // 85% of fontsize
int newNumLines = ((e.length * widthOfCharacter)/widthOfContainer).truncate();
if( newNumLines != numLines) {
setState(() {
if(newNumLines > numLines)
heightOfContainer = heightOfContainer + sizeIncreaseConstant;
else
heightOfContainer = heightOfContainer - sizeIncreaseConstant;
numLines = newNumLines;
});
}
},
initial values:
int numLines = 0;
double widthOfContainer = 120;
double heightOfContainer = 50;
//fontSize = 20;
For this, you will have to use a font that has equal width for all characters like Monospace. And you will also need to determine the width of the character based on fontSize. It is supposed to be 50-60% of the font size but 85% worked for me.

How can i change text rotation in Luck Wheel (Android)

Here the link of GitHub Code https://github.com/thanhniencung/LuckyWheel
I try to change the rotation of canvas using this piece of code
private void drawText(Canvas canvas, float tmpAngle, float sweepAngle, String mStr) {
Path path = new Path();
path.addArc(mRange,tmpAngle,sweepAngle);
float textWidth = mTextPaint.measureText(mStr);
int hOffset = (int) (mRadius * Math.PI / mLuckyItemList.size()/2-textWidth/2);
int vOffset = mRadius/2/4;
canvas.drawTextOnPath(mStr, path, hOffset, vOffset, mTextPaint);
}
I want to change orientation of text like picture shown below
In the latest version of the LuckyWheel, when initialising LuckyItems data, you can set the values for luckyItem.secondaryText instead of luckyItem.topText, and you will have the wanted text orientation.
Like this:
List<LuckyItem> data = new ArrayList<>();
for (int i = 0; i < sectorsTitles.length; i++) {
LuckyItem luckyItem = new LuckyItem();
// luckyItem.topText = sectorsTitles[i]; // Don't use this
luckyItem.secondaryText = sectorsTitles[i]; // Use this instead
luckyItem.color = Color.parseColor(ROULETTE_COLORS[i]);
data.add(luckyItem);
}
luckyWheelView.setData(data);
In XML code you can change the rotation by using
android:rotation="120" (Then any angle you want positive or negative)
But, you may need to rotate this image when it is in the bottom and then rotate it again when it is in the top. So you can rotate it programatically whenever you want.
TextView textView = findViewById(R.id.textView);
ImageView imageView = findViewById(R.id.imageView);
textView.setRotation(120);
imageView.setRotation(120);
And then if you want to comeback it latter to the position it was in the beginning. You just add the negative sign.
textView.setRotation(-120);
imageView.setRotation(-120);
Of course, the angle is up to you.

Place ListView item at the center of the visible region

I have a ListView which contains an ArrayAdapter which contains items for each hour of the day, and I need it to place the item which contains the current hour in the center of the visible region of the ListView.
I've tried setSelection, but it will place the item on the top instead of the center.
Is there any method to do it without calculating layout heights and such?
Right now I'm using this method, because I know the items are 40dip tall, but they won't always be 40dip tall.
private void centerListView() {
final Calendar c = Calendar.getInstance();
final int hour = c.get(Calendar.HOUR_OF_DAY);
final int listViewProgrammerPixels = listViewProgrammer.getHeight();
final int itemPixels = (int) (40 * getResources().getDisplayMetrics().density + 0.5f);
final int pixelsFromTop = (listViewProgrammerPixels - itemPixels) / 2;
listViewProgrammer.setSelectionFromTop(hour, pixelsFromTop);
}
I have done this using this logic first you have to get height of ListView and the height of View in adapter getView method then write this code in your click event .
int offset = ((listViewheight / 2) - (viewHeight / 2));
mListView.smoothScrollToPositionFromTop(position,Math.abs(offset));
Hope this will work for you.
Use this property item_layout.setGravity(Gravity.CENTER);

Show expanded float value in Android

I have a function that simply converts units, and my goal is for the output number to be displayed in expanded form, as in showing the full number, not "3.0E7". The code is as follows:
public void convertUnits(View view)
{
EditText fromNumberEditText = (EditText)view.findViewById(R.id.fromField);
TextView toNumberEditText = (TextView)view.findViewById(R.id.toField);
String fromUnitString = selectedList[selectedLeftItem];
String toUnitString = selectedList[selectedRightItem];
float fromUnit = Float.parseFloat(fromUnitString.split(",")[2]);
float toUnit = Float.parseFloat(toUnitString.split(",")[2]);
float fromNumber = Float.parseFloat(fromNumberEditText.getText().toString());
float toNumber = (fromNumber * fromUnit) / toUnit;
toNumberEditText.setText(Float.toString(toNumber));
}
The toNumberEditText has marquee, and because of that I would like for it to show the expanded number, i.e. 30000000 not 3.0E7. Whenever the toNumber float is anything more than 7 digits it assumes the "floating e" form. Is there a way to either change the amount of digits before it does this, or remove it altogether?
Use String.format():
toNumberEditText.setText(String.format("%f", toNumber));

Android EditText real content height

I have an EditText in Android. It has long content and it scrolls into pages
How can I find the real height of content, not just the visible area?
I tried this, but it just gives height of visible area:
EditText editor=(EditText)findViewById(R.id.ed1);
.
.
.
double H=editor.getHeight();
not H is 1150, not 10000 or something like that which is real height of content.
This is what I do:
EditText tv = ...;
if (Build.VERSION.SDK_INT >= 16) {
totalHeight = Math.round((tv.getLineCount() * (tv.getLineHeight() + tv.getLineSpacingExtra()) *
tv.getLineSpacingMultiplier())) + tv.getCompoundPaddingTop() + tv.getCompoundPaddingBottom();
} else {
totalHeight = tv.getLineCount() * tv.getLineHeight() + tv.getCompoundPaddingTop() + tv.getCompoundPaddingBottom();
}
You can do this in following steps
count the lines in the edit text get help from here
after that calculate the height(includeing top padding) of single line then simply multiply that number to get the real height of the text inside the editText
It was enough for me to call EditText.getLayout().getHeight();
I've just debugged an app where I need this functionality too:
int contentHeight = view.getTextArea().getLayout().getHeight();
int visibleAreaHeight = view.getTextArea().getHeight();
The values are:
contentHeight = 7531
visibleAreaHeight = 1408

Categories

Resources