I have a horizontal linear layout that is filled with TextViews from a dynamic list, but it doesn't show all elements since it seems to be out of the parent's bounds. What I need is to fill the space below if needed.
Here's my code:
The xml:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView_serie" android:contentDescription="imatge_serie"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom_serie"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:id="#+id/nom_serie" android:contentDescription="nom_serie" tools:text="Nom_serie"
android:layout_toRightOf="#id/imageView_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "16dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:id="#+id/any_serie" android:contentDescription="any_serie" tools:text="any_serie"
android:layout_toRightOf="#id/nom_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sinopsis"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/sinopsi_serie" android:contentDescription="sinopsis_serie" tools:text="sinopsis_serie"
android:layout_toRightOf="#id/imageView_serie"
android:layout_below = "#id/nom_serie"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:id="#+id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
android:layout_centerVertical="true"
style="?android:attr/ratingBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntuacio"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/puntuacio" android:contentDescription="puntuacio" tools:text="puntuacio"
android:layout_toRightOf="#id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Casting:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/casting" android:contentDescription="casting" tools:text="casting"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/cast" android:contentDescription="cast" tools:text="cast"
android:layout_toRightOf="#id/casting"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Direccio:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/direccio" android:contentDescription="direccio" tools:text="direccio"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/dir" android:contentDescription="dir" tools:text="dir"
android:layout_toRightOf="#id/direccio"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temporades:"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/nTemporades" android:contentDescription="nTemporades" tools:text="nTemporades"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comentaris"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/titol_comentaris_serie" android:contentDescription="titol_comentari_serie" tools:text="titol_comentari_serie"
/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/panell"
android:orientation="horizontal">
</LinearLayout>
And this how I generate the text views from the list:
LinearLayout panell = (LinearLayout) findViewById(R.id.panell);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView btn = new TextView(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
//btn.setBackgroundColor(Color.rgb(70, 80, 90));
panell.addView(btn, params);
btn = ((TextView) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
Here's my :
output
I recommend Google's flexbox-layout project: https://github.com/google/flexbox-layout
A FlexboxLayout (when configured correctly) has the ability to add any number of child TextViews in a line (like LinearLayout would), but will automatically "wrap" to the next line if a child wouldn't fit.
Why not try to use a ListView , if u add the same propreties every time
Related
I been working on a simple android app that calculates a person's Body Mass Index, I have all the features working but positioning the arrow in the right place in the color bar corresponding to the user's screen size is what Im stuck on. I have it working by setting the X and Y values of the arrow ImageView but obviously the place of the arrow changes when i test my application in different screen sizes even though im coverting a dp value to pixels. How can I position the arrow ImageView so that it stays the same in different screen sizes?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="coding.guillermo.bmiapp.MainActivity2"
tools:showIn="#layout/activity_main2"
android:clickable="false"
android:background="#ffffff"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI"
android:id="#+id/bmiText"
android:textSize="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21.24"
android:id="#+id/bmiResult"
android:textSize="30dp"
android:layout_below="#+id/bmiText"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bmiCategory"
android:textSize="25dp"
android:text="Normal weight"
android:layout_marginTop="22dp"
android:layout_below="#+id/bmiResult"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save result"
android:id="#+id/saveButton"
android:backgroundTint="#color/toolBarColor"
android:textColor="#ffffff"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Log"
android:id="#+id/trackerButton2"
android:backgroundTint="#color/toolBarColor"
android:textColor="#ffffff"
android:layout_alignTop="#+id/saveButton" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/bmibar"
android:layout_marginTop="36dp"
android:layout_below="#+id/bmiCategory" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Underweight <18.50 "
android:id="#+id/underweightText"
android:textSize="22sp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal 18.5 - 24.99"
android:id="#+id/normalText"
android:textSize="22sp"
android:paddingTop="5dp"
android:layout_below="#+id/underweightText"
android:layout_alignStart="#+id/underweightText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overweight >=25.00"
android:id="#+id/overweightText"
android:layout_below="#+id/normalText"
android:textSize="22sp"
android:paddingTop="5dp"
android:layout_alignStart="#+id/normalText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obese >=30.00"
android:id="#+id/obeseText"
android:textSize="22sp"
android:paddingTop="5dp"
android:layout_below="#+id/overweightText"
android:layout_alignStart="#+id/overweightText" />
public class MainActivity2 extends AppCompatActivity {
TextView resultText,bmiLabel,underWeightText,normalText,overweightText,obeseText;
RelativeLayout.LayoutParams params;
Button saveButton,trackerButton;
Result result;
EditText userName;
DBhandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// TextViews
resultText = (TextView) findViewById(R.id.bmiResult);
bmiLabel = (TextView) findViewById(R.id.bmiCategory);
underWeightText = (TextView) findViewById(R.id.underweightText);
normalText = (TextView) findViewById(R.id.normalText);
overweightText = (TextView) findViewById(R.id.overweightText);
obeseText = (TextView) findViewById(R.id.obeseText);
// Button
saveButton = (Button) findViewById(R.id.saveButton);
trackerButton = (Button) findViewById(R.id.trackerButton2);
// Getting User object from the previous activity
result = (Result) getIntent().getParcelableExtra("result");
// Database
dbHandler = new DBhandler(this);
// Displaying the arrow in the corresponding place
ImageView arrow = new ImageView(this);
params = new RelativeLayout.LayoutParams(80,80);
arrow.setImageResource(R.drawable.arrow2);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
// the display of the arrow is different when tested in device's with different screen sizes
int dpValue = 0;
int dpValue2 = 166;
float d = getApplicationContext().getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d);
int margin2 = (int) (dpValue2 * d);
arrow.setX(margin);
arrow.setY(margin2);
rl.addView(arrow);
// BMI diplay
resultText.setText(Double.toString(result.getBMI()));
bmiLabel.setText(result.getBmiCategory());
// BMI category bold display
bmiCategoryBold(result.getBMI());
// Saving result to internal storage for later retrieval
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = (LayoutInflater.from(MainActivity2.this)).inflate(R.layout.alert_content,null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity2.this);
alertBuilder.setView(view);
userName = (EditText) view.findViewById(R.id.nameInput);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String date = dateFormat.format(new Date());
result.setDate(date);
alertBuilder.setCancelable(true).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
result.setName(userName.getText().toString());
// adding result to the SQLite database
dbHandler.addResult(result);
Toast toast = Toast.makeText(getApplicationContext(),"result saved",Toast.LENGTH_SHORT);
toast.show();
}
});
AlertDialog dialog = alertBuilder.create();
dialog.show();
Button nButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
nButton.setBackgroundColor(getResources().getColor(R.color.toolBarColor));
nButton.setTextColor(Color.WHITE);
}
});
trackerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity3.class);
startActivity(intent);
}
});
}
public void bmiCategoryBold(double bmi){
if(bmi < 18.50){
underWeightText.setTypeface(null, Typeface.BOLD);
}
else if(bmi <= 24.99){
normalText.setTypeface(null,Typeface.BOLD);
}
else if(bmi<=29.99){
overweightText.setTypeface(null,Typeface.BOLD);
}
else{
obeseText.setTypeface(null,Typeface.BOLD);
}
}
}
The first pic is the app running on 1080 pixels by 1920 pixels screen and the second is a 1440 pixels by 2560 pixels screen
first pic
second pic
Add Linearlayout as subparent of childview,use its orientation and gravity attribute you can easily get the design in more optimize way and suitable for everyscreen size.
here i have used RelativeLayout as Parent and LinearLayout as sub-parent of childview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/bmiText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI"
android:textSize="25dp" />
<TextView
android:id="#+id/bmiResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="21.24"
android:textSize="30dp" />
<TextView
android:id="#+id/bmiCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal weight"
android:textSize="25dp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/underweightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Underweight <18.50 "
android:textSize="22sp" />
<TextView
android:id="#+id/normalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Normal 18.5 - 24.99"
android:textSize="22sp" />
<TextView
android:id="#+id/overweightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Overweight >=25.00"
android:textSize="22sp" />
<TextView
android:id="#+id/obeseText"
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Obese >=30.00"
android:textSize="22sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:gravity="left"
android:layout_height="wrap_content">
<Button
android:id="#+id/trackerButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#000000"
android:text="BMI Log"
android:gravity="center"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<Button
android:id="#+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:backgroundTint="#000000"
android:text="Save result"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try to avoid too much margin top and bottom.
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
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);
}
}
}
i have a custom listview i want to add one lat row to it which will calculate total of all the list when i scroll the last row gets added again and again
if(position==searchResult.size() && searchResult.size()!=1)
{
holder.checkImg.setVisibility(ImageView.GONE);
holder.fvtImg.setVisibility(ImageView.GONE);
holder.type.setVisibility(TextView.GONE);
holder.name.setVisibility(TextView.GONE);
holder.offer_price.setVisibility(TextView.GONE);
holder.real_price.setVisibility(TextView.GONE);
holder.total.setVisibility(TextView.VISIBLE);
holder.total_price.setVisibility(TextView.VISIBLE);
//DecimalFormat df = new DecimalFormat("0.00");
//holder.txt_distance.setText(df.format(mData.get(position).get("distance")).toString()+"...");
DecimalFormat twoDForm = new DecimalFormat("0.00");
String totalPrice = (twoDForm.format(mTotalPrice)+"").replace('.',',');
holder.total.setText("Total");
holder.total_price.setText("Kr. "+totalPrice);
}
else
{
holder.total.setVisibility(TextView.GONE);
holder.total_price.setVisibility(TextView.GONE);
if(isEdit){
/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin=30;
holder.fvtImg.setLayoutParams(lp);*/
/* TranslateAnimation anim = new TranslateAnimation(0,35,0,0);
holder.fvtImg.setAnimation(anim);
anim.setFillAfter(true);*/
holder.fvtImg.setVisibility(ImageView.GONE);
holder.name.setPadding(10,0, 0, 0);
holder.type.setPadding(10,0, 0, 0);
holder.checkImg.setVisibility(ImageView.VISIBLE);
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/list_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/price_list_background"
/>
<CheckBox android:id="#+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:visibility="gone">
</CheckBox>
<ImageView
android:id="#+id/img_pol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pol_icon_tag"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="16dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_type"
android:layout_width="200dp"
android:layout_height="13dp"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_real_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="90dp"
/>
<TextView
android:id="#+id/txt_offer_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_marginTop="31dp"
android:layout_marginLeft="190dp"
/>
<TextView
android:id="#+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Total"
android:visibility="gone"
/>
<TextView
android:id="#+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="230dp"
android:text="kr. 222.22"
android:visibility="gone"
/>
</RelativeLayout>
Have you considered using a footer instead of placing the last item inside the ListView?
What you do is, before you set the adapter of the ListView, you inflate a footer layout (or perhaps just a TextView, if that's all you need). Store a reference to it, and then add it to the ListView:
TextView myFooter = new TextView( context );
myListView.addFooterView( myFooter );
Then when you add items to the list or when the calculation needs to occur, you just calculate and set the text of myFooter.
myFooter.setText( myCalculatedValue );
Setting the text of the footer must ofcourse happen on the UI-thread so if you use a background thread or a AsyncTask to add to your listview, be sure you update stuff the right place.
I'd like to display this under a toggle button when the toggle button is pressed
then disappear when the toggle button is pressed again, I've looked into expandable list views but it's seems over complicated for a few items that wont change visually.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:textSize="40dp"
android:id="#+id/button"
android:text="Dummy Job"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- <android.support.v7.widget.RecyclerView
android:id="#+id/jobStats"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView> -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewMachine" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewItem" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewDate" />
<LinearLayout
android:id="#+id/testing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Button"
android:id="#+id/bJobOpen"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Button"
android:id="#+id/bJobEdit"
android:layout_weight="1" />
<Button
android:layout_width="245dp"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/bJobExport"
android:textSize="40dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Here is what i have for it's creation.
LinearLayout layout = (LinearLayout) findViewById(R.id.ReportsLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
//layout.setLayoutParams(lp);
//layout.setLayoutParams(lp);
//layout.setOrientation(LinearLayout.HORIZONTAL);
TextView viewMachine = new TextView(Reports.this);
TextView viewItem = new TextView(Reports.this);
TextView viewDate = new TextView(Reports.this);
LinearLayout horLayout = new LinearLayout(context);
Button enterJob = new Button(Reports.this);
Button editJob = new Button(Reports.this);
Button exportJob = new Button(Reports.this);
//enterJob.setLayoutParams(lp);
//editJob.setLayoutParams(lp);
//exportJob.setLayoutParams(lp);
if( button.isChecked()==(true)) {
layout.addView(viewMachine,lp);
layout.addView(viewItem,lp);
layout.addView(viewDate,lp);
layout.addView(enterJob,lp);
layout.addView(editJob, lp);
layout.addView(exportJob, lp);
}
else{
//layout.setVisibility(View.INVISIBLE);
}
}
};
}
Problems are when i create this it looks like this
setting the view to invis
suggestion to use this lib directly in your app.
ExpandableLayout