LayoutParams.MATCH_PARENT does not work but integer does - java

I am generating editTexts in a TableRow and the editTexts width is wrapping the content by default. I tried to set their width to match_parent like this:
TableRow tr = new TableRow(getActivity());
EditText et = new EditText(getActivity());
LayoutParams lparams = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
et.setLayoutParams(lparams);
It did not change anything, but i can set their width if i replace LayoutParams.MATCH_PARENT with an integer. Could you please point me in the right direction to see why i cannot set their width to MATCH_PARENT?

I accidentally found out, that if i am not using a TableRow, but placing the editTexts into the TableLayout its width will automaticall match the parent's width.

Related

How can I set a textview's style programmatically?

How can I add a style to a textview in java? I am trying to add one under my values/styles.xml, not to add each attribute individually.
LinearLayout messagesLayout = (LinearLayout) findViewById(R.id.messages_layout);
TextView sentOne = new TextView(this);
sentOne.setText(sentMessage);
messagesLayout.addView(sentOne);
You may find this answer by Benjamin Piette handy. To change this into working code for a TextView just change it up a bit:
TextView tv = new TextView (new ContextThemeWrapper(this, R.style.mystyle), null, 0);
EDIT: to set other things like margins, height & width and others, use LayoutParams. To set the params throrin19's answer can be helpful.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
tv.setLayoutParams(params);
There is an answer for this already: https://stackoverflow.com/a/7919203/5544859
textview.setTypeface(Typeface.DEFAULT_BOLD);
to preserve the previously set typeface attributes you can use:
textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);
Credit to: #Raz

What is this Android Snippet doing?

EDIT** Figured out my own answers except for my major question as follows:
TableLayout tableLayout = new TableLayout(this);
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 1; j <= courseHoleCount; j++) {
**TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));**
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setTextSize(30);
tv.setPadding(5, 5, 5, 5);
tv.setText(String.valueOf(players[i-1].getScore()[j-1]));// (-1) because j = 1 and not 0.
row.addView(tv);
}
tableLayout.addView(row);
linearLayout.addView(tableLayout);
In the above code snippet, what does this do and what/why are TableRow.Layout params being added to a text view layout param?
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams are being used because tv is a child of row which is a TableRow.
If you look at the Android API reference, it says
Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged.
And the two parameters for the LayoutParams constructor are used to set the width and height of tv. Since both width and height is set to WRAP_CONTENT, the size of tv will be big enough to enclose its content (plus padding).
The TextView's parent is a TableRow, therefore the TextViews layout params should be of type TableRow.LayoutParams.
This is the exact same as specifying:
<TableRow ...>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
If the TextView was added to some other ViewGroup like a LinearLayout then it would need LinearLayout.LayoutParams set.

Custom view and change the size of the child views

I have a custom view that extends from FrameLayout. I'd like one of the children to have a size equal to the 20% of the total size as shown in the image.
How can I achieve the imageview to always have this size even if the custom view changes its size in runtime?
Lets say you have your view:
FrameLayout view = (FrameLayout)findViewById(R.id.customView);
Then you can get the layoutParams of the view calculate 20% of it, and then create a new view which will be 20% of the view.
For example:
ImageView img = new ImageView(this);
int imgSize = view.getLayoutParams().width * 0.2; //20 % of the width
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(imgSize, imgSize);
//params.gravity = Gravity.TOP;//How to set gravity programmatically
img.setLayoutParams(params);
//Set img attirubutes like src etc.
view.addView(img)
Also because you are using a FrameLayout it might be better to create an instance of a LayoutParam first and the set it in img.setLayoutParams(params), because iam sure you want to change the layout_gravity of the different views, which you can do in the params vairable

ImageView disappears after LayoutParams Change

I added a ImageView to my LinearLayout in the onCreate method programmatically and it worked as it should.
img = new ImageView(this);
img.setImageResource(R.drawable.source);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 50, 0, 0);
img.setLayoutParams(params);
layout = (LinearLayout) findViewById(R.id.index_view);
layout.addView(img);
And the I want to change it's x and y coordinates when the user touches the screen.
onTouch method - case ACTION_MOVE:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(newX, newY, 0, 0);
img.setLayoutParams(params);
the newX and newY are calculated based on the users touch-coordinates
But this code doesn't seem to work correctly.
I created toasts to show me the Image width after every touch and it showed me that the width is set to zero after first touch. This tells me: ImageView disappeared.
Any guesses?
I am assuming that you made sure that you are getting newX, newY values in onTouch().
I feel that your aim is to edit the Layout parameters but instead of doing that what you are doing is, you are assigning a new set of parameters with width and height as LayoutParams.WRAP_CONTENT and then setting the margins.
You might try to first get the existing parameters as follows:
LinearLayout.LayoutParams params =
( LinearLayout.LayoutParams) img.getLayoutParams();
Then change the margin:
params.setMargins(newX, newY, 0, 0);
img.setLayoutParams(params);
Try it and notify if it helps. If not then we can talk further.

How to fix my TableLayout and TableRow into specific positions dynamically?

I'm having problems setting up my TableRow with some TextViews in TableLayout dynamically. I have two pictures, one is my current situation shown, and the other is my mockup, expected situation (the goal which I need to achieve). I do not have any XML layouts; All of these are created programmatically, which is also something I need to achieve.
The snapshots shown are for a High Score screen, where I get a list of players with high scores, and display them altogether in a TableLayout. I'm just having trouble with the row/column positions.
Here's my code:
public void onResume() {
super.onResume();
//=============================================
TextView number = new TextView(this);
number.setText("1");
TextView place = new TextView(this);
place.setText("4th");
TextView testScore = new TextView(this);
testScore.setText("113489");
table = new TableLayout(this);
//rows = new Stack<TableRow>();
TableRow row = new TableRow(this);
row.addView(number);
row.addView(place);
row.addView(testScore);
table.addView(row);
this.setContentView(table);
}
If anyone knows how I should change my code from the Current Situation to the mockup Expected Situation, I gladly appreciate it. If I'm doing something wrong, please post a comment. Thanks in advance.
Set the layout weight of each textview to 1 and the layout width to 0dp. That will cause them to split the available space equally. You could use layout gravity left, center and right as appropriate to further ensure maximum separation.
I just found a good link about TableLayout and TableRow, creating specific positions dynamically. Link is here. Of course, the code here is just the barebones of it, used to demostrate a simple position layout.
Here's my code and the given picture of the result:
public void onResume() {
super.onResume();
//=============================================
dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 1, this.getResources().getDisplayMetrics());
if (dip <= 0)
dip = 1;
TextView number = new TextView(this);
TextView place = new TextView(this);
TextView testScore = new TextView(this);
number.setText("1");
place.setText("4th");
testScore.setText("113489");
number.setWidth(50 * dip);
place.setWidth(75 * dip);
testScore.setWidth(150 * dip);
number.setPadding(20*dip, 0, 0, 0);
table = new TableLayout(this);
TableRow row = new TableRow(this);
row.addView(number);
row.addView(place);
row.addView(testScore);
table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
this.setContentView(table);
}
I'm actually astonished that there's a method for obtaining the DIP unit. Should've searched for that in the first place. :P

Categories