Only the first RelativeLayout is rendered in for loop - java

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.

Related

TextView and ImageView stuck out of LinearLayout

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.

Add buttons dynamically to ScrollView android

So I want to add x amount of buttons to a layout in android (where x is usually between 4 and 24), and I can achieve this using the code below, but it doesn't scroll so it cuts off some of the buttons
I am using a fragment, which contains a LinearLayout and BottomNavigation, and one of those navigation options leads to the fragment contained below
At the moment my.xml file looks like:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/units_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_course_units, container, false);
final LinearLayout unitsBtns = (LinearLayout) view.findViewById(R.id.units_group);
//I actually get this from a volley request but didn't want to include all the code
String[] units = {"1", "2", "3", "4", "5", "6", "7", "8"};
for(int i = 0; i < units.length; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
params.setMargins(0, 30, 0, 0);
Button b = new Button(getActivity());
b.setLayoutParams(params);
b.setText(units[i]);
b.setId(i);
b.setBackgroundColor(Color.WHITE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// STUFF HERE
} catch (Exception e) {
e.printStackTrace();
}
}
});
unitsBtns.addView(b);
}
return view;
}
Is there any way to get the buttons to be scrollable? I have tried to put the scrollView around the parent of this too and it still doesn't seem to work.
Note: I am still pretty new to developing native Android apps so please correct me if I'm doing something incorrectly
when you have to use ScrollView you should give android:layout_height="wrap_content" in child view of ScrollView then only you have to scroll your view
You should use user LinearLayout like this
<LinearLayout
android:id="#+id/units_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
</LinearLayout>
here also you have to replace
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Note: when you give match_parent in height attribute, it will limit your view to device screen
hope it will help you.. :)
Here's a working example (horizontal) :
<HorizontalScrollView
android:id="#+id/hsv1"
android:scrollbars="none"
android:background="#0d47a1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/hsvLayout1"
android:background="#0d47a1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
Get ScrollView and Layout:
HorizontalScrollView hsv1 = (HorizontalScrollView) findViewById( R.id.hsv1 );
LinearLayout layout = (LinearLayout) hsv1.findViewById( R.id.hsvLayout1 );
layout.removeAllViews();
Create a button dynamically:
Button myButton = new Button (this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
lp.setMargins( 20, 0, 20, 0 );
myButton.setLayoutParams(lp);
myButton.setTextColor( Color.YELLOW );
myButton.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
myButton.setText( "Hello World !" );
Add button to layout:
layout.addView(myButton);

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

TableLayout inside a Fragment

I am creating a Fragment that will dynamically create a tableLayout but I am having problems with it as it is not being displayed. Can someone tell me why it is not displaying? The section label can be displayed except for the table.
Here is my xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_fragment_bp_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/section_bp_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/tableLayoutBloodPressure"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
Here is my Fragment Class - the data is for testing only
public class BloodPressureFragment extends Fragment {
public BloodPressureFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bp_list, container,
false);
TextView txt = (TextView) rootView.findViewById(R.id.section_bp_label);
txt.setText("Blood Pressure History Logs");
TableLayout ll = (TableLayout) rootView
.findViewById(R.id.tableLayoutBloodPressure);
for (int i = 1; i < 10; i++) {
TableRow tbrow = new TableRow(getActivity().getApplicationContext());
tbrow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv1 = new TextView(getActivity().getApplicationContext());
tv1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setId(i);
tv1.setText("Test id: " + i);
tbrow.addView(tv1);
ll.addView(tbrow, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
return rootView;
}
}
I have same problem, and my solution is to inflate row layout from fragment view.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_intro_a,container,false); // infalte normally
Tablelayout mytable = (TableLayout) v.findViewById(R.id.mytable); // init table
/* your stuff*/
/* now fill table layout directly on fragment */
TableRow row = new TableRow(getActivity()); // new row
View table_row_view = v.inflate(getActivity(),R.layout.your_layout,row); // inflate from parent view;
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView mytextview = (TextView)vvv.findViewById(R.id.yourtextview-in-yourlayout);
mytable.addView(row, new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
/* TADAAN! rows is visible! */
}
Instead of this hiearchy
[TableLayout < TableRow < Textview]
try to add directly textview to tablelayout, avoiding tablerows.
UPDATE
This works fine for me. Create a tableLayout where generate few tablerows with some textviews inside.
TableLayout tl;
tl = (TableLayout) rootView.findViewById(R.id.fragment1_tlayout);
for (int i = 0; i < 30; i++) {
TableRow tr = new TableRow(getActivity());
tr.setId(i);
tr.setBackgroundResource(R.color.RoyalBlue);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//TEXTVIEWS********
TextView tv1 = new TextView(getActivity());
tv1.setText("TEST NUMBER");
tv1.setId(i);
tv1.setTextColor(Color.WHITE);
tv1.setTextSize(20);
tv1.setPadding(5, 5, 5, 5);
tr.addView(tv1);
TextView tv2 = new TextView(getActivity());
tv2.setText("nº: "+ i);
tv2.setId(i+i);
tv2.setTextColor(Color.WHITE);
tv1.setTextSize(20);
tv2.setPadding(5, 5, 5, 5);
tr.addView(tv2);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
XML:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<TableLayout
android:id="#+id/fragment1_tlayout"
android:background="#color/LightGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>

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