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
Related
I am trying to create several RelativeLayouts with two TextViews each, using "for" loop and insert those RelativeLayouts into LinearLayout, selected with id, using addView, but only the first RelativeLayout is rendered. Also I have a button in the layout after that LinearLayout. That button also is not rendered. Could you please help me in identifying a mistake in my code? Logcat does not show any error while rendering. Thank you.
MainFragment.java:
public class MainFragment extends Fragment {
DateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy, EEEE");
Date date = new Date();
String[] timeNames;
public static MainFragment newInstance() {
return new MainFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = view.findViewById(R.id.date0);
textView.setText(dateFormat.format(date));
timeNames = getResources().getStringArray(R.array.time_names);
LinearLayout parentToday = view.findViewById(R.id.today_times);
RelativeLayout newRelativeLayout;
TextView newTextView;
for (int i = 0; i < 6; i++) {
newRelativeLayout = new RelativeLayout(getContext());
newRelativeLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT));
newTextView = new TextView(getContext());
newTextView.setText(timeNames[i]);
newTextView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT));
newRelativeLayout.addView(newTextView);
newTextView = new TextView(getContext());
newTextView.setText("00:00");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newTextView.setLayoutParams(params);
newRelativeLayout.addView(newTextView);
parentToday.addView(newRelativeLayout);
}
return view;
}
}
fragment_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="10dp"
android:paddingEnd="10dp">
<TextView
android:id="#+id/date0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="18sp" />
<Button
android:id="#+id/internet_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/internet_check"
android:textSize="18sp" />
<LinearLayout
android:id="#+id/today_times"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Показать на неделю"
android:textSize="18sp"/>
</LinearLayout>
I have found the solution. The problem was in the following lines:
newTextView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT));
and
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
I had changed them to the following ones:
newTextView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
and
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
After that the problem was solved.
I'm building an android app with a TextView, a ScrollView and a LinearLayout, I want to add one TextView and an ImageView to this LinearLayout using Java but they both stay outside of the Layout.
This is my activity XML:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#drawable/galaxy"
tools:context=".Planet">
<TextView
android:id="#+id/planets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/purple"
android:text="#string/planets"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />
<ScrollView
android:id="#+id/scroll"
android:layout_width="409dp"
android:layout_height="646dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
And this is the code I'm using to generate and add the TextView and the ImageView:
public class MainActivity extends AppCompatActivity {
private ArrayList<Planet> planets = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Planet mercury = Planet.getPlanetFromResources(0, getResources());
planets.add(mercury);
addPlanetToUI(mercury);
}
public void addPlanetToUI(final Planet planet){
int color = getResources().getColor(R.color.yellow);
LinearLayout linear = findViewById(R.id.linear);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setWeightSum(20f);
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp1 = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp2 = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout horizontal = new LinearLayout(this);
horizontal.setWeightSum(6f);
horizontal.setOrientation(LinearLayout.HORIZONTAL);
lp.weight = 10f;
horizontal.setLayoutParams(lp);
ImageView iv = new ImageView(this);
lp1.weight = .75f;
iv.setImageDrawable(getDrawable(planet.getImgSmallResourceValues()));
iv.setLayoutParams(lp1);
TextView tv = new TextView(this);
lp2.weight= .5f;
tv.setText(planet.getName());
tv.setBackgroundColor(color);
tv.setTextSize(40);
tv.setLayoutParams(lp2);
horizontal.addView(iv);
horizontal.addView(tv);
linear.addView(horizontal);
}
}
This is the outcome:
So is not that your elements are outside of the layout. The problem is that your ScrollView has a top constraint to the top of parent.
Change your ScrollView top constraint (app:layout_constraintTop_toTopOf="parent") to:
app:layout_constraintTop_toBottomOf="#id/planets"
Since your layout container is a ConstraintLayout elements can overlap between them.
i am newbie and facing difficulty to achieve my required output.
XML CODE:
//this code is inside of ScrollView
<LinearLayout
android:id="#+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></TableLayout>
</LinearLayout>
JAVA CODE:
setContentView(R.layout.status_view);
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
TableRow statusTableRow = new TableRow(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(TableRow.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);
for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout,rlp);
}
statusTableLayout.addView(statusTableRow);}
please tell me what should i need to do changes in my current code to product required given image
You can use LinearLayout instead of TableRow like this
TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
LinearLayout statusTableRow = new LinearLayout(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(LinearLayout.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);
for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout);
}
statusTableLayout.addView(statusTableRow);}
also put everything inside Relativelayout and then put inside ScrollView like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</TableLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
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
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.