background for textview can be scaled programmatically? - android

i need to create balls with numbers in the middle . i need about 10-11 balls in a row so i use weight .
but my balls (background's for textview don't think dirty) are not circle . i want them to save there own proportions .
LayoutParams textParams = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 0.09f);
resultNumber = new TextView(context);
resultNumber.setBackgroundResource(R.drawable.ball_golden);
resultNumber.setGravity(Gravity.CENTER);
resultNumber.setLayoutParams(textParams);
resultNumber.setText("6");
resultLayout.addView(resultNumber);
this is the code , resultNumber added about 10 time for each row .
thanks for your time (-:

You can use a relative layout and position the text view in center for each item. Apply the background resource to the relative layout.

Related

reposition textview in constraint layout dynamically

I'm building an android app that does 3 variations of a similar calculation.
I have an imageview, three radiobuttons, and four textviews inside the imageview that display inputs/results. I have editext for actual input and a button to do the calculation. That is not my problem.
I can load the appropriate image by checking appropriate radiobutton As each image is slightly different I'd like to reposition the the textview with each image so they line up with the image better.
During layout design I loaded the appropriate, positioned the textviews where I want them and copied down the Horizontal and Vertical bias numbers.
Can't figure out how to change the bias to move the textview when I change the image.
I'm using constraint layout.
Thanks Steve
The ConstraintLayout.LayoutParams class has two fields: horizontalBias and verticalBias. You can therefore update the bias values for any view that is a child of a ConstraintLayout like this:
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.verticalBias = 0.3f;
view.setLayoutParams(params);

Creating an array of buttons programatically in android

I am currently learning android and I am trying to create an array of buttons for my app in the following manner:
LinearLayout answer_layout = (LinearLayout)findViewById(R.id.answer_layout);
idCount = answer_layout.getId() + 1000;
for(int i = 0 ; i<letters.length ; i++)
{
Button b = new Button(this);
b.setText(letters[i]);
b.setTypeface(null, Typeface.BOLD);
b.setBackgroundResource(R.drawable.puzzletilebg);
b.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
b.setIncludeFontPadding(false);
b.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
b.setId(idCount + i);
b.setTag(Integer.valueOf(i));
bLetters[i] = b;
answer_layout.addView(b);
}
}
When i run this code, i am able to get a row of buttons depending on the length value of string. My problem is the buttons appear stretched and when the length value is more than 7, The buttons dont appear. For this problem i tried implementing the method suggested here(How do I programmatically add buttons into layout one by one in several lines?) but i didnt get any result. What parameters do i have to use to make the shape of the buttons as a perfect square and make sure they are of the same size for all screen sizes? My button background drawable size is 50x50.
If you want a perfect square change
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
to
LinearLayout.LayoutParams(50, 50);
And ofc if your linearLayour is horizontal the buttons will dissapear
on the edge of the screen.
You can use Gridlayout instead of LinearLayout, but if you want something like the LinearLayout i recommend: FlowLayout
example:
Last edit: sorry is not AutoFitLayout is FlowLayout i edited the answer;
Solve your problem?
Instead of LinearLayout use a GridLayout
Try with
b.setWidth(50);
b.setHeight(50);
The numbers are pixels, so maybe you have to convert dp to pixels.
For the problem with button more than 7 not appear, you could try to wrap your LinearLayout answer_layout in a ScrollView or HorizontalScrollView for horizontal scroll behaviour in the xml file.
And for programmatically change the width and the height of the button, you could try this answer https://stackoverflow.com/a/11294075/1331743
from existing post.

Center view in android after changing its width properties

I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.

Set EditText to percentage of parents's width in LinearLayout

I have a LinearLayout class in which I have:
TexView | ImageView | EditText | ImageView.
I have the last ImageView all the way to the right side of the LinearLayout its wrapped in. The EditText runs very long and in some case pushes the last ImageView out of view (or so it seems to push it out of view).
I want to to have the EditText set to a percentage of the total width. I tried using weight with LinearLayout parameters but it seems to cause the view to get all out of wack. For example here it is for the EditText:
LinearLayout.LayoutParams lpEt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
.50f);
All the other views follow suit but have lesser weights (.2, .2, .1) and they all add up to 1.0. But the LinearLayout row is never spaced correctly.
Should I find out the width of the parent (which is a ListView) and then set the width of the EditText explicitly based on the parent's width or is there a better way?
Im not sure what your linear layout orientation is but im guessing its horizontal. if so try this.
LayoutParams textViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
0,LayoutParams.WRAP_CONTENT,1f);
editText.setLayoutParams(editTextParams);
LayoutParams imageViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageViewParams);
This will make the edit text to fill the remaining space between the textview and imageview. If you need something else, just modify the code a bit. When using weights, set the weight or height as 0 for which you are setting weight for.

Overlap image over two rows in a listview

I need create a listview with pinned overlay image over upper row and next row, like image example below.
Thanks a lot.
set the Layout parameters of image, ie size not exceed to the parent view
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

Categories

Resources