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
Related
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);
}
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);
}
I had a ListView created programmatically, and I need to display only ONE visible row between the red borders not multiple items, and scroll to get the others.
This my function
public void fillFormules(List<Category> objectsTextToShow)
{
LinearLayout layoutItemDetail = (LinearLayout) view.findViewById(R.id.middle);
LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.titles);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j <objectsTextToShow.size() ; j++) {
TextView textCat = new TextView(getActivity());
textCat.setText(Check.Languages(objectsTextToShow.get(j).getName(), LANGUAGE_ID));
textCat.setTextSize(28);
textCat.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
textCat.setTextColor(colorUtils.TITLE);
textCat.setGravity(Gravity.CENTER);
relativeLayout.addView(textCat);
textCat.setBackgroundColor(colorUtils.backgroundColor);
LinearLayout parentLayout = new LinearLayout(getActivity());
parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0));
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
List<Item> items = new ArrayList<Item>();
ListView listView = new ListView(getActivity());
for (int i = 0; i <objectsTextToShow.get(j).getItems().size() ; i++) {
items.add(objectsTextToShow.get(j).getItems().get(i));
}
listView.setAdapter(new ScrollAdapter(getActivity(), items));
parentLayout.addView(listView);
layoutItemDetail.addView(parentLayout);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
I use this xml to inflate the view, even I give match parent fot the weight and the height but it doesn't work :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="25dp"
android:text="TextView" />
This what I must have in the end but I don't know how to do it :/
You can set a fix height to your Linearlayout used to display every item of the list. And put also the same height to your listView
LinearLayout parentLayout = new LinearLayout(getActivity());
parentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, yourHeight, (float) 1.0));
And in your LinearLayout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="yourHeight"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="25dp"
android:text="TextView" />
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
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);
}