How to make the height match the relative width? - java

so I got this xml code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#drawable/profilbildfrau"
android:id="#+id/bild0"
>
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#drawable/profilbildfrau"
android:id="#+id/bild1">
</ImageView>
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#drawable/profilbildfrau"
android:id="#+id/bild2">
</ImageView>
</LinearLayout> (...)
And this is my JavaCode:
ImageView beispiel;
ImageView[] profilbilder = new ImageView[21];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hauptmenue);
beispiel = (ImageView)findViewById(R.id.bild0);
int weite = beispiel.getWidth();
profilbilder[0] = (ImageView)findViewById(R.id.bild0);
profilbilder[1] = (ImageView)findViewById(R.id.bild1);
profilbilder[2] = (ImageView)findViewById(R.id.bild2);
profilbilder[3] = (ImageView)findViewById(R.id.bild3);
profilbilder[4] = (ImageView)findViewById(R.id.bild4);
profilbilder[5] = (ImageView)findViewById(R.id.bild5);
profilbilder[6] = (ImageView)findViewById(R.id.bild6);
profilbilder[7] = (ImageView)findViewById(R.id.bild7);
profilbilder[8] = (ImageView)findViewById(R.id.bild8);
profilbilder[9] = (ImageView)findViewById(R.id.bild9);
profilbilder[10] = (ImageView)findViewById(R.id.bild10);
profilbilder[11] = (ImageView)findViewById(R.id.bild11);
profilbilder[12] = (ImageView)findViewById(R.id.bild12);
profilbilder[13] = (ImageView)findViewById(R.id.bild13);
profilbilder[14] = (ImageView)findViewById(R.id.bild14);
profilbilder[15] = (ImageView)findViewById(R.id.bild15);
profilbilder[16] = (ImageView)findViewById(R.id.bild16);
profilbilder[17] = (ImageView)findViewById(R.id.bild17);
profilbilder[18] = (ImageView)findViewById(R.id.bild18);
profilbilder[19] = (ImageView)findViewById(R.id.bild19);
profilbilder[20] = (ImageView)findViewById(R.id.bild20);
for(int i = 0; i < 20; i++){
profilbilder[i].setMaxHeight(weite);
//ImageView bild = profilbilder[i];
//bild.getLayoutParams().height = weite;
profilbilder[i].setMinimumHeight(weite);
profilbilder[i].requestLayout();
}
}
My goal is to get the width of one of the pictures after it has its correct width fitting the screen and then set the height of each picture to the width of the picture. Tried some code, this one is my recent one, but the height won't match the width. What am I missing here?
Thank you in advance,
Julian

In onCreate the view is not sized yet and normally the width will return 0. Add OnGlobalLayoutListener to the view and get the height and width of the ImageView inside onGlobalLayout. Then you can set a new LayoutParams to every ImageView :
beispiel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
beispiel.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
beispiel.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
int width = beispiel.getWidth();
for(int i = 0; i <= 20; i++){
// make the height and width of each ImageView equal to width of beispiel
profilbilder[i].setLayoutParams(new LinearLayout.LayoutParams(width, width));
}
}
});

Related

When clicking on object to drag it, it jumps straight to the corner

It happens because I set Rule of CENTER HORIZONTAL and ALIGN PARENT BOTTOM to 0 for the Layout, but without removing those rules, object doesn't move at all. I need these rules, because it's an object created in code, and i can't set it beforehand in xml file.
After jumping away, object is dissorted from my touch, so when i move it, it moves, but away from my finger. Here's the code for Touch Listener, MainActivity and activity main.xml. Sorry it's really messy, but i'm a complete begginer!
public boolean onTouch(View view, MotionEvent motionEvent) {
float x = motionEvent.getRawX();
float y = motionEvent.getRawY();
Rectangle rect = (Rectangle) view;
if(!rect.canMove){
return true;
}
boolean moved=false;
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
lParams.addRule(RelativeLayout.CENTER_HORIZONTAL,0);
lParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
xDelta = x - lParams.leftMargin;
yDelta = y - lParams.topMargin;
rect.bringToFront();
break;
case MotionEvent.ACTION_MOVE:
int yDiff = Math.round((motionEvent.getRawY() - yDelta) / rect.grid ) * rect.grid;
int xDiff = Math.round((motionEvent.getRawX() - xDelta) / rect.grid ) * rect.grid;
if( yDiff >= MainActivity.startBoard && yDiff <= MainActivity.startBoard + (MainActivity.BdimY-rect.dimY+2)*rect.grid
&& xDiff >= MainActivity.topBoard + (rect.dimX)*rect.grid && xDiff <= MainActivity.topBoard + rect.grid*(MainActivity.BdimX) ){
lParams.topMargin = yDiff;
lParams.leftMargin = xDiff;
}
else
{
lParams.leftMargin = (int) (x - xDelta);
lParams.topMargin = (int) (y - yDelta);
}
rect.setAlpha(0.7f);
view.setLayoutParams(lParams);
break;
case MotionEvent.ACTION_UP:
if (((RelativeLayout.LayoutParams) view.getLayoutParams()).topMargin >= MainActivity.startBoard && lParams.topMargin <= MainActivity.startBoard + (MainActivity.BdimY-rect.dimY+2) * rect.grid //27 = PlanszaY-RozmiarKlocka+1 (rozmiar klocka nie wiem czy x czy y)
&& ((RelativeLayout.LayoutParams) view.getLayoutParams()).leftMargin >= MainActivity.topBoard + (rect.dimX) * rect.grid && lParams.leftMargin <= MainActivity.topBoard + rect.grid * (MainActivity.BdimX)) {//5=rozmiarem prostokata+1, 21= PlanszaX+1
rect.canMove = false;
rect.setAlpha(1f);
}
rect.setLayoutParams(lParams);
break;
}
main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout layout = findViewById(R.id.layout);
final ImageView imageSquare = findViewById(R.id.imageViewSquare);
imageSquare.post(new Runnable() {
#Override
public void run() {
TouchListener touchListener = new TouchListener();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
ImageView imageBoard = findViewById(R.id.imageBoard);
getBitmapPositionInsideImageView(imageBoard);
sendViewToBack(imageBoard);
int[] dimensions = getBitmapPositionInsideImageView(imageBoard);
int startOfBoard = dimensions[0];
startBoard = startOfBoard;
int topOfBoard = dimensions[1];
topBoard = topOfBoard;
int gridSize = dimensions[2] / BdimX;//zakładam, że kratki są idealnie kwadratowe
Rectangle r = new Rectangle(getApplicationContext());
int SDimx = 4;
int SDimy = 4;
r.setImageBitmap( createRectangle(SDimx, SDimy, gridSize));
r.dimX = SDimx;
r.dimY = SDimy;
r.grid = gridSize;
rects.add(r);
for(Rectangle piece : rects){
piece.setOnTouchListener(touchListener);
layout.addView(piece);
piece.setLayoutParams(params);
}
}
});
}
and activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/imageBoard"
android:layout_width="317dp"
android:layout_height="466dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.139"
app:srcCompat="#drawable/board2030" />
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageViewSquare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="600dp"
android:layout_marginEnd="205dp"
android:layout_marginRight="205dp"
android:layout_marginBottom="161dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/square" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks for any help!

Dynamically adding views in grid layout doesn't take the size specified

I am creating an app in which I want to dynamically add items to grid layout and want them to look like this:
What I did to achieve this is given as follows:
activity_main.xml
<ScrollView 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity">
<LinearLayout
android:weightSum="1"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridLayout
android:id="#+id/myGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFFFF"
android:layout_weight="1">
</GridLayout>
</LinearLayout>
</ScrollView>
item_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/item"
android:background="#color/itemBackground"
android:layout_weight="4"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/cImage"
android:layout_marginTop="20dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#drawable/icon"/>
<TextView
android:id="#+id/cText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:layout_marginBottom="10dp"
android:text="ORAL CARE"/>
</LinearLayout>
MainActivity.java: (Code adding items in grid layout)
gridLayout = (GridLayout) findViewById(R.id.myGrid);
myArray = new String[]{"pink","blue","red","orange","green","purple","white","black","brown","other"};
int total = myArray.length;
int column = 2;
int row = total / column;
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
LinearLayout linearLayoutItem;
Display display = getWindowManager().getDefaultDisplay();
int displayWidth = display.getWidth();
int displayHeight = display.getHeight();
for(int i =0, c = 0, r = 0; i < total; i++, c++)
{
if(c == column)
{
c = 0;
r++;
}
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_layout, gridLayout, false);
TextView cText = (TextView) v.findViewById(R.id.cText);
ImageView cImage = (ImageView) v.findViewById(R.id.cImage);
cText.setText(myArray[i]);
cImage.setImageResource(R.drawable.icon);
v.setMinimumWidth(displayWidth/2); // setting width and height
v.setMinimumHeight(displayWidth/2);
gridLayout.addView(v);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.rightMargin = 20;
param.topMargin = 20;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
This code is adding the views in grid layout perfectly. But the problem is that it is not adjusting the size of view according to the screen, as shown:
I have tried to make the view's width equal to half the space of screen but it doesn't seem to work. The image which I am using in imageView is large but I have tried experimenting with the size of the imageView as well but it doesn't work.
What I want is to:
Adjust the views (the gray colored views with image and text) according to the size of the screen on which it is run. As width of grid layout is match_parent.
Height of the view to be in ratio with width of the view.
Size of the image in the view to be in ratio with the size of the view.
Thanks in advance. Any help would be appreciated.

Creating Linear layout with Horizontal scroll view at the bottom of the screen

I am trying to create a 5x5 table with buttons that take 70% of the space Of the screen. Below this i want to put buttons horizontally with a scrollbar (at the bottom of the screen that means).
The java and xml files are shown below.
On the output screen i can only see the 5x5 table created and the bottom space is left blank. I am new to android and layout so please help me with this. Thanks!
xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/mainPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
<HorizontalScrollView
android:id="#+id/hsvMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/scroll_ll"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbars="horizontal"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Java file:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.match_main_skl);
table = new TableLayout (this);
setControls();
hsv = new HorizontalScrollView(this);
ll = new LinearLayout(this);
mainTable();
}
private void setControls()
{
table = (TableLayout) findViewById(R.id.mainPage);
hsv = (HorizontalScrollView) findViewById(R.id.hsvMain);
ll = (LinearLayout) findViewById(R.id.scroll_ll);
}
private void mainTable()
{
//find screen size of the device (supports all versions)
Point windowSize = MAUIUtil.GetDefaultWindowSize(this);
int cellSize;
if (windowSize.x >= 0.70 * windowSize.y)
cellSize = windowSize.x / numColumns;
else cellSize = (int) ((0.70*windowSize.y)/numColumns);
**//5x5 TABLE**
TableRow.LayoutParams buttonRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1.0f);
buttonRow.height = cellSize * numRows;
//buttonRow.height = windowSize.x;
table.setLayoutParams(buttonRow);
table.setStretchAllColumns(true);
for(int rw = 0; rw < numRows; rw++)
{
TableRow row = new TableRow(this);
row.setPadding(0, 0, 0, 0); //setPadding(left,top,right,bottom)
row.setId(ROW_ID_OFFSET + rw);//giving each row an id
TableLayout.LayoutParams tbLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1.0f);
tbLp.height = cellSize;
table.setLayoutParams(tbLp);
for(int col = 0; col < numColumns; col++)
{
int btnID = COLUMN_ID_OFFSET + (numColumns * (rw - 1)) + col;
//Adding buttons
Button btn = new Button(this);
btn.setId(btnID);
btn.setPadding(0, 0, 0, 0);
btn.setTypeface(Typeface.DEFAULT_BOLD);
btn.setBackgroundColor(Color.CYAN);
btn.setText(Integer.toString(btnID));
row.addView(btn);
}
table.addView(row);
}
**//LAST SCROLLABLE HORIZONTAL ROW**
hsv.addView(ll);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
llp.height = windowSize.y - cellSize;
hsv.setLayoutParams(llp);
LinearLayout sll = new LinearLayout(this);
for(int col = 0; col < scrNumColumns; col++)
{
int btnID = SCROLL_COLUMN_ID_OFFSET + col;
//Adding buttons
Button btn = new Button(this);
btn.setId(btnID);
btn.setPadding(0, 0, 0, 0);
btn.setTypeface(Typeface.DEFAULT_BOLD);
btn.setBackgroundColor(Color.RED);
btn.setText(Integer.toString(btnID));
sll.addView(btn);
sll.setGravity(Gravity.BOTTOM);
}
ll.addView(sll);
}
It seems that you created a new LinearLayout and a new HorizontalScrollView after you called findViewById() in the onCreate() method. So you are operating the new created views rather than the views defined in the .xml file.
Try delete these rows:
hsv = new HorizontalScrollView(this);
ll = new LinearLayout(this);
Use below code.I have implemented it as you have described.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
</TableLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Hope it will help.You can ask if you have any further queries.
use android:alain_parentBottom="true" in linear layout.and you will achieve what you want.

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.

setMinimumHeight() on LinearLayout isn't working (in subclass)

I have a subclass of a liner layout (referenced from XML custom type) The minimum height I've tried to set just isn't working nor is setting the layout. I need to do this programatically...
Why isn't setMinimumHeight() working?
NB: The complexity of the code can be ignored, the value it is setting is 70
public class SummaryDynamicSpacer extends LinearLayout {
private static final String TAG = "mymeter-DynamicSpacer";
int dpWidth = 0;
int dpHeight = 0;
public SummaryDynamicSpacer(Context context) {
super(context);
init(context);
}
public SummaryDynamicSpacer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
//Set dpWidth, this is the key metric we use for scaling.
float density = getResources().getDisplayMetrics().density;
float dpWidth = metrics.widthPixels / density;
float dpHeight = metrics.heightPixels / density;
this.dpWidth = (int)dpWidth;
this.dpHeight = (int)dpHeight;
Log.d(TAG, "zyxHeightDP: "+metrics.heightPixels / density);
int id = this.getId();
int increment = 10;
int adjustment = getValueForLargeDevice(increment);
if(id == R.id.betweenAddressAndTopBox) {
doBetweenAddressAndTopBox(context, adjustment);
} else if(id == R.id.betweenTopAndAddress) {
doBetweenTopAndAddress(context, adjustment);
}
Log.d(TAG, "SettingHeight: "+adjustment);
this.setBackgroundColor(Color.BLUE);
}
private void doBetweenAddressAndTopBox(Context context, int adjustment) {
this.setMinimumHeight(adjustment);
}
private void doBetweenTopAndAddress(Context context, int adjustment) {
this.setMinimumHeight(adjustment);
}
private int getValueForLargeDevice(double increment) {
int largeDeviceAdjustment = 0;
if(dpHeight > 750) {
int difference = (int)dpHeight - 750;
int interval = 25;
int iterations = (difference / interval) > 12 ? 12 : (difference / interval);
int increasePerInterval = 0;
for(int i=0; i < iterations; i++) {
largeDeviceAdjustment += increment;
}
return largeDeviceAdjustment;
} else {
return 1;
}
}
}
I think your code is fine. Your layout likely has an issue. Did you set the layout as follows?
<com.example.dynamicspacerlayout.SummaryDynamicSpacer
android:id="#+id/betweenAddressAndTopBox"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
See this also does minHeight do anything?
GalaxyNexus Screen Shot running your code
Nexus7 Screen Shot
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Top Text">
<requestFocus />
</EditText>
<com.example.dynamicspacerlayout.SummaryDynamicSpacer
android:id="#+id/betweenAddressAndTopBox"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/white"
/>
</com.example.dynamicspacerlayout.SummaryDynamicSpacer>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Bottom Text"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
int minimumHeight = findViewById(R.id.betweenAddressAndTopBox).getHeight();
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("Height: " + minimumHeight);
// text.setVisibility(minimumHeight > 1 ? View.VISIBLE : View.GONE);
}
}, 1000);
}
}
Hope that Helps!

Categories