How can we set margins programatically for Relative-layout in android - android

Hi i am very begginer for android and i have added Relative layout programatically on my blank Activity and so for everything is ok
Here my main requirement is i would like to set margins for that Relative-layout at four sides for this i have tried some code but that's not working please help me
my code:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout ll = new RelativeLayout(this);
ll.setBackgroundColor(getResources().getColor(R.color.red));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(10, 10, 10, 10);
setContentView(ll);
}

Just call setLayoutParams() after setting your margins:
layoutParams.setMargins(10, 10, 10, 10);
ll.setLayoutParams(layoutParams);// here
setContentView(ll);
PS: In your case, it's better if you set your RelativeLayout from xml

Just in case that you need it in XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dip">
</RelativeLayout>
Or you could set them by one
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
And so on

Related

Android - Wrap buttons within view

I am trying to make buttons wrap in a LinearLayout in Android, but they are just continuing off to the right of the view (the word shown in the screenshot should be "HELLO", so I want the "O" to drop down to the next line).
I am adding the buttons programmatically, but even if I code them into the XML layout file, they still don't wrap. Here is the layout file with the LinearLayout container into which I am dynamically adding the buttons:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constrainedWidth="true"
tools:context=".LetterTileView">
<LinearLayout
android:id="#+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
And here is the code I am using to create and add the tile buttons:
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
Any advice would be appreciated. Thanks!
I ended up using FlexboxLayout, which works great for this. Thanks to those who offered suggestions!
First of all, there is no need to use a ConstraintLayout you can use your LinearLayout as the parent layout.
Then for the purpose of displaying all buttons in one line, you have to set weight for the LinearLayout in XML and set weight for the views you add to it.
The xml file should look like:
<LinearLayout
android:id="#+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="5"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
And in code you should set weight for each view you by adding ,1.0f to LayoutParam :
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1.0f );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}

layout_below and weightsum of LinearLayout Programmatically

I am creating LinearLayout in RelativeLayout programmatically. What I am trying to do is to assign the weigtsum in LinearLayout and want to set the layout_above attribute as well. The problem is weightSum is available in LinearLayout.LayoutParams and layout_above is available in RelativeLayout.LayoutParams. Currently,, I am doing
LinearLayout ll_menu_code = new LinearLayout(this);
ll_menu_code.setId(1001);
LinearLayout.LayoutParams parmas_ll_menu_code = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
parmas_ll_menu_code.setMargins(0, 20, 0, 0);
ll_menu_code.setLayoutParams(parmas_ll_menu_code);
Which sets the weightSum. How to set the layout_above? What should the way to set the both attributes?
I want to create the following xml layout programmatically
<LinearLayout
android:layout_below="#id/ll_menu_code"
android:id="#+id/ll_quantity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:weightSum="5">
If you want to add LinearLayout into RelativeLayout programmatically, then you must create instance of RelativeLayout.LayoutParams rather than LinearLayout.LayoutParams since LinearLayout is a child to Relativelayout
So your code must be
LinearLayout ll_menu_code = new LinearLayout(this);
ll_menu_code.setId(1001);
ll_menu_code.setWeightSum(2f);
RelativeLayout.LayoutParams parmas_ll_menu_code = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
parmas_ll_menu_code.setMargins(0, 20, 0, 0);
parmas_ll_menu_code.addRule(RelativeLayout.BELOW, 1001);
ll_menu_code.setLayoutParams(parmas_ll_menu_code);
I Hope it helps..

Android - Implement Views programmatically

I want to implement an imageview and a textview side by side. I achieved this by using XML. However i wanted to achieve this programmatically but had no luck so far. My XML and Java code are below. Please help me to execute programmatically.
I'm executing the Java code in a fragment.
XML CODE:
<RelativeLayout
android:id="#+id/rl1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<com.loopj.android.image.SmartImageView
android:id="#+id/my_image1"
android:layout_width="160dip"
android:layout_height="100dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/my_image1"
android:layout_alignTop="#+id/my_image1"
android:layout_toRightOf="#+id/my_image1"
android:gravity="center_vertical"
/>
</RelativeLayout>
JAVA CODE:
RelativeLayout rl1 = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlParams;
rlParams = new RelativeLayout.LayoutParams(
newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rlParams.addRule(LinearLayout.VERTICAL);
rl1.setLayoutParams(rlParams);
SmartImageView siv1 = new SmartImageView(getActivity());
siv1.setId(rand.nextInt(50000) + 1);
siv1.setLayoutParams(new LayoutParams(width,height));
siv1.setAdjustViewBounds(true);
siv1.setScaleType(ScaleType.FIT_XY);
siv1.setImageUrl(Uri);
TextView tv1 = new TextView(getActivity());
RelativeLayout.LayoutParams relativeLayoutParams;
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(relativeLayoutParams);
relativeLayoutParams.setMargins(10, 0, 0, 0);
tv1.setGravity(Gravity.CENTER_VERTICAL);
tv1.setText("Sample Text");
rl1.addView(siv1);
rl1.addView(tv1);
ll.addView(rl1);
By executing the above code, i'm getting the image but the text is inside the image. But, i want to get the image on the left and the text on the right.
Thanks in advance
Add below code to your Activity:
rlParams.addRule(RelativeLayout.ALIGN_TOP, siv1);
rlParams.addRule(RelativeLayout.ALIGN_BOTTOM, siv1);
rlParams.addRule(RelativeLayout.RIGHT_OF, siv1);
tv1.setLayoutParams(rlParams);
and then do:
rl1.addView(siv1);
rl1.addView(tv1);
Hope this helps.
In your case it is better to use a horizontal LinearLayout:
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(param);
TextView textview = new TextView(getActivity());
...
SmartImageView siv1 = new SmartImageView(getActivity());
...
ll.addView(textview);
ll.addView(siv1);
To align the views in RelativeLayout is much difficult. I will suggest you to use the TableRows or LinearLayouts. Both these give much easier way to align views side by side. Here is a source in which one TextView is aligned-left with 4 ImageViews aligned on its right. You can get idea from this
TextView taking/consuming too much space above and below the text
You can set the margin of your text like this
relativeLayoutParams.setMargins(200, 0, 0, 0);
but this is not the best practice so do one thing take linear layout set orientation to horizontal add both imageview and textview to it and then add this linearlayout to your relative layout.
Hope this helps.

position views programmatically in Android

I have a method that creates 3 textViews in a vertical LinearLayout:
public LinearLayout CreateLayout() {
LinearLayout aLap = new LinearLayout(this);
LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txtName = new TextView(this);
txtName.setText(result1);
txtName.setLayoutParams(TextParams);
txtName.setGravity(Gravity.CENTER);
txtName.setPadding(20, 8, 20, 4);
txtName.setTextSize(20);
txtName.setTextColor(Color.parseColor("#000000"));
TextView txtTime = new TextView(this);
txtTime.setText(result2);
txtTime.setLayoutParams(TextParams);
txtTime.setGravity(Gravity.CENTER);
txtTime.setTextSize(20);
txtTime.setPadding(20, 4, 10, 4);
txtTime.setTextColor(Color.parseColor("#000000"));
android.view.ViewGroup.LayoutParams Params = new ViewGroup.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, 150);
TextView rep = new TextView(this);
String Rep = sharedPrefs.getString("R_PREFS", "2");
rep.setLayoutParams(TextParams);
rep.setGravity(Gravity.CENTER);
rep.setTextSize(20);
rep.setPadding(20, 4, 10, 4);
rep.setTextColor(Color.parseColor("#000000"));
rep.setText("Test: " + Rep);
aLap.setLayoutParams(Params);
aLap.setBackgroundResource(R.drawable.bg);
aLap.setOrientation(LinearLayout.VERTICAL);
aLap.setPadding(3, 3, 3, 3);
aLap.addView(txtName);
aLap.addView(txtTime);
aLap.addView(rep);
return aLap;
}
I use a vertical layout since I want the textViews to be placed under each other. Now I want an ImageView to be placed to the right of this textViews and centered vertically. Is that possible?
You can not achieve desired output with a single LinearLayout. I would suggest you use RelativeLayout. It's more flexible. You can add more child-views to RelativeLayout at desired positions by adding rules to the views.
Check this post on how to add rules when working with Relative Layout at runtime: How to lay out Views in RelativeLayout programmatically?.
<LinearLayout orientation="horizontal">
<LinearLayout orientation="vertical">
<TextView view1/>
<TextView view1/>
<TextView view1/>
</LinearLayout>
<ImageView height=fill_parent scaleType="center"/>
</LinearLayout>
Is more or less what you want I think

Set position of an EditText and TextView in a RelativeLayout programatically

I have a RelativeLayout defined in main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
And add a TextView and EditText programatically in onCreate:
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
addContentView(customGlView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
addContentView(myTextView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(myEditText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
They both show up, but are overlapping in the top left corner. I've spend hours to figure out how to either position them just below each other, or one in the left corner of the screen and the other in the right corner. If I add them through the main.xml neither of them will show up. Can anyone give me a hint to the right direction?
For a RelativeLayout, you need to specify the relative positions of your elements, using the LayoutParams:
myTextView.setId(1);
myEditText.setId(2);
addContentView(myTextView, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, myTextView.getId());
addContentView(myEditText, params);
Add a rule to your LayoutParams which will set one textview below the other..
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("myTextView");
params1.addRule(RelativeLayout.BELOW, tv1.getId());
TextView tv2 = new TextView(this);
I couldn't solve this with RelativeLayout, but using LinearLayout does exactly what you want to do.
addView(myEditText);
Or you can use this method to add them to any position you want. For example, you can first add myEditText and then add myTextView above myEditText:
addView(myEditText);
addView(myTextView, 0);

Categories

Resources