linearLayout.addView(TextView) not working - java

private LinearLayout clmnView;
LinearLayout clmnView = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = DimensionsHelper.getMargins(getContext(), DimensionsHelper.DimensionType.BALL);
params.setMargins(0, margin, 0, margin);
clmnView.setLayoutParams(params);
clmnView.setOrientation(LinearLayout.VERTICAL);
clmnView.setGravity(Gravity.CENTER_VERTICAL);
TextView historyTextView = new TextView(getContext());
historyTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
historyTextView.setText("this is history");
clmnView.addView(historyTextView);
No matter what I do ,the historyTextView is not display inside my clmnView.
Could you please help? Thank you in advance.

Okay everything was working as it should. But I was displaying white text in white background.
Changed the color of the text like this :
historyTextView.setTextColor(Color.parseColor("#000000"));
and my text appeared.

Related

problem in creating textview programmatically

I'm try to create programmatically textView for a project. My problem is that I don't now how to set up the position of the textview on the screen.
I tried with the code below but instead of moving the textview it moves all the margin. The code is this:
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint);
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(50);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
constraintLayout.getLayoutParams();
params.setMargins(100, 100, 100, 100);
valueTV.setLayoutParams(params);
constraintLayout.addView(valueTV);
the code is inside the oncreate. Thanks for help
You can try something like that:
TextView valueTV = new TextView(getApplicationContext());
valueTV.setText("TEST");
valueTV.setTextColor(R.color.black);
valueTV.setId(id);
valueTV.setHeight(50);
valueTV.setWidth(100);
constraintLayout.addView(valueTV);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) valueTV.getLayoutParams();
layoutParams.rightToRight = constraintLayout.getId();
layoutParams.topToTop = constraintLayout.getId();
layoutParams.bottomToBottom = constraintLayout.getId();
layoutParams.leftToLeft = constraintLayout.getId();
valueTV.setLayoutParams(layoutParams);

Add multiple lines to a LinearLayout in AlertDialog

In my app, I have a button that when pressed, displays an AlertDialog with the question to add an entry with two NumberPickers, one for Minutes and the other for seconds. I would like to display a TextView with "Min" above the minutes one, and one with "Sec" above the seconds one. The Java file looks like this:
//create linearlayout for display on the alertdialog
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.HORIZONTAL);
//create upper and lower LinearLayout
LinearLayout upper = new LinearLayout(this);
LinearLayout lower = new LinearLayout(this);
upper.setOrientation(LinearLayout.HORIZONTAL);
lower.setOrientation(LinearLayout.HORIZONTAL);
//create parameters for the lower and upper linearLayouts
LinearLayout.LayoutParams upParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams downParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
//Create numberPicker and TextView for the Seconds
final NumberPicker secondsPicker = new NumberPicker(this);
secondsPicker.setMinValue(MIN_SECOND_INPUT);
secondsPicker.setMaxValue(MAX_SECOND_INPUT);
final TextView secondsText = new TextView(this);
secondsText.setText(R.string.Seconds);
//Create numberpicker and TextView for the minutes
final NumberPicker minutesPicker = new NumberPicker(this);
minutesPicker.setMinValue(MIN_MINUTE_INPUT);
minutesPicker.setMaxValue(MAX_MINUTE_INPUT);
final TextView minutesText = new TextView(this);
minutesText.setText(R.string.Minutes);
//create parameters for the LinearLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
//create parameters for the TextView for seconds
LinearLayout.LayoutParams paramsTextSec = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsTextSec.weight = 1;
//create parameters for the TextView for minutes
LinearLayout.LayoutParams paramsTextMin = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsTextMin.weight = 1;
//create parameters for the numberpicker for the seconds
LinearLayout.LayoutParams paramsSeconds = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsSeconds.weight = 1;
//create parameters for the numberpicker for the Minutes
LinearLayout.LayoutParams paramsMinutes = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsMinutes.weight = 1;
//add parameters to linear layout
parent.setLayoutParams(params);
upper.setLayoutParams(upParams);
lower.setLayoutParams(downParams);
upper.addView(secondsPicker, paramsSeconds);
upper.addView(secondsText, paramsTextSec);
lower.addView(minutesPicker, paramsMinutes);
lower.addView(minutesText, paramsTextMin);
parent.addView(upper);
parent.addView(lower);
//create alertdialog with the linear layout with numberpickers
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add an extra slot:");
builder.setView(parent);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
int seconds = secondsPicker.getValue() + minutesPicker.getValue() * 60;
addValueToScheme(seconds);
}
});
AlertDialog dialog = builder.create();
dialog.show();
The AlertDialog gives a result where the Seconds appear next to the NumberPicker and the NumberPickers appear under each other.
How can I add the NumberPickers next to each other with the text above them?
first of all: please, move this Java code do XML, it would be much more easier to read, design and develop...
add weight=1 for upParams and downParams
LinearLayout.LayoutParams upParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT);
upParams.weight = 1;
LinearLayout.LayoutParams downParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT);
downParams.weight = 1;
this will cause LinearLayout upper to be on the left and LinearLayout lower on the right (thanks to parent orientation HORIZONTAL), so change their names... then set their orientation for VERTICAL
upper.setOrientation(LinearLayout.VERTICAL);
lower.setOrientation(LinearLayout.VERTICAL);
and now you have two vertical containers, left and right, with equal width, and you can add to both some elements, e.g. at first TextView, after that NumberPicker, both with width MATCH_PARENT to fulfil (half of width of whole Dialog)

How to set margin on view programmatically?

I am new to android development and I am trying to add margins to my view but I have not been able to get it to work.
Here's my code:
ConstraintLayout layout = new ConstraintLayout(this);
final float scale = getResources().getDisplayMetrics().density;
layout.setMinHeight((int)(100*scale));
layout.setMaxHeight((int)(100*scale));
CircleImageView icon = new CircleImageView(this);
icon.setImageResource(image);
icon.setBorderWidth(3);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)(90*scale), (int)(90*scale));
icon.setLayoutParams(layoutParams);
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(icon.getLayoutParams());
marginParams.setMargins(100, 0, 0, 0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(marginParams);
icon.setLayoutParams(params);
layout.addView(icon);
Why isn't this working? Thanks!
You should do like this:
LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
param.setMargins(left, top, right, bottom);
view.setLayoutParams(param);
After you have created the icon, you should first generate an id for it:
icon.setId(View.generateViewId());
Then, remove the setMargins line and add these lines below layout.addView(icon):
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.constrainWidth(icon.getId(), (int)(90*scale));
set.constrainHeight(icon.getId(), (int)(90*scale));
set.connect(icon.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(icon.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 100);
set.applyTo(layout);
I haven't tested it but it should be a step in the right direction.
Let me know how it goes.

How to align vertical textview and editText in Linearlayout in android

Currently, I am trying to code a textview and a edittext to align vertically using Linearlayout in android studio. I tried setting out the layout gravity and the other element but I cannot achieve what I want currently this is my code.
LinearLayout params3 = new LinearLayout(this);
params3.setOrientation(LinearLayout.VERTICAL);
params3.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.weight = 35;
params3.setLayoutParams(params2);
params2.height = convertDiptoPix(80,context);
//params2.leftMargin = convertDiptoPix(5,context);
TextView textView1 = new TextView(this);
textView1.setId(View.generateViewId());
textView1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
textView1.setPadding(0,convertDiptoPix(1,context),0,convertDiptoPix(3,context));
textView1.setText("Receipt");
textView1.setTextColor(Color.parseColor("#000000"));
//textView1.setLayoutParams(params2);
textView1.setTextSize(convertDiptoPix(13,context));
EditTextIME editTextIME = new EditTextIME(this);
editTextIME.setId(View.generateViewId());
//editTextIME.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
//editTextIME.setMinimumHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
editTextIME.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
//editTextIME.setLayoutParams(params2);
editTextIME.setFocusableInTouchMode(true);
editTextIME.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzöäüßñéúíóáèùòìàâãåæçëêìíîïôõûýABCDEFGHIJKLMNOPQRSTUVWXYZÖÄÜÕÔÒÔÓÙÚÛÝÑÇÈÉÊËÌÍÎÏÆÅÄÃÂÁÀØ01234567890()&*$%#!+-:;,._?¿/><[]{}|\\^ "));
editTextIME.setHint("Notes");
editTextIME.setInputType(InputType.TYPE_CLASS_TEXT);
editTextIME.setHorizontallyScrolling(true);
editTextIME.setTextSize(convertDiptoPix(13,context));
is anyone had anyone tried to code like vertical align in android studio? Can you share your idea?
You can try this,
LinearLayout parent = new LinearLayout(context);
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
parent.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(params2);
textView1.setId(View.generateViewId());
textView1.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
textView1.setText("Receipt");
textView1.setTextColor(Color.parseColor("#000000"));
EditText editTextIME = new EditText(this);
editTextIME.setId(View.generateViewId());
editTextIME.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL);
editTextIME.setLayoutParams(params2);
editTextIME.setFocusableInTouchMode(true);
editTextIME.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzöäüßñéúíóáèùòìàâãåæçëêìíîïôõûýABCDEFGHIJKLMNOPQRSTUVWXYZÖÄÜÕÔÒÔÓÙÚÛÝÑÇÈÉÊËÌÍÎÏÆÅÄÃÂÁÀØ01234567890()&*$%#!+-:;,._?¿/><[]{}|\\^ "));
editTextIME.setHint("Notes");
editTextIME.setInputType(InputType.TYPE_CLASS_TEXT);
editTextIME.setHorizontallyScrolling(true);
// editTextIME.setTextSize(convertDiptoPix(13,context));
parent.addView(textView1);
parent.addView(editTextIME);

Cannot retrieve the integer getHeight()

I have created a Layout 'layout' programmatically, then I created a TextView in the same layout to display the layout height, there are no other methods or anything else in the class. Unfortunately the app closes, and I can't identify why.
LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;
TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(layout.getHeight());
layout.addView(fimText, lparams);
setContentView(layout);
Your LinearLayout needs to be added to a container before you can call getHeight on it. Also, getHeight returns an int so you need to convert it to a String.
Try this:
LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;
setContentView(layout);
TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(String.valueOf(layout.getHeight()));
layout.addView(fimText, lparams);
This is because layout.getHeight() will not return an CharSequence value. Add .toString() or ""+ to it.

Categories