I need to set TextView dynamically, I have a word, and I want to create as TextView as there are letter in the word. So I put the first TextView and then each TextView on the right of the last one created. This is my code :
letters = new TextView[Anagrame.length()];
for (int i = 0; i < Anagrame.length(); i++)
{
TextView letter = new TextView(getApplicationContext());
letter.setBackgroundResource(R.drawable.square);
letter.setText(String.valueOf(Anagrame.charAt(i)));
letter.setGravity(Gravity.CENTER);
letter.setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
if (i > 0)
{
Log.d("I ", Integer.toString(i));
Log.d("ID", Integer.toString(letters[i - 1].getId()));
params.addRule(RelativeLayout.RIGHT_OF, letters[i - 1].getId());
}
letter.setLayoutParams(params);
letters[i] = letter;
anagrameLayout.addView(letter);
}
Everything is fine except one thing the last TextView is not on the Right Position explanation :
The last TextView is on the same place than the first TextView, but when I checked in loop everything look fine, when I check log, the counter "i" and the id is good so I don't see the reason why the last one is not on the right of previous one but on the same place of the first one.
So if anyone have an idea, thanks in advance.
Related
I noticed, that a TextView with the same textSize can have a different height depending on the String it is displaying.
I disscovered it when paiting a simple TableLayout which has some TableRow items with some cells, each cell is a TextView.
Those cells doesn't have top margin, and they are being painted perfectly in Android 8.0 and upper devices, but in Android 7.0 and lower versions, some of the cells are adding a top margin between them, and this is because the height of the TextView is different. I can't understand how is that happening, it's very frustrating.
In this case, it's happening in the TextView called player and only when the name is too much long and it's using ellipsize to truncate it.
The code is very simple:
public void populateGoalscorersTable(ArrayList<Player> players){
goalscorersTableLayout.removeAllViews();
for (int i=0; i<players.size() && i<30; i++){
TableRow row = new TableRow(this);
row.setLayoutParams(goalscorersRowParams);
TextView position = new TextView(this);
position.setGravity(Gravity.CENTER);
position.setLayoutParams(goalscorersPositionParams);
position.setText(""+(i+1));
position.setBackgroundResource(R.drawable.table_cell_background);
position.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
row.addView(position);
TextView player = new TextView(this);
player.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
player.setLayoutParams(goalscorersPlayerParams);
player.setText(players.get(i).getName());
player.setBackgroundResource(R.drawable.table_cell_background);
player.setMaxLines(1);
player.setEllipsize(TextUtils.TruncateAt.END);
player.setPadding((int) getResources().getDimension(R.dimen.spacing_small),0,(int) getResources().getDimension(R.dimen.spacing_small),0);
player.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
row.addView(player);
TextView team = new TextView(this);
team.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
team.setLayoutParams(goalscorersTeamParams);
team.setText(players.get(i).getTeam().getName());
team.setBackgroundResource(R.drawable.table_cell_background);
team.setMaxLines(1);
team.setEllipsize(TextUtils.TruncateAt.END);
team.setPadding((int) getResources().getDimension(R.dimen.spacing_small),0, (int) getResources().getDimension(R.dimen.spacing_small),0);
team.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
row.addView(team);
TextView goals = new TextView(this);
goals.setGravity(Gravity.CENTER);
goals.setLayoutParams(goalscorersPositionParams);
goals.setText(""+players.get(i).getLeagueGoals());
goals.setBackgroundResource(R.drawable.table_cell_background);
goals.setMinHeight((int) getResources().getDimension(R.dimen.cell_height));
row.addView(goals);
if (players.get(i).getTeam() == Manager.getInstance().getTeam()){
player.setTextColor(getResources().getColor(R.color.player_team));
player.setTypeface(null, Typeface.BOLD);
team.setTextColor(getResources().getColor(R.color.player_team));
team.setTypeface(null, Typeface.BOLD);
}
goalscorersTableLayout.addView(row);
}
}
It's not the only way to make this errorneal top margin appearing (caused by a incorrect text height), I can see it for example when i have a row whith TextView cells with only one character and others with more than one character. The TextViews with more characters get's the margin. Also i can see this error happening when some TextViews are aligned to left and others centered. Very frustrating.
How can this be avoided?
If the font is not monospaced, different text can have different sizes and needs of space for letters that venture down the line such as j or p or q.
I want to place Check-boxes next to each other dynamically in android and when the width is over then the check boxes need to be aligned from the next line.
I have number of check-boxes which are initializing in an array. I have used the following code. It correctly place only one check-box. Other one gets placed in between. I have used the following code.
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout_symptoms_checkboxes);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);;
params.setMargins(0, 10, 0, 0);
for(int i = 0; i < arraySymptoms.length; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(arraySymptoms[i]);
cb.setId(i);
if(i==0)
{
cb.setLayoutParams(params);
layout.addView(cb);
}
else
{
params.addRule(RelativeLayout.ALIGN_RIGHT, i-1);
cb.setLayoutParams(params);
layout.addView(cb);
}
}
Please guide me through this. Your help will be greatly appreciated. Thank you in advance people :)
Just use LinearLayout with horizontal orientation instead of RelativeLayout
I tried to use below code, but the textviews do not change line after it reach the end of screen.
RelativeLayout ll = (RelativeLayout)findViewById(R.id.viewObj);
int lastid=0;
for (int i=0; i<100; i++) {
String teststr = " hello ";
TextView textView2 = new TextView(this);
textView2.setId(i+1);
textView2.setTextSize(30);
textView2.setBackgroundColor(Color.GREEN);
textView2.setTextColor(Color.RED);
textView2.setTypeface(Typeface.MONOSPACE);
textView2.setText(teststr+String.valueOf(i));
if (i>0)
{
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.RIGHT_OF, lastid);
ll.addView(textView2, lay);
} else {
ll.addView(textView2);
};
lastid = textView2.getId();
}
However, I dont know how to determine when to change line. I just keep on putting new textview to the right of the last one created. Can anyone tell me the correct logic to do the task? many thanks.
Easy fix. Switch to a dynamically-created LinearLayout, and set its orientation to vertical:
lay.setOrientation(LinearLayout.VERTICAL);
I have created two imageViews promatically as shown below:
public void createImageViews(Integer count){
ImageView[] imageViewArray = new ImageView[count];
for (int i = 0; i < count; i++ ) {
imageViewArray[i] = new ImageView(getBaseContext());
imageViewArray[i].setId(i); // unique property for every imageView
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageViewArray[i].setLayoutParams(params);
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i]);
Log.v("first", "first"+i);
}
else if(i < 3){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i],params);
Log.v("second", "second"+i);
}
}
I just need to place the second imageView toRightOf first imageView. Can someone help me. This is eating away a lot of my time.
try https://stackoverflow.com/a/5191159/1436931
you are using wrong index values.
at line
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
you aligning current image RIGHT of current image. :)
you need to track the index of last imageView id i.e left Image view
you need something like this
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());
First off I am new to android development and some seemingly easy tasks are frustrating me to no end. This is a follow on question to what was answered here: Changing the checkbox size in Android 2.0
The gist of it is that for some reason there is this extra space that keeps showing up next to my checkboxes (See Below).
Now I have tried to set the width to 1 and it doesn't get any shorter than what is shown. I have tried to set the width bigger and I can make the size bigger, but no shorter.
Please take a look at the code below and let me know what you see.
TableLayout table = (TableLayout) findViewById(R.id.timeEntriesTable);
for (int i = 0; i < timeEntries.getLength(); i++)
{
Element element = (Element) timeEntries.item(i);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
CheckBox box = new CheckBox(this);
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
box.setPadding(0,0,0,0);
box.setWidth(1);
box.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
if (box.isChecked())
{
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_on_background));
}
else
{
box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
}
}
});
TextView dateBox = new TextView(this);
String dateText = element.getAttribute("date");
dateBox.setText(dateText);
dateBox.setTextColor(Color.BLACK);
TextView customerBox = new TextView(this);
String customerName = element.getAttribute("customer");
customerBox.setText(customerName);
customerBox.setTextColor(Color.BLACK);
TextView hoursBox = new TextView(this);
String hours = element.getAttribute("hours");
hoursBox.setText(hours);
hoursBox.setTextColor(Color.BLACK);
TextView test = new TextView(this);
test.setTextColor(Color.RED);
test.setText("Test");
tr.addView(box);
tr.addView(dateBox);
tr.addView(customerBox);
tr.addView(hoursBox);
table.addView(tr, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
Thank you in advance!
That looks suspiciously like the "Time Entries" header is in its own table cell in a header row and the column of checkboxes below it is matching the same width. (See how the right edge of the text lines up perfectly with the date below it?) Since the checkbox's layout_gravity is likely defaulting to TOP|LEFT, it's being positioned in the upper left of the cell.
Try altering either the header row or the layout_gravity of the checkboxes.
So after the input of the two other answerers #adamp and #Phobos. I stumbled across what worked for me.
How to set (Text)View attributes programmatically?
By adding this:
TableRow.LayoutParams lp = new TableRow.LayoutParams();
lp.width = 15;
lp.setMargins(3, 5, 0, 0);
lp.height = 15;
tr.addView(box, lp);
I was able to tweak that cell exactly how I wanted it. It was very strange to me that it wasn't growing the column of the cell it was growing the checkbox itself. This is because in Android there are no actual cells, it uses the views and their sizes to generate the table apparently.
I have came across no tutorials or anything that has you set the LayoutParams programmatically that way. Thank you for your help troubleshooting this!
Table Layouts naturally want to make all the cells the same size. If you want one, or more, to be different sizes then that takes additional parameters.
To shrink all the extra space in your cells add this to your table object
table.setShrinkAllColumns(true);
Or to just shrink the first column where your checkbox is
table.setColumnShrinkable(int columnIndex, boolean isShrinkable)