TextView ellipse disappears on setText() - java

Good afternoon.
I have the following TextView:
<TextView android:id="#+id/keypadwidget_number"
android:tag="#string/phone_number_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:ellipsize="start"
android:scrollHorizontally="true"
android:gravity="right"
android:singleLine="true"
android:textColor="#color/widget_text"
android:layout_toLeftOf="#+id/keypadwidget_btn_delete"/>
My expected result is to have the following:
...99862214
But instead, I'm getting:
Sorry about the background/text colour. Not my choice!
Each of those buttons call:
mPhoneNumberTextView.append(clickValue);
My question is; What am I doing wrong?
EDIT:
When I set the TextView's android:text property to something very long, the ellipse is set as expected. It's when mPhoneNumberTextView.append(clickValue); is run that it breaks.

Use android:layout_width="fill_parent" for your TextView

In a fit of rage, I added a drawable to the left of the text view every time it was marqueed.
private boolean isMarqueed(TextView tv){
return isMarqueed(tv.getText().toString(), mPhoneNumberTextView.getWidth(), mPhoneNumberTextView);
}
private boolean isMarqueed(String text, int textWidth, TextView tv) {
Paint testPaint = new Paint();
testPaint.set(tv.getPaint());
boolean isMarquee = true;
if (textWidth > 0) {
int availableWidth = (int) (textWidth - tv.getPaddingLeft() - tv.getPaddingRight()-testPaint.measureText(text));
if(availableWidth > 0)
isMarquee = false;
}
return isMarquee;
}
Which is a slight variation of How to check if a TextView String has been trimmed (marquee)?

Related

Android, Can't change textView when use getLayoutInflater()

I'm trying to set text in the tooltip inside my layout but It's now working, what I did is get the tooltip layout using getLayoutInflater() and get the textView then assign a new value to it, but this is not working I don't know why here's my code below if I'm missing anything please tell me.
My code
findViewById(R.id.img_hint).setOnClickListener(e -> {
TooltipWindow tipWindow = new TooltipWindow(SelectAnAmount.this);
View toolTipView = getLayoutInflater().inflate(R.layout.tooltip_layout, null, false);
TextView textView = toolTipView.findViewById(R.id.text_title);
textView.setText("test");
int screen_pos[] = new int[2];
findViewById(R.id.img_hint).getLocationOnScreen(screen_pos);
if (!tipWindow.isTooltipShown()) {
tipWindow.showToolTip(toolTipView, screen_pos);
} else {
tipWindow.dismissTooltip();
}
});
And here's the method that I'm using to show the tooltip
public void showToolTip(View anchor,int[]screen_pos) {
tipWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
tipWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
tipWindow.setOutsideTouchable(true);
tipWindow.setTouchable(true);
tipWindow.setFocusable(true);
tipWindow.setBackgroundDrawable(new BitmapDrawable());
tipWindow.setContentView(contentView);
// Get rect for anchor view
Rect anchor_rect = new Rect(screen_pos[0], screen_pos[1], screen_pos[0]
+ anchor.getWidth(), screen_pos[1]-500 + anchor.getHeight());
// Call view measure to calculate how big your view should be.
contentView.measure(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int contentViewHeight = contentView.getMeasuredHeight();
int contentViewWidth = contentView.getMeasuredWidth();
// In this case , i dont need much calculation for x and y position of
// tooltip
// For cases if anchor is near screen border, you need to take care of
// direction as well
// to show left, right, above or below of anchor view
int position_x = anchor_rect.centerX() - (contentViewWidth / 2);
int position_y = anchor_rect.bottom - (anchor_rect.height() / 2);
tipWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, position_x, position_y);
// send message to handler to dismiss tipWindow after X milliseconds
// handler.sendEmptyMessageDelayed(MSG_DISMISS_TOOLTIP, 4000000);
}
tooltip layot
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialog_tooltip"
android:orientation="vertical">
<TextView
android:id="#+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_semibold"
android:text="Tooltip Title"
android:textColor="#color/white"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_title"
android:fontFamily="#font/poppins"
android:text="An example of the text that\n will be displayed in\n this tooltip"
android:textColor="#color/white"
android:textSize="12dp" />
</LinearLayout>
<TextView
android:layout_width="20dp"
android:layout_height="80dp"/>
</LinearLayout>
my layout output img

Change image in drawablle inside an editText field

I am building a password visibility toggle system (Without using TextInputLayout).
It is working, but I wan to change the icon when user clicks on the icon inside the EditText input field.
xml
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/etPassword"
android:layout_marginRight="20dp"
android:inputType="textPassword"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="#drawable/input_border"
android:drawableRight="#drawable/eye_image"
android:hint="Your Password" />
eye_image.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/eye_show"
android:width="25dp"
android:height="25dp"
/>
</layer-list>
java code
password.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (password.getRight() - password.getCompoundDrawables()
[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
if(password.getTransformationMethod().equals(PasswordTransformationMethod.getInstance())){
//Show Password
((ImageView)(view)).setImageResource(R.drawable.eye_show);
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
password.setSelection(password.getText().length());
}
else{
//Hide Password
((ImageView)(view)).setImageResource(R.drawable.eye_show);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
password.setSelection(password.getText().length());
}
}
}
return false;
}
});
I get this error - androidx.appcompat.widget.AppCompatEditText cannot be cast to android.widget.ImageView
I just want to make the image in the eye_image.xml drawable which is placed at the right of the editText to change to another image onClick
Found a way, This did the trick
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.eye_image2, 0);

Dynamically created TextView in FrameLayout has no width

I'm trying to add a TextView in code to a framelayout. This sits above an imageview in the z order of the framelayout. The ultimate aim is to allow the creation of a screenshot from the framelayout that shows the image and the text that has been overlayed on to it. I have this working when using a textview created in xml but not in the dynamic code version. The create bitmap method returns an error complaining about the width of the textbox being 0. In the code below I am trying to capture just the textview as an image to identify what the issue is, as the captured image from the framelayout did not contain the contents of the textview as expected. In doing this I was able to find the width error and I believe it is this that is the root of the problem. I have tried to set the textview's width using setWidth and also using the LayoutParams. The end result is always that the textview has no width although it can be seen on the handset clearly. I think I am missing something between the dynamic creation and the existing xml which results in the 0 width. Can anyone point me in the correct direction please?
The code is as follows
public void applyTextToImage(View view) {
// Do something in response to button
//Hide the virtual keyboard
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//Get the text to overlay on the image
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
//Bring the overlay layout to the front
//LinearLayout overlay_layout = (LinearLayout) findViewById(R.id.image_Overlay_Layout);
//overlay_layout.bringToFront();
//Apply the new text to the text box
/* Old code to get the view that is shown in the layout
TextView text_overlay = (TextView) findViewById(R.id.image_Overlay);
text_overlay.bringToFront();
text_overlay.setText(message);
*/
//New code to create a view dynamically instead
TextView text_Overlay = new TextView(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
text_Overlay.setId(Utils.generateViewId());
}
else
{
text_Overlay.setId(TextView.generateViewId()); //static class
}
FrameLayout image_Layout = (FrameLayout) findViewById(R.id.image_Layout);
//View image_Layout = (View) findViewById(R.id.image_Layout);
//FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
fParams.gravity = Gravity.CENTER;
// text_Overlay.setMaxWidth(image_Layout.getWidth());
// text_Overlay.setWidth(image_Layout.getWidth());
//image_Layout.addView(text_Overlay, fParams);
image_Layout.addView(text_Overlay, fParams);
Toast.makeText(this,"TextView Width: " + text_Overlay.getWidth(),Toast.LENGTH_LONG).show();
//TODO: Something has forced the dynamic layout to not be saved in the bitmap try removing the params and set the values on the textview itself
//TODO: for some reason the width keeps coming back as 0 could be that the image_Layout is 0 too
text_Overlay.setGravity(Gravity.CENTER);
text_Overlay.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
//text_Overlay.setWidth(250dp);
text_Overlay.setTextSize(pixels);
text_Overlay.setTextColor(Color.RED);
text_Overlay.bringToFront();
text_Overlay.setText(message);
text_Overlay.setVisibility(View.VISIBLE);
//End of new dynamic code
if (folderCheck()){
try {
String filePath = getFilePath();
int myId = text_Overlay.getId();
Bitmap bitmap;
View v1 = findViewById(myId);
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache();
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
// End imported code
streamBitmapToFile(bitmap, filePath);
}
catch (Exception e)
{
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
Log.e("Bitmap Creation","Couldn't create bitmap error as: " + e.toString());
}
}
}
XML contents
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false">
<!--app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_my"-->
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:enabled="false"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="applyTextToImage"
android:enabled="false"
android:id="#+id/overlayButton"/>
</LinearLayout>
<FrameLayout
android:id="#+id/image_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView
android:id="#+id/image_View"
android:layout_width="match_parent"
android:layout_height="250dp" />
<!--<TextView
android:id="#+id/image_Overlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textSize="25dp"
android:textColor="#ff0000"/>-->
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/open_gallery"
android:onClick="openGallery">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/new_image"
android:onClick="newImage">
</Button>
</LinearLayout>
</RelativeLayout>
The view has not been measured yet. Until the system does another layout pass, the view reports its width as zero.
In my opinion, you are better off leaving the TextView in the XML layout and simply making it invisible (android:visibility="invisible") until you need it, then make it visible prorammatically with setVisbility(View.VISIBLE). (Note that if you set it to be gone, it will also not be measured.)
For reference Karakuri pointed out the weaknesses and the path to follow to resolve them.
Extra code that was implemented for the listener is as follows.
IMAGE_CAPTURE_REQUESTED = true;
// New Listener
ViewTreeObserver vto = image_Layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
image_Layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
else
{
image_Layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
if (IMAGE_CAPTURE_REQUESTED) {
//int myId = text_Overlay.getId();
//Toast.makeText(MyActivity.this, "Text Overlay Width " + text_Overlay.getWidth() ,Toast.LENGTH_LONG).show();
overlayTextAndExportImage();
}
}
}); //End New Listener
The new overlayTextAndImportImage calls all the image creation routines after the layout has been redrawn.

change position when button clicked?

I've two object in android xml, my object locate in horizontal orientation, imageview in left and textview in right,my code look like
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
/>
<TextView
android:id="#+id/txtFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Nama File : test.zip"
android:textSize="10dip" />
</LinearLayout>
i want to switch 2 object when button fire (imageview to right,textview to left)
how i achieve that ?
The following code should do it. Remove the ImageView and add it to the LinearLayout again. Do this indside the OnClick listener.
LinearLayout row = (LinearLayout) findViewById(R.id.rowFile);
ImageView image = (ImageView) findViewById(R.id.img_dilvChat);
row.removeView(image);
row.addView(image);
row.invalidate();
write these in onClickListener of the Button
(findViewById(R.id.imageView)).getLocationOnScreen(posXY);
int imgx = posXY[0];
int imgy = posXY[1];
(findViewById(R.id.textView)).getLocationOnScreen(posXY);
int txtx = posXY[0];
int txty = posXY[1];
(findViewById(R.id.imageView)).setX(txtx);
(findViewById(R.id.imageView)).setY(txty);
(findViewById(R.id.textView)).setX(imgx);
(findViewById(R.id.textView)).setY(imgy);

Add dots to TextView like in the receipt

I want to implement something like a product list in the receipt:
Beer ......................................... 20
Milk .......................................... 10
Cookies with jam ..................... 15
Smartphone 10GB 3GHz
1GB RAM NFC 10MPx
camera .................................. 400
Description:
Check info (Beer, Milk) I think it shoud be a TextView, which I need to fill with dots.
Money (20, 10) is another TextView which shoud be aligned to the right of ViewGroup.
Any ideas how to do this? Maybe I need inherited from TextView and override onDraw() or something?
Many thank for advices!!!
You should create THREE TextViews for each row, layed out one next to another:
TextView With product name (e.g. 'beer')
TextView with dots
TextView with number (e.g. '20')
Then put one product in one row. The Row should be relative layout where:
TextView no. 1 is aligned to left edge, with width set to wrap content
TextView no. 3 is aligneg to right edge, with width set to wrap content
TextView no. 2 is toLeftOf TV no. 3 and toRightOf TV no.1 and it should be filled with big amount of dots in XML file. this will never be changed.
This will work, since the TV no.2 will have width that will shrink and will always be fitted between product name and the price. believe me :)
I have the solution. Maybe it will help someone.
File check_info_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/txt_fake_value"
android:textSize="18dp"
android:textColor="#android:color/transparent"
android:layout_alignParentRight="true"/>
<example.com.CheckInfoTextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/txt_fake_info"
android:textSize="18dp"
android:textColor="#android:color/transparent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/txt_fake_value"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/txt_check_info_value"
android:text=""
android:textSize="18dp"
android:textColor="#000"
android:layout_alignParentRight="true"
android:layout_alignBottom="#id/txt_fake_info"/>
<example.com.CheckInfoTextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="18dp"
android:textColor="#000"
android:id="#+id/txt_check_info"
android:text=""
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/txt_check_info_value"/>
</RelativeLayout>
</LinearLayout>
Code to fill the info fields (in the Activity):
View row = getLayoutInflater().inflate(R.layout.check_info_item, null);
//Fake fields needed to align base fields in the xml file
TextView txtFakeValue = (TextView) row.findViewById(R.id.txt_fake_value);
txtFakeValue.setText(String.valueOf(pair.second));
TextView txtFake = (TextView) row.findViewById(R.id.txt_fake_info);
txtFake.setText(pair.first);
TextView txtValue = (TextView) row.findViewById(R.id.txt_check_info_value);
txtValue.setText(String.valueOf(pair.second));
TextView txtTitle = (TextView) row.findViewById(R.id.txt_check_info);
txtTitle.setText(pair.first);
And the CheckInfoTextView:
public class CheckInfoTextView extends TextView {
public CheckInfoTextView(Context context) {
super(context);
}
public CheckInfoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CheckInfoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if(!hasWindowFocus) return;
int requiredDots = getRequiredDotsNumber();
if(requiredDots == 0) {
String text = getText().toString();
StringBuilder result = new StringBuilder();
result.append(text.substring(0, text.lastIndexOf(' ')));
result.append("\n");
result.append(text.substring(text.lastIndexOf(' ') + 1));
setText(result.toString());
requiredDots = getRequiredDotsNumber();
}
String dots = "";
for (int i = 0; i < requiredDots; ++i) {
dots += " .";
}
setText(getText() + dots);
}
private int getRequiredDotsNumber() {
final int width = getWidth();
final int lastLineWidth = (int) getLayout().getLineWidth(getLineCount() - 1);
final int availableWidthForDots = width - lastLineWidth;
final int widthOfOneDot = getWidthOfOneDot();
final int widthOfTwoDotsWithSpace = getWidthOfTwoDotsWithSpace();
final int widthOfSpace = widthOfTwoDotsWithSpace - (widthOfOneDot * 2);
final int widthOfDotWithSpace = widthOfSpace + widthOfOneDot;
int numberOfDots = availableWidthForDots / widthOfDotWithSpace;
return numberOfDots;
}
private int getWidthOfTwoDotsWithSpace() {
return getStringWidth(". .");
}
private int getWidthOfOneDot() {
return getStringWidth(".");
}
private int getStringWidth(String text) {
Rect dotBounds = new Rect();
getPaint().getTextBounds(text,0,text.length(),dotBounds);
return dotBounds.width();
}
}
I've changed Serg_'s CheckinfoTextView class so that it both works in the eclipse layout editor, but also adds spaces if possible, to put the page number as close to the right side as possible. I've also changed a bit how it is used.
To accomplish:
Milk...................23
Chocolate cookies......24
set the text to 'Milk 23' and 'Chocolate cookies 24' respectively
The number of spaces is rounded to nearest rather than rounded down, so it's better to put the number a little bit over to the right rather than too much to the left
public class DotAutofillTextView extends TextView {
private int availableWidthForDots;
private int widthOfSpace;
private int widthOfDotWithSpace;
public DotAutofillTextView(Context context) {
super(context);
}
public DotAutofillTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DotAutofillTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int width = getWidth() - getPaddingLeft() - getPaddingRight();
int lastLineWidth = (int) getLayout().getLineWidth(getLineCount() - 1);
availableWidthForDots = width - lastLineWidth;
int widthOfOneDot = getWidthOfOneDot();
int widthOfTwoDotsWithSpace = getWidthOfTwoDotsWithSpace();
widthOfSpace = widthOfTwoDotsWithSpace - (widthOfOneDot * 2);
widthOfDotWithSpace = widthOfSpace + widthOfOneDot;
int requiredDots = getRequiredDotsNumber();
if (requiredDots != 0) {
int spaces = getRequiredSpacesNumber(requiredDots);
StringBuilder result = new StringBuilder();
String text = getText().toString();
result.append(text.substring(0, text.lastIndexOf(' ')));
setText(result.toString());
StringBuilder dots = new StringBuilder();
for (int i = 0; i < requiredDots; ++i) {
dots.append(" .");
}
for (int i = 0; i < spaces; ++i) {
dots.append(" ");
}
result.append(dots.toString());
result.append(text.substring(text.lastIndexOf(' ') + 1));
setText(result.toString());
}
super.onLayout(changed, left, top, right, bottom);
}
private int getRequiredSpacesNumber(int requiredDots) {
float remain = (1f * availableWidthForDots) % (1f * widthOfDotWithSpace);
return (int) ((remain / widthOfSpace) + 0.5f);
}
private int getRequiredDotsNumber() {
if (getLayout() == null) {
return 1;
}
int numberOfDots = availableWidthForDots / widthOfDotWithSpace;
return numberOfDots;
}
private int getWidthOfTwoDotsWithSpace() {
return getStringWidth(". .");
}
private int getWidthOfOneDot() {
return getStringWidth(".");
}
private int getStringWidth(String text) {
Rect dotBounds = new Rect();
getPaint().getTextBounds(text, 0, text.length(), dotBounds);
return dotBounds.width();
}
}
If '.'(dots) is your basic requirement then you can try like this..
My suggestion, Use only I TextView to fit in Beer...10 (hitch)
try like this..
int label_len = 10;//Edit as per your requirement
String MoneyValue = "20";
TextView tv = (TextView)findViewById(your id);
tv.setText("Beer");
if(tv.getText().length() < label_len){
for(int i = tv.getText().length(); i < label_len; i++){
tv.setText(tv.getText()+".");
}
}
tv.setText(tv.getText() + MoneyValue);
This is a sample with hardcoded value, you can try to add dynamically...
Hope this helps...
Use a relative layout:
TextView View TextView
The first set the width to wrap content, and below the text view of the previous row.
The second view, set to to right of first text view, and use a background drawable with dots that repeat horizontally. Set the width to match parent.
The last view, set to right of middle view and width to wrap content and aligned to right of parent.
I've had the same problem and found this solution using ConstraintLayout:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="...................................................................................................."
android:maxLines="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="#id/item"
app:layout_constraintRight_toLeftOf="#id/price"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:text="100"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
You can achieve this by using layout.xml only.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:![enter image description here][2]layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.9"
android:text="Milk.................................................................................................................................." />
<TextView android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.1"
android:text="20" />
</LinearLayout>
</LinearLayout>
This solution will work for all the device with different resolutions.

Categories