Android scrollview not working - TableLayout - android

I am programatically adding components to TableLayout but scrolling is not working.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<ScrollView
android:id="#+id/appointments_view"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/appointmentType_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/text_appointment_type"
android:background="#color/text_grey_light"
android:padding="#dimen/list_item_top_padding"
android:textColor="#color/text_grey"/>
<Spinner
android:id="#+id/appointmentTypes"
android:layout_width="wrap_content"
android:padding="#dimen/booking_text_padding"
android:layout_below="#+id/appointmentType_title"
android:background="#null"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/appointmentTypeDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="16sp"
android:layout_below="#+id/appointmentTypes"
android:paddingStart="28dp"
android:paddingEnd="#dimen/list_item_top_padding"
android:textColor="#color/text_grey_light"/>
<TextView
android:id="#+id/select_appointments_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="#id/appointmentTypeDescription"
android:textStyle="bold"
android:text="#string/text_appointment"
android:background="#color/text_grey_light"
android:layout_marginTop="#dimen/list_item_top_padding"
android:padding="#dimen/list_item_top_padding"
android:textColor="#color/text_grey"/>
<TextView
android:id="#+id/no_appointments_available"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="#id/select_appointments_title"
android:text="#string/text_no_appointment"
android:layout_centerHorizontal="true"
android:padding="#dimen/list_item_top_padding"
android:textColor="#color/text_grey"/>
<Spinner
android:id="#+id/appointment_days"
android:layout_width="wrap_content"
android:padding="#dimen/booking_text_padding"
android:background="#null"
android:layout_below="#id/select_appointments_title"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table"
android:layout_below="#id/appointment_days"
android:paddingStart="#dimen/booking_text_padding"
android:paddingEnd="#dimen/booking_text_padding">
</TableLayout>
</RelativeLayout>
</ScrollView>
<android.support.v7.widget.AppCompatButton
android:id="#+id/action_book"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_booking"
android:textStyle="bold"
android:background="#color/colorPrimary"
android:textColor="#color/white"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Adding components to tableLayout container
private void addComponents(List<String> values){
for(String value: values){
TableRow tr = new TableRow(getActivity());
TableRow.LayoutParams p1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
tr.setPadding(getValueInDp(0),getValueInDp(8),getValueInDp(0),getValueInDp(0));
tr.setLayoutParams(p1);
TextView tv = new TextView(getActivity());
tv.setText(value);
int padding = getValueInDp(10);
tv.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.rounded_time_view));
tv.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
tv.setId(View.generateViewId());
TableRow.LayoutParams p = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
p.setMarginStart(getValueInDp(25));
tv.setLayoutParams(p);
tv.setClickable(true);
tv.setOnClickListener(valueClicked);
tv.setPadding(padding,padding,padding,padding);
tr.addView(tv);
container.addView(tr);
}
}

Related

Fields overlapping, creating layout programmatically

I have a layout XML and when I click on a floatbutton, I create a layout programmatically, so that the data of this layout is passed to this new one.
Here's my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundnewlayout"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#drawable/img_interna" />
<TextView
android:id="#+id/txtTitle_avaliacao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Medicamento"
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/txtTitle_avaliacao"
android:layout_marginBottom="40dp"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/linearCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_margin="5dp"
android:elevation="7dp"
android:minHeight="200dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtNome_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Nome:"
android:textSize="10sp" />
<AutoCompleteTextView
android:id="#+id/edtNome_medicamento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtNome_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="Paracetamol"
android:maxLines="1"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtNome_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Quantidade:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQtd_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="3"
android:maxLines="1"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/edtQtd_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Posologia:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPosologia_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="2 vezes ao dia a cada 8 horas"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fltNewLayout"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/cardView"
android:layout_marginRight="5dp"
android:layout_marginTop="170dp"
android:backgroundTint="#color/colorPrimaryDark"
android:clickable="true"
android:src="#drawable/icone_check"
app:fab_colorPressed="#color/colorPrimary"
app:rippleColor="#android:color/white" />
<LinearLayout
android:id="#+id/linearReceita"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fltNewLayout"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</FrameLayout>
</RelativeLayout>
When I click on the floatbutton, I create a new layout with the data of this layout that is fixed.
But, the fields of the layout created programmatically, overlap, how to fix?
The method below is as follows:
private void createNewLayout(){
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,15,5,5);
//linearLayout.setLayoutParams(params);
cardView = new CardView(getContext());
cardView.setLayoutParams(linearLayout.getLayoutParams());
cardView.setPadding(10,10,10,10);
cardView.setMaxCardElevation(6);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(linearLayout.getLayoutParams());
cardView.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
cardView.addView(txtNomeMedicamento);
linearLayout.addView(cardView);
}
I solved the issue by adding a new linear layout, this linear adds the textviews vertically, after adding these fields, it is inserted into the cardview.
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
Change the code below:
private void createNewLayout() {
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 5, 5);
cardView = new CardView(getContext());
cardView.setLayoutParams(params);
//cardView.setPadding(10, 10, 10, 10);
cardView.setMaxCardElevation(6);
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
cardView.addView(linearCard);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(params);
//cardView.addView(txtNomeTitle);
linearCard.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
txtNomeMedicamento.setPadding(10, 10, 10, 10);
// cardView.addView(txtNomeMedicamento);
linearCard.addView(txtNomeMedicamento);
//cardView.addView(linearLayout);
linearLayout.addView(cardView);
}
I was inserting the fields directly into the cardview, without a layout inside it, which made the fields overlap.

How to set 4 images to linear layout acording to devices in android

I have 4 images within linearlayout that is created programmatically and i want to set textview on top of fourth image how to set??if u have piece of code please share with me??
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300, 1.0f);
public void path(ArrayList<Integer> integerImg) {
// int width=mContext.getWgetWindowManager().getDefaultDispaly.getWidth();
ImageView images;
for (int i = 0; i < integerImg.size(); i++) {
if (i < 4) {
images = new ImageView(mContext);
params.setMargins(10, 0, 0, 0);
images.setLayoutParams(params);
images.setScaleType(ImageView.ScaleType.FIT_XY);
images.setBackgroundResource(R.drawable.salon_profile_back);
images.setImageResource(integerImg.get(i));
linearImgGrid.addView(images);
}
Try this
TextView tv= new TextView(this);
tv.setText("hallo hallo");
tv.setId(5);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//add the 4th linear layout
((LinearLayout) linearLayout).addView(tv);
Instead of add an ImageView in the LinearLayout, you could create a RelativeLayout put inside an ImageView and a TextView and add RelativeLayout to LinearLayout, as this:
RelativeLayout.LayoutParams layoutParamsImage = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams layoutParamsText = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsText.addRule(RelativeLayout.CENTER_IN_PARENT);
for (int i = 0; i < integerImg.size(); i++) {
if (i < 4) {
RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.setLayoutParams(params);
ImageView images = new ImageView(mContext);
params.setMargins(10, 0, 0, 0);
images.setLayoutParams(layoutParamsImage);
images.setScaleType(ImageView.ScaleType.FIT_XY);
images.setBackgroundResource(R.drawable.salon_profile_back);
images.setImageResource(integerImg.get(i));
relativeLayout.addView(images);
TextView textView = new TextView(mContext);
textView.setLayoutParams(layoutParamsImage);
textView.setText("My Text");
relativeLayout.addView(textView);
linearImgGrid.addView(relativeLayout);
}
}
You can use following code for reference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_marginTop="70dp"
android:weightSum="3"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/user" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Master" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/report1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Report" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:layout_marginRight="#dimen/feed_item_margin"
android:weightSum="2"
android:layout_weight="1"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/expense1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Expenses" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:layout_marginTop="70dp"
android:weightSum="3"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/collection" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Collection" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_marginLeft="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:layout_weight="1"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/monthly" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Report" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="2"
android:layout_weight="1"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/extra" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Expenses" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Dynamically added tablerow only last row not visible

I have created a tablelayout with the header row added in the XML file and the remaining content rows filled in dynamically.
For some reason, the last row is not visible.
I looked through other related posts and everything seems okay in my code.
Appreciate if someone could take a look and point out the problem.
XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.bluechipsys.mcpackagemanager.PMActivity"
android:id="#+id/view_layout">
<ProgressBar
android:id="#+id/installProgressBar"
android:indeterminate="true"
style="#android:style/Widget.Holo.ProgressBar.Large"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
<ScrollView
android:id="#+id/textAreaScroller"
android:layout_above="#+id/btn_reload"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:singleLine="false">
<TextView
android:text="#string/msg_default"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="#+id/viewLog"
android:maxLines="1000"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:singleLine="false"
android:background="#drawable/border" />
</ScrollView>
<TextView
android:id="#+id/versionInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayoutScroller"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/tableLayoutScroller"
android:text="#string/tv_versioninfo"
android:textStyle="bold" />
<ScrollView
android:id="#+id/tableLayoutScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_alignParentStart="#+id/textAreaScroller"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller"
android:layout_above="#+id/textAreaScroller"
android:fadeScrollbars="false"
android:scrollbars="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:id="#+id/tbl_packagelist"
android:orientation="vertical"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_marginTop="30dp"
android:layout_alignParentStart="#+id/textAreaScroller"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller"
android:layout_above="#+id/textAreaScroller"
android:scrollHorizontally="false"
android:stretchColumns="1,2,3,4,5"
android:shrinkColumns="2"
android:background="#drawable/border_table">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="false"
android:background="#android:drawable/list_selector_background">
<CheckBox
android:id="#+id/cbSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/title_cell_shape"
android:onClick="onCheckboxClicked" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/tvState"
android:background="#drawable/title_cell_shape"
android:editable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_package"
android:id="#+id/tvPackage"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:textAlignment="center"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_instver"
android:id="#+id/tvInstver"
android:singleLine="false"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:textAlignment="center"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_latestver"
android:id="#+id/tvLatestver"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_size"
android:id="#+id/tvSize"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:paddingLeft="2dp" />
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reload"
android:id="#+id/btn_reload"
android:background="#android:color/transparent"
android:drawableTop="#drawable/reload"
android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_apply"
android:id="#+id/btn_apply"
android:background="#android:color/transparent"
android:drawableTop="#drawable/apply"
android:layout_alignTop="#+id/btn_reload"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_close"
android:id="#+id/btn_close"
android:background="#android:color/transparent"
android:drawableTop="#drawable/close"
android:layout_alignTop="#+id/btn_reload"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller" />
</RelativeLayout>
Java Code:
private void clearTableRows() {
Logger.d(TAG, "Clearing table rows");
TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
int rowCount = main_table.getChildCount();
main_table.removeViews(1, rowCount - 1); // Leave header row intact
}
// Create table rows and add to the GUI
private void createTableRows() {
Logger.d(TAG, "Creating table rows");
final TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
Drawable background;
// Define layout parameters
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams itemCBParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams itemIVParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
itemIVParams.gravity = Gravity.CENTER; // Centre the image display
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
ViewGroup.MarginLayoutParams mLayoutItemParams = new ViewGroup.MarginLayoutParams (itemParams);
MarginLayoutParamsCompat.setMarginStart(mLayoutItemParams, 4); // Need this workaround for API level < 17
for (MCPackage pkg: list_packages) {
// Create the TableRow and widgets to add to the TableRow
TableRow tbl_row = new TableRow(this);
CheckBox cb_select = new CheckBox(this);
ImageView iv_state = new ImageView(this);
TextView tv_package = new TextView(this);
TextView tv_instver = new TextView(this);
TextView tv_currver = new TextView(this);
TextView tv_pkgsize = new TextView(this);
int index = list_packages.indexOf(pkg);
if(index % 2 == 0) {
background = getResources().getDrawable(R.drawable.cell_shape_white);
} else {
background = getResources().getDrawable(R.drawable.cell_shape_grey);
}
// Set GUI parameters for the TableRow
tbl_row.setLayoutParams(rowParams);
tbl_row.setBackground(background);
tbl_row.setId(index); // Set ID to match index of list_packages
// Initialize the cells
cb_select.setLayoutParams(itemCBParams);
cb_select.setId(index + CHECKBOX_ID);
cb_ids[index] = (index + CHECKBOX_ID);
cb_select.setOnClickListener(checkboxOnClick());
cb_select.setOnCheckedChangeListener(checkboxOnCheckChange());
iv_state.setLayoutParams(itemIVParams);
iv_state.setImageResource(pkg.getState().getImageResourceId());
iv_state.setId(index + IVSTATE_ID);
iv_state_ids[index] = (index + IVSTATE_ID);
tv_package.setLayoutParams(itemParams);
tv_package.setText(pkg.getPackageName());
tv_package.setGravity(Gravity.FILL);
tv_package.setTextAppearance(this, android.R.style.TextAppearance_Small);
tv_package.setTextSize(12);
tv_package.setTextColor(Color.DKGRAY);
tv_package.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_instver.setLayoutParams(itemParams);
tv_instver.setText(pkg.getInstalledVersion());
tv_instver.setGravity(Gravity.FILL);
tv_instver.setTextSize(12);
tv_instver.setTextColor(Color.BLUE);
tv_instver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_currver.setLayoutParams(itemParams);
tv_currver.setText(pkg.getUpdateVersion());
tv_currver.setGravity(Gravity.FILL);
tv_currver.setTextSize(12);
tv_currver.setTextColor(Color.DKGRAY);
tv_currver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_pkgsize.setLayoutParams(itemParams);
tv_pkgsize.setText(String.valueOf(((int) pkg.getPackageSize()) / KBINBYTES) + "K");
tv_pkgsize.setGravity(Gravity.FILL);
tv_pkgsize.setTextSize(12);
tv_pkgsize.setTextColor(Color.BLUE);
tv_pkgsize.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
// Add the cells to the row
tbl_row.addView(cb_select);
tbl_row.addView(iv_state);
tbl_row.addView(tv_package);
tbl_row.addView(tv_instver);
tbl_row.addView(tv_currver);
tbl_row.addView(tv_pkgsize);
main_table.addView(tbl_row); // Add the row to the table
tbl_row.setOnClickListener(rowClickListener);
tbl_row.setOnLongClickListener(rowLongClickListener);
registerForContextMenu(tbl_row); // Register the table row for context menu
Logger.d(TAG, "Added package: " + pkg.getPackageName());
}
}
I'm not sure if it is still a problem, but try to use NestedScrollView instead of ScrollView.

Create multiple linear layouts and text views programmatically

Create multiple linear layouts and text views programmatically
I have tried to explain please see the code of xml file to understand the question
See image for my desired output
The xml code below is what I want exactly but programmatically
<RelativeLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
</LinearLayout>
</RelativeLayout
I want to achieve the same output but programmatically
Try This
add LinearLayout into your xml
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
</LinearLayout>
and change in your Java file like this
LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
for(int i= 0; i <5 ;i++) {
LinearLayout parent = new LinearLayout(Main.this);
LinearLayout.LayoutParams param= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(Main.this);
tv.setText("T1");
TextView tv2 = new TextView(Main.this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
ll_main.addView(parent);
}
i just got my answer and its below
LinearLayout ll_main = (LinearLayout) findViewById(R.id.linear);
ll_main.setVisibility(View.VISIBLE);
int j = 5;
final LinearLayout[] linearlayout = new LinearLayout[j];
for (int i = 0; i < j; i++) {
LinearLayout parent = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("T1");
TextView tv2 = new TextView(this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
linearlayout[i] = parent;
ll_main.addView(parent);
}
}
}

how to generate dynamic textview in a tablerow or tablelayout in android?

I have a problem with the following code:
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
final TableLayout tl = (TableLayout)findViewById(R.id.maintable);
for(i = 0; i < 10; i++)
{
tx[i] = new TextView(TransactionsActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText(Dates1);
tr[i] = new TableRow(TransactionsActivity.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
tl.addView(tr[i], new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
What I am trying to do is to dynamically add textview to a table row or layout. But this code does nothing when I run it, no errors though.
My XML code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/energy">
<ScrollView
android:id="#+id/scrllvwNo1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Screen Design for VIDEOS -->
<TextView android:id="#+id/textview1"
android:text="Transactions page"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button"
android:text="Clear Transactions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textview2"
android:layout_below="#id/textview1"/>
<TextView android:id="#+id/textview2"
android:text=""
android:padding="15dip"
android:textSize="18dip"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_centerHorizontal="true"/>
<Button android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Transactions"
android:layout_toLeftOf="#id/textview2"
android:layout_alignTop="#id/button"/>
<TextView android:id="#+id/textview"
android:text=""
android:padding="0dip"
android:textSize="0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button"/>
<HorizontalScrollView android:id="#+id/horz"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/textview">
<TableLayout android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="4"
android:layout_below="#id/horz">
<TableRow>
<TextView
android:id="#+id/textview1"
android:layout_column="1"
android:text="Date"
android:padding="15dip" />
<TextView
android:id="#+id/textview2"
android:layout_column="2"
android:text="Device"
android:layout_toLeftOf="#id/textview1"
android:padding="15dip" />
<TextView
android:id="#+id/textview3"
android:layout_column="3"
android:text="Access Type"
android:layout_toLeftOf="#id/textview2"
android:padding="15dip" />
<TextView
android:id="#+id/textview4"
android:layout_column="4"
android:text="Area"
android:layout_toLeftOf="#id/textview3"
android:padding="15dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</HorizontalScrollView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The problem might be that the layout parameters are not specified for the textviews. Try this
tx[i].setLayoutParams(new LayoutParams(ViewGroup.WRAP_CONTENT,
ViewGroup.WRAP_CONTENT));
Also the tr array should be added to the tablelayout
I just removed adding LayoutParams for TextView and got it working. I don't know why.
Sample Code :
for(int i = 0; i < 10; i++)
{
tx[i] = new TextView(this);
tx[i].setText(Dates1);
tr[i] = new TableRow(this);
tr[i].setLayoutParams(
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
tl.addView(tr[i], new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}

Categories

Resources