How to use two column ScrollView? - android

I want a 2-column ScrollView. In each column, there should be an ImageButton:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_height="800dp"
android:background="#FFF"
android:layout_width="600dp" >
<LinearLayout
android:id="#+id/categoryLinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
And the code:
LinearLayout sv = (LinearLayout) findViewById(R.id.categoryLinearLayout1);
for (int i = 0; i < 10; i++) {
ImageButton ib = new ImageButton(this);
// ib.setImageDrawable(getResources().getDrawable(R.drawable.cat1));
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.cat1);
int width = 300;
int height = 300;
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width,
height, true);
ib.setImageBitmap(resizedbitmap);
sv.addView(ib);
}
But in this way, all 10 ImageButtons horizontally. What I need is, put 2 ImageButton in a row (it makes 600px) and go down, put more 2 ImageButtons etc. So there will be 5 rows for 10 ImageButtons.
How can I do that?

Use a TableLayout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_height="800dp"
android:background="#FFF"
android:layout_width="600dp" >
<TableLayout
android:id="#+id/categoryLinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
Then in your code:
TableLayout sv = (TableLayout) findViewById(R.id.categoryLinearLayout1);
for (int i = 0; i < 5; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 2; j++) {
ImageButton ib = new ImageButton(this);
// ib.setImageDrawable(getResources().getDrawable(R.drawable.cat1));
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.cat1);
int width = 300;
int height = 300;
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width,
height, true);
ib.setImageBitmap(resizedbitmap);
ib.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(ib);
}
sv.add(tr);
}

Related

params.setMargins does not work programmatically

I set layout_marginStart and layout_marginTop in xml and it is ok, but when I try to set programmatically margins it does not work. I try to find how to resolve, but can not find resolution.
What I do not correctly?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
android:weightSum="3"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_resourses">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyGridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:background="#color/borderHigh"
android:columnCount="2"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/firstLinear1"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/firstLinear2"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
</GridLayout>
</ScrollView>
XML above works
I try to do this by code, and it must work but i can not resolve this problem
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int dp9 = dpToPx(9);
GridLayout MyGridLayout = findViewById(R.id.MyGridLayout);
final int N = 10; // total number of textviews to add
for (int i = 0; i < N; i++) {
LinearLayout testlayout = new LinearLayout(this);
//LinearLayout.LayoutParams tst = new LinearLayout.LayoutParams(0,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.height = (width/2)-dp9;
params.width = (width/2)-dp9;
params.setMargins(6, 6, 0, 0);
testlayout.setLayoutParams(params);
testlayout.setOrientation(LinearLayout.VERTICAL);
testlayout.setBackgroundResource(R.drawable.resource_panel);
TextView title = new TextView(this);
title.setText("TextView "+i);
title.setTextColor(getResources().getColor(R.color.colorPrimary2));
title.setAllCaps(true);
title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(title, 10, 24, 1, TypedValue.COMPLEX_UNIT_SP);
testlayout.addView(title);
TextView text = new TextView(this);
text.setText("Text "+i);
testlayout.addView(text);
TextView date = new TextView(this);
date.setText("date "+i);
testlayout.addView(date);
}

Create checkbox dynamically with weight attribute

I want to create checkboxes in java with for loop. i want to add it in gridlayout.
my problem is that checkbox take only wrap content size. I want that they should cover full width.
I can easily get it in xml like this.
<GridLayout
android:id="#+id/checkBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:columnCount="2">
<CheckBox
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_columnWeight="1"
android:text="dfdfdffddfd"/>
<CheckBox
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_columnWeight="1"
android:text="dfdfdffddfd"/>
<CheckBox
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_columnWeight="1"
android:text="dfdfdffddfd"/>
</GridLayout>
but i want add these like this
<GridLayout
android:id="#+id/checkBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:columnCount="2">
</GridLayout>
and Java code
for (int i = 0; i < 3; i++) {
CheckBox cb = new CheckBox(getActivity());
cb.setText("I'm dynamic!");
cb.setTextColor(Color.WHITE);
cb.setPadding(10, 10, 10, 10);
checkBoxLayout.addView(cb);
}
Something like:
final int h = 25;
final int w = 200;
final int margin = 10;
checkBoxLayout.setColumnCount(3);
checkBoxLayout.setRowCount(1);
for( i = 0 ; i < 3 ; i++ ) {
CheckBox check = new CheckBox( getActivity() );
check.setText("Checkout");
// check.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// left, top, right, bottom
params.setMargins(0, margin , 0, margin);
params.gravity = Gravity.NO_GRAVITY;
check.setLayoutParams(params);
check.setGravity(Gravity.CENTER);
checkBoxLayout.addView(check, w, h);
}

Make square buttons in TableLayout

I am using the following code to generate a table with button in each cell.
<?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" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
</TableLayout>
</LinearLayout>
And this is the function that generates the table
private void BuildTable(int rows, int cols) {
// outer for loop
for (int i = 1; i <= rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 1; j <= cols; j++) {
Button bt = new Button(this);
LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
bt.setLayoutParams(lp);
bt.setText("R"+i+" ,C"+j);
row.addView(bt);
}
table_layout.addView(row);
}
}
Which gives me the following:
I need your help to make the height of each button the same as its width. (As squares)
Thanks

Dynamically added items in LinearLayout margin

I have code to add checkboxes from an Array to the LinearLayout.
LinearLayout my_layout = (LinearLayout) findViewById(R.id.test);
for (int n = 0; n < listitems.size(); n++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(Integer.parseInt(listitems.get(n).get("cbid")));
cb.setText(listitems.get(n).get("product"));
cb.setTextColor(Color.BLACK);
my_layout.addView(cb);
}
how can i make sure that between each checkbox there is a margin of 2-3dp?
And that the background of the checkboxes has rounded edges?
This is my XML to set the boxes in
<LinearLayout
android:id="#+id/Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/test"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp" />
<LinearLayout
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#13ca8c"
/>
</LinearLayout>
LinearLayout my_layout = (LinearLayout) findViewById(R.id.test);
for (int n = 0; n < listitems.size(); n++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(Integer.parseInt(listitems.get(n).get("cbid")));
cb.setText(listitems.get(n).get("product"));
cb.setTextColor(Color.BLACK);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
parms.leftMargin = 2;
params.rightMargin = 2;
params.topMargin = 2;
params.bottomMargin = 2;
my_layout.addView(cb,params);
}
And for rounded corners use a background image

BackgroundColor in LinearLayout of the width of the screen

I created a gallery with horizontalscrollview. In the imageview LinearLayout I've put a color background. I want the LinearLayout spans the width of the screen with or without images. How I can do?
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/estandar"
android:padding="2dp"
/>
</HorizontalScrollView>
</LinearLayout>
main.xml
LinearLayout linear2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = linear2 .getLayoutParams();
params.height = 100;
params.width = 300;
but my code does not work.
EDIT:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int anchoPantalla = metrics.widthPixels;
if (listaImagenes.length > 0) {
for (String nombreImagen : listaImagenes) {
InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
final Bitmap bitmap = BitmapFactory.decodeStream(is);
final ImageView imageView = new ImageView(getApplicationContext());
alto = bitmap.getHeight();
ancho = bitmap.getWidth();
final float calculo = ancho / (alto / ALTO_IMAGEN);
imageView.setLayoutParams(new ViewGroup.LayoutParams((int)calculo, ALTO_IMAGEN));
imageView.setImageBitmap(bitmap);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundColor(colorResources);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
linear2.addView(imageView);
}
} else {
// PAINT LINE WIDTHSCREEN x 120dp
}
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = galeria2.getLayoutParams();
params.height = 100;
params.width = 300;
galeria2.setLayoutParams(params);
You have to dynamically add width to each element of HorizontalScrollView. I have modified your code have a look. Its working fine.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FF0040"
android:padding="2dp"
/>
</HorizontalScrollView>
Now in code add element like (see the highlited line)
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.ic_launcher);
**iv1.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
ImageView iv2 = new ImageView(this);
**iv2.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
galeria2.addView(iv1);
galeria2.addView(iv2);

Categories

Resources