Add multiple lines to a LinearLayout in AlertDialog - java

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)

Related

How to set one view in vertical linearlayout in top and another in bottom programmatically?

I'm trying to create programmatically parent LinearLayout
and 3 TextViews.
One TextView must be aligned in top left, the second one in the center of parent
and the third one to the right and bottom of my screen. Everything must be done in code.
I'm almost done, but there is still some problem.
All my 3 views are on the top
My code:
public class ActivityFour extends AppCompatActivity {
private LinearLayout mLinearLayout;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private static final int TV_ID1 = 101;
private static final int TV_ID2 = 102;
private static final int TV_ID3 = 103;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams llParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(mLinearLayout, llParams);
LinearLayout.LayoutParams linlayout_params1 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linlayout_params3 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linlayout_params1.setMargins(16,16,16,16);
tv1 = new TextView(this);
tv1.setId(TV_ID1);
linlayout_params1.gravity = Gravity.START;
linlayout_params1.gravity = Gravity.TOP;
mLinearLayout.addView(tv1, linlayout_params1);
tv1.setText("TextView number ONE");
linlayout_params2.setMargins(16,16,16,16);
tv2 = new TextView(this);
tv2.setId(TV_ID2);
linlayout_params2.gravity = Gravity.CENTER;
mLinearLayout.addView(tv2, linlayout_params2);
tv2.setText("TextView number TWO");
linlayout_params3.setMargins(16,16,16,16);
tv3 = new TextView(this);
tv3.setId(TV_ID3);
linlayout_params3.gravity = Gravity.END;
mLinearLayout.addView(tv3, linlayout_params3);
tv3.setText("TextView number THREE");
}
}
After adding the weight property to all view.params it looks like this
In order to make the LinearLayout fill all the available space, you need to set the android:layout_weight attribute on the middle child View. You can do it programmatically by using a Constructor which takes the weight as third parameter:
LinearLayout.LayoutParams linlayout_params2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
First of all no need to create liner layout for all textview you can do like this:
in xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:id="#+id/test"
tools:context=".activity.TestActivity"/>
and in activity file :
LinearLayout layout = findViewById(R.id.test);
TextView product_1 = new TextView(this);
product_1.setText("Product 1");
product_1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
layout.addView(product_1);
TextView product_2 = new TextView(this);
product_2.setText("Product 2");
product_2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_2.setGravity(Gravity.CENTER);
layout.addView(product_2);
TextView product_3 = new TextView(this);
product_3.setText("Product 3");
product_3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
product_3.setGravity(Gravity.END|Gravity.BOTTOM);
layout.addView(product_3);

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 make the button wider by Java Code

I don't know what is the problem. I am creating the simple phone-book, so i have name of abonent, image and call button under the name. And when i try to set width with the layoutParams parameter for button, it doesn't change anything, btn.setWidth(); doesn't work either.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
LinearLayout.LayoutParams sublparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams childparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(200, 200);
linear.setOrientation(LinearLayout.VERTICAL);
for(int i = 0; i < 10 ; i++){
LinearLayout sublinear = new LinearLayout(this);
Button btn = new Button(this);
ImageView img = new ImageView(this);
TextView txt = new TextView(this);
img.setImageResource(R.drawable.img);
txt.setText("Abonent " + (i+1));
btn.setBackgroundColor(getColor(R.color.mygreen));
btn.setText("+3800000");
txt.setLayoutParams(childparams);
btn.setLayoutParams(imageparams);
img.setLayoutParams(imageparams);
linear.addView(txt);
sublinear.addView(img);
sublinear.addView(btn);
linear.addView(sublinear, sublparams);
}
}
}
result of programm, green vertical lines are buttons, that i cannot to make wider
your button is using LayoutParams to set width and heigth so you need to change this properties here (LinearLayout.LayoutParams(widht, height)):
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(500, 300);
and the size of your button must change, because you are setting this values.
btn.setLayoutParams(imageparams);

How to have dynamically a fixed header with scrollable content in Android?

I have created programmatically a RelativeLayout which contains a button. I have also created a ScrollView which contains a LinearLayout in which are more than 10 TextViews. I want to have the RelativeLayout to be aligned top and fixed. When someone tries to scroll down, i want all the TextViews to go behind the fixed RelativeLayout. I want that button to be always visible. With this code, the RelativeLayout and the button are not displayed. Where am i wrong?
RelativeLayout (fixed)
- Button
LinearLayout
- ScrollView
- TextView
- + other 10 TextViews
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
this.setContentView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
this.setContentView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
Thanks!
In your code you replace your RelativeLayout with ScrollView. Just set firstly some LinearLayout as contentView and then put there your RelativeLayout via addView(relativeLayout) and the put there you scrollView also via addView(scrollView)
EDIT:
your new code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout mainLinearLayout = new LinearLayout(this);
LinearLayout.LayoutParams mainLinearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
mainLinearLayout.setLayoutParams(mainLinearLayoutParams);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
this.setContentView(mainLinearLayout);
final RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
mainLinearLayout.addView(relativeLayout);
final Button restartButton = new Button(this);
restartButton.setText(R.string.restartButton);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
restartButton.setLayoutParams(buttonParams);
relativeLayout.addView(restartButton);
ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);
final LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(linearLayout.VERTICAL);
scrollView.addView(linearLayout);
TextView textView1 = new TextView(this);
testTitle.setText(R.string.text_view1);
linearLayout.addView(textView1);
// + other 10 text views
}
EDIT 2: renamed first linearLayout to mainLinearLayout according to the comment

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