How to write this xml in java code - java

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.

Related

How to add below layout programatically

I have below layout in xml. But I want to add few more similar rows(RelativeLayouts one below another) dynamically with same layout. I've tried the below code, but all the values are overlapping.
Also I don't know how to add textview one below another programmatically.
Any help in solving this would be helpful. Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc">
<LinearLayout
android:id="#+id/searchResultsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#fff"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_height="100dp"
android:padding="6dip" >
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="26dp"
android:ellipsize="marquee"
android:text="Ali Connors"
android:textSize="16sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/firstLine"
android:gravity="center_vertical"
android:text="Brunch this weekend?"
android:textSize="12sp" />
<TextView
android:id="#+id/rightTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="15m"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Code:
LinearLayout ll = (LinearLayout) findViewById(R.id.searchResultsLayout);
for (int i = 0; i <1; i++) {
RelativeLayout row= new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relativeParams.setMargins(20, 5, 20, 5);
row.setBackgroundColor(Color.parseColor("#ffffff"));
ll.setLayoutParams(relativeParams);
TextView name = new TextView(this);
TextView time = new TextView(this);
TextView dest = new TextView(this);
name.setText("Name");
time.setText("9hrs");
dest.setText("Destination");
row.addView(name);
row.addView(time);
row.addView(dest);
ll.addView(row,i);
}
just replace your code with below code:
here is java code
setContentView(R.layout.myview_activity);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
for (int i = 0; i <5; i++) {
LinearLayout row= new LinearLayout(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 5, 20, 5);
row.setLayoutParams(layoutParams);
row.setBackgroundColor(Color.parseColor("#000000"));
LinearLayout.LayoutParams layoutParams1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams1.setMargins(0,0,20,0);
TextView name = new TextView(this);
name.setLayoutParams(layoutParams1);
TextView time = new TextView(this);
time.setLayoutParams(layoutParams1);
TextView dest = new TextView(this);
dest.setLayoutParams(layoutParams1);
name.setText("Name");
time.setText("9hrs");
dest.setText("Destination");
row.addView(name);
row.addView(time);
row.addView(dest);
ll.addView(row);
}
}
LinearLayout ll = (LinearLayout) findViewById(R.id.searchResultsLayout);
ll.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i <1; i++) {
RelativeLayout row= new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relativeParams.setMargins(20, 5, 20, 5);
row.setBackgroundColor(Color.parseColor("#ffffff"));
ll.setLayoutParams(relativeParams);
TextView name = new TextView(this);
TextView time = new TextView(this);
TextView dest = new TextView(this);
final RelativeLayout.LayoutParams paramsval =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
name.setText("Name");
name.setId(View.generateViewId());
time.setText("9hrs");
dest.setText("Destination");
time.setLayoutParams(layoutParams);
row.addView(time);
row.addView(name);
paramsval.addRule(RelativeLayout.BELOW,name.getId());
dest.setLayoutParams(paramsval);
row.addView(dest);
ll.addView(row,i);
}
in order to get relative Layout one below other just use listview and adapter. it will automatically generate when you get data

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.

First TableRow is not shown

I'm trying to create a TableLayout, but some error occurs:
The data in the first red colored row are supposed to be in the second, white row.
Here is the layout xml file:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal|vertical"
android:layout_weight="1"
android:background="#838080">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/myOtherTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TableLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000">
<TableLayout
android:id="#+id/myTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical"
android:background="#FFFFFF"
>
</TableLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
And here is the code:
Color c = new Color();
setContentView(R.layout.data_table_creater_activity);
android.widget.TableRow.LayoutParams params = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 2, 0);
rowFiller();
TableLayout TbL = (TableLayout) findViewById(R.id.myOtherTableLayout);
TableRow headerRowHead = new TableRow(this);
headerRowHead.setId(51);
headerRowHead.setBackgroundColor(c.rgb(241,26,41));
TextView header1 = new TextView(this);
header1.setText(Html.fromHtml("<u>Purchase_Order_Number</u>"));
header1.setLayoutParams(params);
headerRowHead.addView(header1);
TextView header2 = new TextView(this);
header2.setText(Html.fromHtml("<u>Vendor</u>"));
header2.setLayoutParams(params);
headerRowHead.addView(header2);
TextView header3 = new TextView(this);
header3.setText(Html.fromHtml("<u>Currency</u>"));
header3.setLayoutParams(params);
headerRowHead.addView(header3);
TextView header4 = new TextView(this);
header4.setText(Html.fromHtml("<u>Total_Price</u>"));
header4.setLayoutParams(params);
headerRowHead.addView(header4);
TbL.addView(headerRowHead, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
TableRow headerRowData = new TableRow(this);
headerRowData.setId(50);
headerRowData.setBackgroundColor(c.rgb(255,255,255));
TextView headerData1 = new TextView(this);
header1.setText("0350005000");
header1.setLayoutParams(params);
headerRowData.addView(headerData1);
TextView headerData2 = new TextView(this);
header2.setText("Vendor_A");
header2.setLayoutParams(params);
headerRowData.addView(headerData2);
TextView headerData3 = new TextView(this);
header3.setText("EUR");
header3.setLayoutParams(params);
headerRowData.addView(headerData3);
TextView headerData4 = new TextView(this);
header4.setText("44.60");
header4.setLayoutParams(params);
headerRowData.addView(headerData4);
TbL.addView(headerRowData, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
Why are the data of the first row not displayed? And why are the data of the second row displayed in the first row?
Working with header1.setText(Html.fromHtml("<u>*Text*</u>"));, to underline text works fine, as you can see in the TableLayout below.
TextView headerData1 = new TextView(this);
header1.setText("0350005000");
header1.setLayoutParams(params);
headerRowData.addView(headerData1);
TextView headerData2 = new TextView(this);
header2.setText("Vendor_A");
header2.setLayoutParams(params);
headerRowData.addView(headerData2);
TextView headerData3 = new TextView(this);
header3.setText("EUR");
header3.setLayoutParams(params);
headerRowData.addView(headerData3);
TextView headerData4 = new TextView(this);
header4.setText("44.60");
header4.setLayoutParams(params);
headerRowData.addView(headerData4);
You have added header3 instead of headerData3 and so on for headerData1 ,headerData2 ,headerData3 .and headerData4 please check that , Is that the issue

Categories