I am adding TextViews programatically to a LinearLayout. But when the second TextView is added, it seems to replace the first.
Here is the code:
LinearLayout l = (LinearLayout) findViewById(R.id.contacts_container);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String username = object.getString("username");
String status = object.getString("status");
// create wrapper
LinearLayout wrapper = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
wrapper.setPadding(padding,padding,padding,padding);
wrapper.setLayoutParams(lp);
l.addView(wrapper);
// add Imageview to wrapper
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(R.drawable.icon_only_dark_crop);
lp = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
lp.setMargins(0, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0);
image.setLayoutParams(lp);
wrapper.addView(image);
// add linearLayout text wrapper to main wrapper
LinearLayout textWrapper = new LinearLayout(getApplicationContext());
textWrapper.setOrientation(LinearLayout.VERTICAL);
textWrapper.setPadding(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()), 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 0.9f);
textWrapper.setLayoutParams(params);
wrapper.addView(textWrapper);
// add username TextView to textWrapper
TextView usernameText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setText(username);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(usernameText);
// add status TextView to textWrapper
TextView statusText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setText(status);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(statusText);
}
It loops twice and two "wrapper" LinearLayouts get added to the main LinearLayout fine. But for each wrapper LinearLayout it should add two TextViews, but when I run the application, only the statusView displays. If I remove the statusView, the usernameView displays fine.
Why is that when the statusView is added to the wrapper, the usernameView seems to be hidden or removed?
You copy pasted the same layout variable name when initializing the second textview
use this instead
// add username TextView to textWrapper
TextView usernameText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setText(username);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(usernameText);`
// add status TextView to textWrapper
TextView statusText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
statusText.setLayoutParams(lp);
statusText.setText(status);
statusText.setTextColor(Color.parseColor("#FFFFFF"));
statusText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(statusText);
in your second textview, check the below text.
TextView **statusText** = new TextView(getApplicationContext());
**usernameText**.setLayoutParams(lp);
**usernameText**.setText(status);
**usernameText**.setTextColor(Color.parseColor("#FFFFFF"));
**usernameText**.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(statusText);
Probably because your textWrapper does'nt have a orientation specified. Define it as vertical or horizontal depending on your needs and it should work fine.
Related
I'm trying to use the setMargins method using ViewGroup but I keep getting the error that setMargins is not defined .This is my code:
LinearLayout content = new LinearLayout(activity);
LayoutParams params = content.getLayoutParams();
params.setMargins(0, 0, 0, 40);
content.setLayoutParams(params);
You should use LayoutParams to set your button margins:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
Depending on what layout you're using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams.
And to convert your dp measure to pixel, try this:
Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
yourdpmeasure,
r.getDisplayMetrics()
);
Use this method to setMargins depend on any view (LinearLayout, RelativeLayout etc.)
Pass the params in integer format
public static void setMargins(View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins(left, top, right, bottom);
view.requestLayout();
}
}
I'm trying to make a list of database items like the following..
for (int i=0; i < jArray.length(); i++)
{
JSONObject row = jArray.getJSONObject(i);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,100));
Button btn = new Button(MainActivity.this);
//btn.setText(row.getString("subject"));
btn.setText(String.valueOf(i));
btn.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.8));
File imgFile = new File(file_path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageBitmap(myBitmap);
iv.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2));
ll.addView(btn);
ll.addView(iv);
LinearLayout sv = findViewById(R.id.sv_layout);
sv.addView(ll);
}
The result is like this:
But I want to have space between Linear Layouts and make Image height shorter to be equal with button height
How can I achieve this? Thank you,
Please Try this code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(0, 5, 5, 10);
ll.setLayoutParams(params);
you can add your specific margins in this line params.setMargins(0, 5, 5, 10);
Take button and image in linear layout with horizontal orientation. Set Button and Imageview height to wrap content. Now for gap between Linear Layouts give appropriate margin.
Set the margins in the layout params and pass them to your ImageView object.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2);
int dp = getDps(8);
layoutParams.setMargins(0,dp,0,dp);
iv.setLayoutParams(layoutParams);
Function getDps():
public int getDps(int sizeInDp){
float scale = getResources().getDisplayMetrics().density;
return (int) (sizeInDp*scale + 0.5f);
}
This looks like:
I'm trying to add some EditTexts to a TableRow programmatially, yet they don't show up on the screen. If I add the EditTexts directly to the TableLayout it works fine but I do want to have them in one row afterall...
public void createPlayers(){
EditText[] editTextSpieler = new EditText[anzahl_Spieler];
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableLayout.addView(tableRow);
for(int i = 0; i < anzahl_Spieler; i++){
editTextSpieler[i] = new EditText(getActivity());
editTextSpieler[i].setLayoutParams(layoutparams);
tableRow.addView(editTextSpieler[i]);
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
}
}
Thanks in advance.
Solution:
layoutparams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
specifying MATCH_PARENT and WRAP_CONTENT with TableRow.LayoutParams did the job
Can you try this change.
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
//Move to last line in the loop.
tableRow.addView(editTextSpieler[i]);
I am trying to building a linear layout programmatically, in which, there are 4 buttons in all, each of the button simply positioned below the previous button. Just like the picture shows below:
And as you can see from the picture above, each button has exactly the same size but they have different layout_margins, the first button has a larger value in layout_marginTop while the other 3 buttons has the same value in layout_marginTop.
Basically to build a layout like this by xml is very simple but now I have really come across difficulties in building all this only through java code. I have gone through many posts and now I can easily add 4 buttons in proper sizes but I just could not find a proper way to programmatically set the layout_margin for each of the button.
To simple add four buttons, I could do like this:
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4 )));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
setContentView(layout);
//setContentView(R.layout.main);
}
}
And to programmatically set margins for each button(view), I could do like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 0, 0);
button.setLayoutParams(params);
But here comes the problem: as long as I setLayoutParams for the 4 buttons, and then add 4 buttons into Linearlayout by "addView(button)", only the first button could be displayed in the proper size with the proper margins. All the other 3 buttons just disappeared. And With many tests I just found it seems that only one layoutParams could be allowed in a linearlayout object. As a result, as long as I set different layout params for different buttons, only the first button could be displayed. But since here the margin params for my 4 buttons are definitely different thus I think I have to use different layoutparams for different buttons.
So pls anyone tell me how could I programmatically set the margin for my each of my buttons and make them display properlly? This has already sucked my life for two days, pls help! :)
linear = (LinearLayout) rootView.findViewById(R.id.linear);
.
.
.
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(layoutParam);
layout.setOrientation(LinearLayout.VERTICAL);
// Below will add three linear layout with 4 buttons in each
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
//Here is important
row.setOrientation(LinearLayout.VERTICAL);
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(getActivity());
btnTag.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
linear.addView(layout);
// You can set also
// setContentView(layout)
I have the following code, which tries to set the margin:
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView name = new TextView(this);
name.setText(Html.fromHtml(venues.get(j).name + "<br><br>" + venues.get(j).getFullAddress()));
However this doesn't set the margin. What am I doing wrong here?
Edit. Whoops, my bad, TableLayout.LayoutParams is fine. Use TableRow.LayoutParams on the children of the TableRow.
I just ran this code (which is, for the most part, your code) and the margin sets correctly
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=100;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView tv = new TextView(this);
tv.setText("EXAMPLE ROW");
TableRow.LayoutParams textParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(textParams);
tr.addView(tv);
tlayout.addView(tr);
isn't it like so?
TableRow tr = new TableRow(Activity.this)
And I thik you'll nee a table for that row too.
Why not go for XML ?
<TableRow
android:id="#+id/tr1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>