I need to set margin to dynamically created UI. I want to add margin to LinearLayout.
Below is my code
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
viewPager = (ViewPager) container;
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
this.layoutInflater = inflater;
scrollView = new ScrollView(getActivity());
linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
//adding few UI controllers dynamically by method call to here
return scrollView;
}
I tried many ways but nothing works. Currently, it is not adding space/margin as per given dimensions.
if(layoutParams instanceof MarginLayoutParams)
{
((MarginLayoutParams) layoutParams).topMargin = 15;
((MarginLayoutParams) layoutParams).leftMargin = 15;
//... etc
}
As per this SO answer:
scrollView.setFillViewport(true);
This forces the childview to stretch to the parent scrollview. Then you can set the margins that will add space after filling the scrollview.
If you want to add margins to LinearLayout, you need to create LayoutParams of the same type of the parent, so:
scrollView = new ScrollView(getContext());
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// I recommend you to set resolved size as margins, not pixels like you are doing here.
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
EDIT
Remember to add the ScrollView to your layout adding LayoutParams to it.
For example in a Fragment (you are using getActivity() so i suppose you are in a Fragment):
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return scrollView;
}
you need to call scrollView.setLayoutParams(layoutParams);
also set Layoutparams to your Scrollview and Inner LinearLayout
in your code :
scrollView = new ScrollView(getActivity());
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
scrollView.setFillViewport(true);
linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) scrollView
.getLayoutParams();
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
Related
Here is my code to display button at bottom of scroll view programmatically.but only scrollview is displaying.button is not displaying on the screen.how to fix this.
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
ll.setLayoutParams(lp);
ScrollView scrollView = new ScrollView(this);
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
scrollView.addView(resultLayout);
ll.addView(scrollView);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
//ll.addView(rl1);
ll.addView(btn);
setContentView(ll);
screen of scroll view which contains
Try this approach... Change your ll (Linear Layout) to relative layout then put the properties for button to attach at layout bottom and and for scroll view to attach above button....hope you get my point....
Change your current layout LinearLayout with RelativeLayout and use this params. it will work
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Just Add the Footer to the Scroll View and then get the button as you wish.
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
Then Add the view to your Scroll View.
ll.addFooterView(btn);
I have created an application with four tabs loaded with specific webviews. I planned to add advertisement below the tabs. In my code I have set the content view as the ViewGroup created as TabHost. If I am add this Viewgroup to linearlayout the application have been crashed due to the TabHost.add(TabSpec) gets NullPointer exception. Here is the code.
public View addTabBarView(Context context)
{
m_vForm = _createTABForm(context);
return m_vForm;
}
private ViewGroup _createTABForm(Context context) {
sTabHost = new TabHost(context,null);
sTabHost.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
HorizontalScrollView sScrollView = new HorizontalScrollView(context);
LinearLayout.LayoutParams sScrollViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
sScrollView.setVerticalScrollBarEnabled(false);
sScrollView.setHorizontalScrollBarEnabled(false);
sScrollView.setScrollBarStyle(TRIM_MEMORY_UI_HIDDEN);
sScrollView.setFillViewport(true);
TabWidget tabWidget = new TabWidget(context);
LinearLayout.LayoutParams sTabWidgetParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tabWidget.setId(android.R.id.tabs);
sScrollView.addView(tabWidget, sTabWidgetParams);
sTabHost.addView(sScrollView, sScrollViewParams);
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setId(android.R.id.tabcontent);
final float scale = context.getResources().getDisplayMetrics().density;
int paddingtop = (int) (64 * scale + 0.5f);
frameLayout.setPadding(0, paddingtop, 0, 0);
sTabHost.addView(frameLayout, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
sTabHost.setup();
return sTabHost;
}
public addTabItem(final String url, String tabTitle, Drawable tabIcon)
{
TabSpec ts1 = sTabHost.newTabSpec(tabTitle);
if(tabIcon==null)
ts1.setIndicator(tabTitle);
else
ts1.setIndicator(tabTitle,tabIcon);
ts1.setContent(new TabHost.TabContentFactory(){
#SuppressWarnings("deprecation")
public View createTabContent(String tag)
{
//Creating webview inside a layout
}
});
sTabHost.addTab(ts1); //Here throws NullPointer exception.
sTabHost.setOnTabChangedListener(this);
}
How can I achieve my requirement.?
Are you not using an XML layout? It's much more efficient and tidy than what you have above...
You haven't included any logcat so there isnt much to go on but it seems to me that the issue is your addTabItem tries to make something arbitrary and use it as the content when really you want to be using the frameLayout as the content so try passing that into the method and setting it as the content
I have relativelayout as container, there i have set text and image, next i addView() this relativelayout twice to one linear layout, everything is fine but i cant separate the two relativelayouts they are alWays stick one to another, here is the code:
LinearLayout
layout = new LinearLayout(context);
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setMargins(5, 5, 5, 5);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setPadding(5, 5, 5, 5);
layout.setLayoutParams(ll);
layout.setId(i);
layout.addView(m1.box);
layout.addView(m2.box);
where m1 and m2 are:
RelativeLayout:
box = new RelativeLayout(context);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rl.setMargins(5, 5, 5, 5);
rl.width = 125;
rl.height = 125;
box.setId(i);
box.setPadding(5, 5, 5, 5);
box.setBackgroundColor(color1);
box.setLayoutParams(rl);
//add some text and image
How to separate m1 from m2? Now they look like OO i need to add space O_O.
First, this doesn't work because the parent layout is a LinearLayout, and the LayoutParams you defined are of RelativeLayout. the LayoutParams should match the type of the parent and not the child, because they tell the parent how and where to place it's children.
Second, use addView(View v, LayoutParams lp) instead of addView(View v) to prevent this kind of mistakes.
I have recently coded my button to be aligned to the right of the screen and it seems to not want to work.
I have also checked to see if my parent layout is set to cover the whole width of the screen and it is.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Vertical Layout - For the layout of the newSheetLayout.
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newSheetButtonLayoutH.addView(saveButton);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
If it is something simple and I'm very blind to the solution, I will punish myself accordingly.
Thank you for your help.
try
saveButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
if you are using xml and your need is not to do it in dynamic way then do
android:layout_gravity="right"
Edit:
i think this is what you want
LinearLayout newSheetLayoutV,dataShowV,dataShowH,newSheetButtonLayoutH,newlayout;
Button nextButton,backButton,saveButton;
//RelativeLayout ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
newlayout = new LinearLayout(this);
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setGravity(Gravity.RIGHT);
newlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//newSheetButtonLayoutH = new RelativeLayout(this);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
//nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
//backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
//saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newlayout.addView(saveButton);
newSheetButtonLayoutH.addView(newlayout);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
Try giving RelativeLayout params
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.ALIGN_RIGHT);
rel_lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
btn.setLayoutParams(rel_lp);
Try setting the layout_gravity of LinearLayout instead of your Button.
I'm having trouble finding exactly the syntax I need to use to set the paramters on child views of a relative layout. I have a root relative layout that I want to set 2 child textviews next to each other like this
---------- ---------
| Second | | First |
---------- ---------
So I have
public class RL extends RelativeLayout{
public RL(context){
TextView first = new TextView(this);
TextView second = new TextView(this);
first.setText('First');
first.setId(1);
second.setText('Second');
second.setId(2);
addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.ALLIGN_PARENT_RIGHT ???);
addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.ALLIGN_RIGHT_OF(first.getId()) ???);
}
}
How do I set the relative alignments?
public class RL extends RelativeLayout {
public RL(Context context) {
super(context);
TextView first = new TextView(context);
TextView second = new TextView(context);
first.setText("First");
first.setId(1);
second.setText("Second");
second.setId(2);
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(second, lpSecond);
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
addView(first, lpFirst);
}
}
You only need ALIGN_PARENT_RIGHT if you want the right edge of the view to line up with the right edge of its parent. In this case, it would push the 'first' view off the side of the visible area!
Falmarri, you'll need to use the 'addRule(int)' method.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RIGHT_OF, first.getId());
The full list of constants that can be used for addRule can be found here:
http://developer.android.com/reference/android/widget/RelativeLayout.html
And here is the addRule method reference:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,%20int)