How to create a LinearLayout with wrap_cotent in java code? - java

What is the java programmically equivalent to create the following XML file:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout />
</LinearLayout>
I try doing this, but some how the LinearLayout scretch to MATCH_PARENT
// child is the RelativeLayout in the above example:
LinearLayout parent = new LinearLayout(getContext());
LinearLayout.LayoutParams childLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.addView(child, -1, childLayoutParams);

Try this
LinearLayout parent = new LinearLayout(getContext());
LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.setLayoutParams(parentLayoutParams);
And then add this to the screen
outerlayout.addView(parent);

The LayoutParams you are setting are for the RelativeLayout, not for the LinearLayout. The LinearLayout is using MATCH_PARENT as default parameters, I would bet. When you add parent to the Window or root ViewGroup, you should create a proper set of LayoutParams and specify WRAP_CONTENT.

Related

Android TextView doesn't align to RIGHT in RelativeLayout

I have a problem trying to align a view to RIGHT inside a relative layout programmatically:
Here is the xml(i don't ant to change the main layout, i want it linear layout because i'am simulating a problem):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.upf.ctrl_tp2.Main2Activity"
android:orientation="vertical">
</LinearLayout>
Here is the mainActivity.java code:
public class Main2Activity extends AppCompatActivity {
LinearLayout main;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
main = (LinearLayout) findViewById(R.id.activity_main2);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setLayoutParams(laypar);
r.addView(txt);
main.addView(r);
}}
The Result i got:
What i wanted:
The align right will not work inside a linear layout.
The layout_gravity will work.
Replace
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
with this
laypar.gravity = Gravity.RIGHT;
Set the LayoutParam to the TextView.
txt.setLayoutParams(laypar);
You can also use android:layout_alignParentRight="truein xml.
Yes MR. #CrazyDeveloper all thing looking good only one thing you have to remark .
RelativeLayout r = new RelativeLayout(this);
// actul issue belove .
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
just change it .
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Width is MATCH_PARENT instead of WRAP_CONTENT
try this :
main = (LinearLayout) findViewById(R.id.ll_layout);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setGravity(Gravity.RIGHT);
r.setLayoutParams(laypar);
r.addView(txt);
ll_layout.addView(r);

Android textField fill remaining height of parent

I'm really new to android and I'm trying to do very basic stuff ..
I would like to have a layout which contains 2 textFields
The first one should be 100 dp height
The second one should fill the remaining height of the layout
This is what I have done for now :
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
But I don't how to say that the second one should be juste after the first one and until the the bottom of the layout.
What should I do ?
Thanks
Don't use relative layout for such task. It's slower than LinearLayout and can be easily done with LinearLayout. Use layoutWeight, like in the example below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="100dp" android:layout_width="match_parent"
/>
<TextView
android:layout_height="0dp" android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
In order to set layout_weight from code, do the following:
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(100));
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(Color.BLUE);
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
setContentView(linearLayout);
}
First of all you should use linear layout for this task.But if you want to put it in RelativeLayout then the solution is below
1-You should set you second textView's height to match_parent
2-RelativeLayout.END_OF will align you layout to right side of textview1.So use RelativeLayout.BOTTOM
3-RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,100); will set your textview's heght to 100 pixels (Pixel size varies with devices).So set height in dp like this
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
TRY this-
//density of device
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
// RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
RelativeLayout.LayoutParams lyp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
lyp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
RelativeLayout.LayoutParams lyp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lyp2.addRule(RelativeLayout.BOTTOM, textView1.getId());
relativeLayout.addView(imageView1, lyp2);
mainLinearLayout.addView(relativeLayout);

Programmatically RelativeLayout.ALIGN_RIGHT with a given View.getId() not working

i try to align a TextView programmatically to the right/bottom edge of a button.
However, this static xml code is working:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView10"
android:layout_alignRight="#+id/button"
android:layout_alignBottom="#+id/button" />
</RelativeLayout>
If i try to do it by code, it is not working.
The TextView is getting aligned top/left as it is by default.
The RelativeLayout.ALIGN_RIGHT and ALIGN_BOTTOM seems to be ignored...
// Adding the LinearLayout
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
final LinearLayout.LayoutParams LLParamsCont = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, Utils.dpToPx(getContext(), 50));
// Adding the RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(getContext());
relativeLayout.setLayoutParams(LLParamsCont);
// Adding the Button
Button button = new Button(getContext());
button.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
relativeLayout.addView(button)
// Adding the TextView
TextView textView = new TextView(getContext());
textView.setGravity(Gravity.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
Utils.dpToPx(getContext(), 20),
Utils.dpToPx(getContext(), 20));
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, button.getId()); // <== THIS DOESN'T SEEM TO WORK
layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, button.getId()); // <== THIS DOESN'T SEEM TO WORK
textView.setLayoutParams(layoutParams);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundResource(R.drawable.score_count);
relativeLayout.addView(textView);
linearLayout.addView(relativeLayout)
The TextView is just not beeing aligned to the right/bottom line of the Button.
Does anyone has an idea why that is not working by code, but with static xml?
What did i miss?
Thank you!
With best regards,
Juergen
Try to set a unique id to the Button before create the rule (more info here).
Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!
I found the solution at this post:
How to lay out Views in RelativeLayout programmatically?
I need to set an id with setId(int) to the Button, then it is working.

android Layout_gravity (not gravity) and Layout_marginTop(not marginTop) programmatically

I need to programmatically create a Toast that use an ImageView and a TextView, and that appears in the middle of the screen, and I have done it.
THIS IS THE RENDER I WANT
(The black square with the heart is the ImageView image, and the "J'aime" is the TextView)
XML Code :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/toastImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:src="#drawable/like" />
<TextView
android:id="#+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
android:text="J'aime !"
android:textColor="#FFFFFF" />
</FrameLayout>
THIS IS THE RENDER I HAVE with JAVA code :
JAVA CODE :
FrameLayout toastLayout = new FrameLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
I have tried with a relativelayout or a linearlayout, but the framelayout works better for this.
So I have a question :
What is the JAVA equivalent of :
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
? Because this is visibly not :
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
Need help.
Try this:
RelativeLayout toastLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
RelativeLayout.LayoutParams toastTextLp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastTextLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
Try this...
LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsOfAnim.gravity = Gravity.center_horizontal;
paramsOfAnim.topMargin = 85;
Let's take that A contains B and B contains C. You set B.layoutParams for describing the placement of B in the A, not for placement of items inside B, such as C.
The other thing is that layout parameters are complex enough, it is difficult not to forget something, and you don't need really to set all parameters by hand. So, it is better to read them, change and set back.
As here.

How to write this xml in java code

Android XML:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_height="28dp" android:layout_width="180dp"
android:layout_toRightOf="#+id/signinemailtxt"
android:paddingLeft="50dp"
android:layout_marginTop="65dp"
android:background="#ffffff"
/>
Java:
layout= new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TextView Txt=new TextView(this);
Txt.setText("Name");
How to get layout_marginTop,layout_toRightOf options in java code
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
For layout_margin :
LayoutParams params = new LayoutParams(context, attrs);
params.setMargins(0, 5, 0, 5); //setMargins (int left, int top, int right, int bottom)
Txt.setLayoutParams(params);
Don't declare new instances of layout and text view, but get the ones you created in the xml:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.LayoutId);
TextView Txt = (TextView) findViewById(R.id.TextViewId);
Now you can edit the layout and textview you are seeing on the screen.

Categories