I am trying to set padding to my linear layouts so that when I press 'create row' they have a gap between them each.
I tried adding marginTop and marginBottom to my LinearLayout but when I run the code, the layout is still spawn in together. not with a gap from the margin I set.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/verticalLayout"
android:layout_below="#id/button"
android:background="#color/colorPrimaryDark"
android:layout_marginTop="50dp"
>
(the above image shows 3 linear layouts I added by pressing create row)
Edit: I tried adding a marginRight to it, just to see if anything worked on it, and it did.
public class MainActivity extends AppCompatActivity {
LinearLayout verticallayout,horizontalLayout; //got
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticallayout = findViewById(R.id.verticalLayout); //got
}
public void createRow(View view){
createRow(); //got
}
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
private void createEditText() {
EditText editText = new EditText(this);
LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editParams.setMargins(10, 10, 10, 10);
editText.setLayoutParams(editParams);
horizontalLayout.addView(editText);
}
private void createCheckbox() { //done
CheckBox checkBox = new CheckBox(this);
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.LEFT;
checkBox.setLayoutParams(checkParams);
horizontalLayout.addView(checkBox);
}
private void createSpinner() {
Spinner spinner = new Spinner(this);
//ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
//spinner.setAdapter(spinnerArrayAdapter);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.setMargins(10, 10, 10, 10);
spinnerParams.gravity = Gravity.LEFT;
spinner.setLayoutParams(spinnerParams);
horizontalLayout.addView(spinner);
}
}
Try this one code
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
horizontalLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
hope so it will be useful for you. :)
Add margin to horizontal layout not in Vertical layout. check following code:
private void createRow() {
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalParams.topMargin = 20; //add margin here.
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout);
}
Related
I have to create like flip cart main page, so I need use more than one horizontal view how to create that programmatically in android. In that horizontal view inside Vertical orientation using images, price, product name will display in android.
Thank in Advance
I just created Like this as mention below code:
public void shopimages()
{
/* GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,new int[] {R.color.shadow,Color.WHITE,R.color.shadow});
drawable.setShape(GradientDrawable.RECTANGLE);*/
// drawable.setStroke(3, Color.BLACK);
//drawable.setCornerRadius(8);
// drawable.setColor(0xFFFFFFFF); //white background
// border.setStroke(1, 0xFF000000);
// categories = getResources().getStringArray(R.array.categories);
/* child layout of horizontal*/
LinearLayout[] lLayout = new LinearLayout[8];
for(int j=0;j<lLayout.length;j++ ) {
lLayout[j] = new LinearLayout(getActivity());
LinearLayout li=new LinearLayout(getActivity());
li.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
li.setOrientation(LinearLayout.VERTICAL);
/* Heading text for each horizontal images*/
name=new TextView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0,150,0,0);
name.setLayoutParams(params);
name.setBackgroundColor(Color.parseColor("#FFFFFF"));
name.setText("kani"+j);
name.setPadding(10,10,10,10);
name.setTextColor(Color.parseColor("#ff1319ff"));
li.addView(name);
li.setTop(50);
root.addView(li);
/*end*/
/* child layout created dynamically*/
// lLayout[j].setBackgroundDrawable(drawable);
sv = new HorizontalScrollView(getActivity());
sv.setHorizontalScrollBarEnabled(false);
lLayout[j].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
lLayout[j].setOrientation(LinearLayout.HORIZONTAL);
sv.addView(lLayout[j]);
/*end*/
t = new TextView[10];
names=new TextView[10];
price=new TextView[10];
existingprice=new TextView[10];
img = new ImageView[10];
/*create the layout for textveiw and image view*/
LinearLayout.LayoutParams image=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
image.topMargin=50;
image.leftMargin=15;
image.rightMargin=15;
image.bottomMargin=10;
image.width=200;
image.height=150;
LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dim.topMargin=20;
dim.leftMargin=15;
dim.rightMargin=15;
dim.bottomMargin=10;
dim.width=200;
dim.height=80;
LinearLayout.LayoutParams dim2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dim2.leftMargin=40;
dim2.width=200;
LinearLayout.LayoutParams dim3 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
/*end*/
/*using for loop to run the rexxtview*/
for (int i = 0; i < 10; i++) {
LinearLayout imageVertical =new LinearLayout(getActivity());
imageVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
imageVertical.setOrientation(LinearLayout.VERTICAL);
img[i] = new ImageView(getActivity());
img[i].setPadding(8, 8, 8, 8);
img[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
img[i].setImageResource(mThumbIds[i]);
t[i] = new TextView(getActivity());
img[i].setLayoutParams(image);
t[i].setLayoutParams(dim);
t[i].setGravity(Gravity.CENTER);
t[i].setTextSize(14);
t[i].setTextColor(Color.parseColor("#ff1319ff"));
t[i].setText("YOHOHO: " + i);
// name
names[i] = new TextView(getActivity());
names[i].setLayoutParams(dim2);
names[i].setGravity(Gravity.CENTER);
names[i].setTextSize(14);
names[i].setTextColor(Color.parseColor("#ff1319ff"));
/*names[i].setEllipsize (TextUtils.TruncateAt.END);*/
names[i].setText("lightweight" + i);
//horizontal linear layout
LinearLayout pricelayout=new LinearLayout(getActivity());
pricelayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
pricelayout.setOrientation(LinearLayout.HORIZONTAL);
//price
price[i] = new TextView(getActivity());
// price[i].setLayoutParams(dim3);
price[i].setGravity(Gravity.CENTER);
price[i].setTextSize(9);
price[i].setTextColor(Color.parseColor("#ff1319ff"));
price[i].setText("Rs.2000");
// existing price
existingprice[i] = new TextView(getActivity());
/*existingprice[i].setLayoutParams(dim3);*/
existingprice[i].setGravity(Gravity.CENTER);
existingprice[i].setTextSize(9);
existingprice[i].setTextColor(Color.parseColor("#ff1319ff"));
existingprice[i].setPaintFlags(existingprice[i].getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
existingprice[i].setText("Rs.5340");
pricelayout.addView(price[i]);
pricelayout.addView(existingprice[i]);
imageVertical.addView(img[i]);
imageVertical.addView(t[i]);
imageVertical.addView(names[i]);
imageVertical.addView(pricelayout);
// imageVertical.addView(existingprice[i]);
lLayout[j].addView(imageVertical);
}
/*end*/
/* parent layout we can add the child view */
root.addView(sv);
}
Sorry for my English. I have a dynamically created TextView and ImageView, they are added to the LinearLayout. But when I start to write a TextView that it changes its size and thus reduces the ImageView. The picture below is an example of how the size of the
final LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
row.setOrientation(LinearLayout.HORIZONTAL);
//params editTExt
android.widget.LinearLayout.LayoutParams paramsEtext = new android.widget.LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
paramsEtext.gravity = Gravity.LEFT;
//params image View
LinearLayout.LayoutParams paramsImageView = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.25f);
paramsImageView.gravity = Gravity.CENTER_VERTICAL;
//create edit text
final EditText etext = new EditText(QuicklyCreateQwestions.this);
etext.setLayoutParams(paramsEtext);
//add et in ArrayList
et.add(etext);
//set text in textView
etext.setText(qwestions.get(i).toString());
row.addView(etext);
//add ImageView in edit text
final ImageView imV = new ImageView(this);
imV.setLayoutParams(paramsImageView);
imV.setImageResource(R.drawable.t_del_poll);
imV.setId(i);
//set button in layaut
row.addView(imV);
//action if button click
imV.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
row.removeView(etext);
row.removeView(imV);
}
});
//add editText and button in layaut
LinearLayoutTextQwestions.addView(row);
I have this code below that creates a square with an image, textview and 2 image buttons.
It looks like this:
I want it so that the text is always in 1 line. The width of the square needs to expand so it includes the full text. In the image, the text gets cropped. The two little buttons (info and settings) needs to be in the corners of the square. Also there is another problem, the text is not vertically centered and it should be.
I can't quite get it to look right. Does anyone know how to fix this?
Thanks
public static LinearLayout createNodeWrapper(Activity context, Fish fish) {
ImageView imageview = createImageView(context, fish);
TextView nametv = createNameView(context, fish.getName());
ImageButton info = createButton(context, R.drawable.ic_action_info);
ImageButton settings = createButton(context, R.drawable.ic_action_settings);
LinearLayout namewrapper = createNameWrapper(context);
namewrapper.addView(info);
namewrapper.addView(nametv);
namewrapper.addView(settings);
LinearLayout contentwrapper = createContentWrapper(context, fish);
contentwrapper.addView(imageview);
contentwrapper.addView(namewrapper);
LinearLayout fullwrapper = createFullWrapper(context, fish);
fullwrapper.addView(contentwrapper);
return fullwrapper;
}
private static ImageView createImageView(Context context, Fish fish) {
ImageView imageview = new ImageView(context);
LinearLayout.LayoutParams params = fish.getImageParams();
params.gravity = Gravity.CENTER;
params.setMargins(20, 20, 20, 20);
imageview.setLayoutParams(params);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageview;
}
private static TextView createNameView(Context context, String name) {
TextView nametv = new TextView(context);
nametv.setText(name);
nametv.setTextAppearance(context, R.style.node);
nametv.setPadding(10, 0, 10, 10);
nametv.setGravity(Gravity.CENTER);
nametv.setSingleLine(true);
LinearLayout.LayoutParams textparams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textparams.gravity = Gravity.CENTER;
nametv.setLayoutParams(textparams);
return nametv;
}
private static ImageButton createButton(Context context, int resId) {
ImageButton imageButton = new ImageButton(context);
imageButton.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageparams.gravity = Gravity.CENTER;
imageButton.setLayoutParams(imageparams);
imageButton.setBackgroundResource(resId);
return imageButton;
}
private static LinearLayout createNameWrapper(Context context) {
LinearLayout wrapper = new LinearLayout(context);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
wrapper.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
wrapper.setLayoutParams(params);
return wrapper;
}
private static LinearLayout createContentWrapper(Context context, Fish fish) {
LinearLayout wrapper = new LinearLayout(context);
wrapper.setOrientation(LinearLayout.VERTICAL);
wrapper.setPadding(3, 0, 3, 3);
LinearLayout.LayoutParams params = fish.getParams();
wrapper.setLayoutParams(params);
wrapper.setMinimumWidth(MIN_NODE_WIDTH);
return wrapper;
}
private static LinearLayout createFullWrapper(Context context, Fish fish) {
LinearLayout wrapper = new LinearLayout(context);
wrapper.setBackgroundResource(R.drawable.category_bg);
wrapper.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = fish.getParams();
params.setMargins(10, 10, 10, 10);
wrapper.setLayoutParams(params);
return wrapper;
}
For the text, try this answer: https://stackoverflow.com/a/2142968/2629065 it will scale down the text until it fits the parameters you give it.
You padded the top, which adds to the height of the item, offsetting the gravity alignment. Remove the top padding and use a margin, or pad the bottom.
I have this code, but the textview are all without Margin right and i don't know why.
I add some lines of onCreate for declaration.
And the functions about the creation of the textview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearlayout);
mEditText = (EditText) findViewById(R.id.input_materia);
mButton = (Button) findViewById(R.id.add_materia);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
/*
* add textview from editext
*/
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 30, 0);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setGravity(Gravity.CENTER);
return textView;
}
some helps?
Have you tried referencing Relative Layout Params or Linear Layout Params depending on what is set in your XML?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
i'm writing progrmicaly adding Linearlayout and Textview into ScrollView. but my ScrollView could not scroll to height.
My programicall Code:
if (cursor.moveToFirst()) {
do {
String last_ID = cursor.getString(cursor.getColumnIndex("lastId"));
String smsBody = cursor.getString(cursor.getColumnIndex("smsBody"));
String senderName = cursor.getString(cursor.getColumnIndex("senderName"));
String date[] = cursor.getString(cursor.getColumnIndex("receiveDate")).split("/");
CalendarTool ct =
new CalendarTool(
Integer.valueOf(date[0]),
Integer.valueOf(date[1]),
Integer.valueOf(date[2])
);
String IranianDate = ct.getIranianDate();
ScrollView SV =new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.parseColor("#f7cbad"));
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 10);
ll.setLayoutParams(params);
TextView TV_IranianDate = new TextView(this);
TV_IranianDate.setText(IranianDate);
TV_IranianDate.setTextColor(Color.parseColor("#ffffff"));
TV_IranianDate.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
ll.addView(TV_IranianDate);
TextView TV_smsBody = new TextView(this);
TV_smsBody.setText(smsBody);
TV_smsBody.setGravity(Gravity.RIGHT);
TV_smsBody.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
TV_smsBody.setPadding(5, 5, 5, 5);
ll.addView(TV_smsBody);
TextView TV_spacer2 = new TextView(this);
TV_spacer2.setBackgroundColor(Color.parseColor("#000000"));
TV_spacer2.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
1)
);
ll.addView(TV_spacer2);
SV.addView(ll);
((LinearLayout) linearLayout).addView(SV);
} while (cursor.moveToNext());
}
I guess the problem is the ScrollView object. You should use HorizontalScrollView instead, like this for example:
HorizontalScrollView SV = new HorizontalScrollView(this);
SV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
like described in the API:
ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.