I'm attempting to add margins to an EditText that is dynamically created, but for some reason when running the following code nothing happens.
public void addName() {
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
row = new TableRow(this);
name = new EditText(this);
name.setHeight((int) getResources().getDimension(R.dimen.textboxHeight));
name.setBackground(getResources().getDrawable(R.drawable.textbox));
counter++;
row.addView(name, layoutParams);
namesList.addView(row);
}
If I remove the second parameter layoutParams from row.addView() it adds the row (without margins), but I want the row to be added with a margin of 15 when dynamically created. Is this possible?
Related
In the code snippet below there's one commented line. When I uncomment that line, then the contents of LinearLayout are not displayed in a TableRow. Without setting the LayoutParams, the row displays both texts. I don't understand this behavior. I know that I can include complex views via xml files, but I'd rather understand what's wrong with this code:
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
TableRow tableRow = new TableRow(this );
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// when I comment out this line, the row only shows the second text.
// linearLayout.setLayoutParams(layoutParams);
TextView textLabel = new TextView(this);
textLabel.setText("inside linear layout");
linearLayout.addView(textLabel);
TextView message = new TextView(this);
message.setText( "inside tablerow");
tableRow.addView(linearLayout);
tableRow.addView(message);
tableLayout.addView(tableRow);
Assuming the question is something like "What's the issue of this? How to resolve this?", here's my answer:
When you are setting LayoutParams to a View, those params would be used by the parent of this View to layout that View appropriately. So in your case what you have done is following:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
Now, the tableRow is confused, because it expects TableRow.LayoutParams in order to layout the view appropriately, but it suddenly finds out some other layout params. Whereas, had you not explicitly specified params (i.e. when linearLayout.setLayoutParams() is commented out), the default layout params would be generated.
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(); // this is TableRow.LayoutParams
}
So, instead of creating LinearLayout.LayoutParams, create TableRow.LayoutParams:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
For creating a structure like the image in android, I tried the following code.
But showing this error. Please help
My code looks like
public void setValues(){
LinearLayout table=(LinearLayout)findViewById(R.id.mainWrap);
LinearLayout row=new LinearLayout(this);;
for(int i=0;i<5;i++){
if(i%2==0){
row= new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
}
FrameLayout layout = new FrameLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((width/2)-10,(height/2)-10);
params.setMargins(5, 5, 5, 5);
layout.setLayoutParams(params);
if(i%2==0){
layout.setBackgroundColor(Color.parseColor("#CACACA"));
}
else {
layout.setBackgroundColor(Color.parseColor("#333333"));
}
ProgressBar pr=new ProgressBar(this);
FrameLayout.LayoutParams params_p = new FrameLayout.LayoutParams((width/6),(width/6));
params_p.gravity=Gravity.CENTER;
pr.setLayoutParams(params_p);
layout.addView(pr);
ImageView im=new ImageView(this);
FrameLayout.LayoutParams params_im = new FrameLayout.LayoutParams((width/2),(width/2));
im.setImageResource(R.drawable.man);
im.setLayoutParams(params_im);
layout.addView(im);
row.addView(layout);
table.addView(row);
}
Error is
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Thanks in advance
I have a feeling the problem is here:
LinearLayout row=new LinearLayout(this);;
for(int i=0;i<5;i++){
The first time it loops row will be replaced by your second assignment of it and held in the variable OUTSIDE of the loop, the second time it is not replaced and because it is outside of the loop, it is the same row you're trying to add to the table again. Just swap the order of the lines above.
Hi I was want to add 2 different text views to my linear layout but somehow when i try to add both of them only the first one appears why is this the case? Here is my code :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// create the text view for the main string to
// be displayed
TextView displayMainText = new TextView(this);
displayMainText.setTextSize(15);
displayMainText.setText(mainString);
displayMainText.setLayoutParams(layoutParams);
displayMainText.setPadding(0, 20, 0, 0);
// Create the text view for the optional string to
// be displayed
TextView displayOppText = new TextView(this);
displayOppText.setTextSize(15);
displayOppText.setText(optionalString);
displayOppText.setLayoutParams(layoutParams);
displayOppText.setPadding(0, 20, 0, 0);
// add text views to the layout
LinearLayout studyTLayout = (LinearLayout) findViewById(R.id.study_time_layout);
studyTLayout.addView(displayMainText);
studyTLayout.addView(displayOppText);
setContentView(studyTLayout);
With my code it only adds the first text view correctly
It seems like you didn't set the orientation of the LinearLayout to Vertical.
studyTLayout.setOrientation(LinearLayout.VERTICAL);
I am currently doing an android application that contains customize alert dialog. It contains a button , but i can't set the margin for the button . the code is given below. setmargin method is not working
AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);
button.setText("Send");
LayoutParams buttonLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(buttonLayoutParams);
resetPassword=editText.getText().toString();
LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);
myDialog.setView(layout);
Write below code to set margin, it may help you.
AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);
EditText editText = new EditText(Login.this);
TextView textView = new TextView(Login.this);
button.setText("Send");
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutParams.setMargins(50, 10, 0, 0);
button.setLayoutParams(buttonLayoutParams);
String resetPassword = editText.getText().toString();
LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);
myDialog.setView(layout);
myDialog.show();
Use LinearLayout.LayoutParams or RelativeLayout.LayoutParams according to parent layout of the child view
Just sharing a slightly different approach.
Instead of casting to LinearLayout.LayoutParams, RelativeLayout.LayoutParams, etc, you can just cast to MarginLayoutParams.
Casting to MarginLayoutParams is better because you can later update your layout and you don't need to return to your java code to change from LinearLayout to RelativeLayout or any other Layout type
You can do something like:
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
// Set bottom margin
layoutParams.bottomMargin = x;
// Set top margin
layoutParams.topMargin = x;
// Set left margin
// This won't have effect if you set any relative margin (start) previously or in the layout.xml
layoutParams.leftMargin = x;
// Set left margin
// This won't have effect if you set any relative margin (end) previously or in the layout.xml
layoutParams.rightMargin = x;
// Set start margin
layoutParams.setMarginStart(x);
// Set end margin
layoutParams.setMarginStart(x);
// Set all left, top, right, bottom margins at once
// Note that here, left and right margins are set (not start/end).
// So, if you have used start/end margin before (or via layout.xml),
// setting left/right here won't have any effect.
layoutParams.setMargins(left, top, end, bottom)
// Then re-apply the layout params again to force the view to be re-draw
// This step may not be necessary because depending where you set the margin,
// view is already scheduled to be drawn
// For any case, to ensure the view will apply the new margin, call:
view.setLayoutParams(layoutParams);
buttonLayoutParams.bottomMargin
buttonLayoutParams.topMargin
buttonLayoutParams.leftMargin
buttonLayoutParams.rightMargin
can be used to set margins
The setMargin() method is available if you're using LinearLayout.LayoutParams but not if you're using ViewGroup.LayoutParams. Dipak Keshariya alludes to this but doesn't say it in so many words.
You can set in LinearLayout margin
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);
This works for me (The support library (need to use AndroidX):
Kotlin
val params = LinearLayoutCompat.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
setMargins(0,16,0,16)
}
Java
LinearLayoutCompat.LayoutParams params = LinearLayoutCompat.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
params.setMargins(0,16,0,16)
I created a custom view. In it, theres a line, a textview, another line. beneath the bottom line, i wanted to put a new horizontally oriented linearlayout. when i run it, this nested linearlayout doesnt seem to show up at all. Instead, i can see the test button right underneath the bottom line. what am i doing wrong?
public class MyView extends LinearLayout {
public MyView(Context context, Question question) {
super(context);
// this.setLayoutParams(params);
this.setOrientation(VERTICAL);
this.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 2);
View topLine = new View(context);
lineParams.setMargins(0, 15, 0, 0);
topLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
topLine.setLayoutParams(lineParams);
this.addView(topLine);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//Challenge Question
TextView questionText = new TextView(context);
questionText.setTextColor(Color.BLACK);
questionText.setTextSize(14);
questionText.setLayoutParams(params);
questionText.setGravity(Gravity.CENTER_HORIZONTAL);
questionText.setText(question.getQuestion());
this.addView(questionText);
View bottomLine = new View(context);
bottomLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
bottomLine.setLayoutParams(lineParams);
this.addView(bottomLine);
LinearLayout innerLayout = new LinearLayout(context);
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
innerLayout.setLayoutParams(innerLayoutParams);
innerLayout.setBackgroundColor(Color.RED);
innerLayout.setOrientation(HORIZONTAL);
//TableLayout for the multiple choices
TableLayout tableLayout = new TableLayout(context);
LayoutParams tableLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// tableLayoutParams.weight = .8f;
tableLayout.setBackgroundColor(Color.RED);
tableLayout.setLayoutParams(tableLayoutParams);
innerLayout.addView(tableLayout);
this.addView(innerLayout);
Button button = new Button(context);
button.setLayoutParams(params);
button.setText("testing 123");
this.addView(button);
}
Note that I pasted the code without all the stuff that I added to the tablelayout. I probably should have pasted that too. But it didn't work when I did that either. but either way, if i set the nested linearlayout to 300 width and set a background color of red to it, i should at least see it, no?
Think about what the height of the inner layout should be. Right now it is wrap_content and contains a TableLayout (with no rows) with its height also set to wrap_content. There doesn't seem to be anything in that inner layout giving it a height dimension, so that may be why it is not being displayed.
Trying the following will make your layout visible:
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, 300);
More usefully, you can try adding something with a real width/height to the TableLayout.
Also consider writing your layout in XML to better separate your application logic and the presentation.