TableLayout inside a Fragment - java

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>

Related

Dynamic buttons dont show in android studio using fragments

Im trying to dynamicaly add buttons to a fragment, but they do not show.
This is the listCharacter.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list_character, container, false);
context = view.getContext();
for (int i = 1; i <= 20; i++) {
LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button firstBtn = new Button(this.context);
firstBtn.setId(i);
final int id_ = firstBtn.getId();
/**
* The text is not updated(?)
*/
firstBtn.setText("button " + id_);
/**
* it does not add 20 button, might be on top of each other
*/
layout.addView(firstBtn, params);
firstBtn = view.findViewById(R.id.characterNameBtn);
firstBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
Navigation.findNavController(view).navigate(R.id.action_listCharacter_to_loadCharacter);
}
});
}
return view;
}
And this is the fragment_list_character.xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context=".listCharacter"
android:orientation="vertical">
<LinearLayout
android:id="#+id/list_character_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<Button
android:id="#+id/characterNameBtn"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#66000000"
android:fontFamily="#font/main"
android:gravity="center"
android:padding="5dp"
android:text=""
android:textColor="#fff"
android:textSize="40sp"
/>
</LinearLayout>
</LinearLayout>
I dont get any error messages atm, but i cant see the buttons when i run the program.
The on click works and it says it has id 20.
You're adding the buttons to a LinearLayout that you are recreating each time through your loop.
for (int i = 1; i <= 20; i++) {
LinearLayout layout = new LinearLayout(context);
...
layout.addView(firstBtn, params)
....
}
layout is never added to the view that is returned. You will need to add the buttons to the view or to a child of the view that you return to see them on the screen. I think that there are a few other issues, but this would be the first one to address.

Only the first RelativeLayout is rendered in for loop

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.

OverLay Two Layouts in One Fragment

I'd like to have layout B on top of layout A. I want to join or merge the two layouts. In the image below, the round green, and blue views shares the same layout but they are created as different fragments. The RoundedColourFragment class extends SherlockFragment and is responsible in customizing the fragment layout size and shape. My problem is I can't put the two_fragment layout at the top of twoFragments layout. I been trying to join them in the onCreateView of RoundedColourFragment. It's like overlaying the layouts.
leftFrag = new RoundedColourFragment(getActivity().getResources().getColor(
R.color.trans), 1f, MARGIN, MARGIN / 2, MARGIN, MARGIN);
rightFrag = new RoundedColourFragment(getActivity().getResources().getColor(
R.color.honeycombish_blue), 2f, MARGIN / 2, MARGIN, MARGIN,
MARGIN);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.add(R.id.twoFragments, leftFrag, "left");
ft.add(R.id.twoFragments, rightFrag, "right");
ft.addToBackStack(null);
ft.commit();
twoFragments.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/twoFragments">
</LinearLayout>
RoundedColourFragment.java
public class RoundedColourFragment extends SherlockFragment {
private View mView;
private int mColour;
private float mWeight;
private int marginLeft, marginRight, marginTop, marginBottom;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mView = new View(getActivity());
GradientDrawable background = (GradientDrawable) getResources()
.getDrawable(R.drawable.rounded_rect);
background.setColor(mColour);
mView.setBackgroundDrawable(background);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, mWeight);
lp.setMargins(marginLeft, marginTop, marginRight, marginBottom);
mView.setLayoutParams(lp);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = vi .inflate(R.layout.two_fragment, container, false);
((ViewGroup) rootView).addView(mView, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
return rootView;
}
}
two_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/two_fragment" >
<Button
android:id="#+id/bbtnn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

How to add rows in table of android

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/myImView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="#drawable/image1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/myTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
I want to add n-rows in table "myTable" at run time what should i do ?
currently i am using this code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
TextView tv = new TextView(this);
for (int x = 0; x < 5; x++) {
tv.setText("This is row number=" + (x + 1));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
}
but it is giving me this error
03-26 00:29:34.070:
E/AndroidRuntime(2230): java.lang.RuntimeException: Unable to start activity ComponentInfo{assignment.1/assignment.1.myView}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
for (int x = 0; x < 5; x++) {
TextView tv = new TextView(this);
tv.setText("This is row number=" + (x + 1));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
}
Try now a small and minor change
I guess this is because you are creating the row object new every time in your loop, but you dont do this with the TextView Object.
Like this it should work:
TableLayout myTable = (TableLayout) findViewById(R.id.myTable);
TextView tv;
TableRow tr;
for (int x = 0; x < 5; x++) {
tv = new TextView(this);
tv.setText("This is row number=" + (x + 1));
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(tv);
myTable.addView(tr, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}

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