Android AlertDialog Builder multiple EditText - android

I am wanting to use Android AlertDialog.Builder to programatically structure the layout of a dialog. I have two EditText fields that I want to display, displayed vertically, one on top of another, but I can't get it to work. The following code just displays the second one, as if it is being displayed over the first one linearly instead of vertically.
final EditText inputOne = new EditText(MainActivity.this);
final EditText inputTwo = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
inputOne.setLayoutParams(lp);
inputOne.setLayoutParams(lp);
alertDialog.setView(inputOne);
alertDialog.setView(inputTwo);

Take a parent Layout and add the the views and finally set the view to dialog like this way:
LinearLayout parent = new LinearLayout(this);
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.VERTICAL);
parent.addView(inputOne);
parent.addView(inputTwo);
alertDialog.setView(parent);

Related

How to make two buttons on the same row in eclipse programmatically?

LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
btnControl = new Button(this);
btnControl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnControl.setPadding(0,40,0,40);//x,y,w,h
btnControl.setBackgroundColor(Color.parseColor("#1C344E"));
btnControl.setTextColor(Color.WHITE);
setMargins(btnControl, 50, 50, 50, 50);
btnLogs = new Button(this);
btnLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
btnLogs.setPadding(0,40,0,40);//x,y,w,h
btnLogs.setBackgroundColor(Color.parseColor("#1C344E"));
btnLogs.setTextColor(Color.WHITE);
setMargins(btnLogs, 50, 50, 50, 50);
btnLogs.setId(5);
btnLogs.setOnClickListener(this);
btnTermLogs = new Button(this);
btnTermLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
btnTermLogs.setPadding(0,40,0,40);//x,y,w,h
btnTermLogs.setBackgroundColor(Color.parseColor("#CC6675"));
btnTermLogs.setTextColor(Color.WHITE);
setMargins(btnLogs, 50, 50, 50, 50);
btnTermLogs.setId(6);
btnTermLogs.setOnClickListener(this);
btnLogout = new Button(this);
btnLogout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
btnLogout.setPadding(0,40,0,40);//x,y,w,h
btnLogout.setBackgroundColor(Color.parseColor("#CC6675"));
btnLogout.setTextColor(Color.WHITE);
table.addView(btnLogs);
table.addView(btnTermLogs);
I want the two buttons to be aligned together horizontally. However it just align themselves vertically which i dont want.
My buttons look like this:
what i want is to look like this:
Try this below function. It will help
public void addButtons(){
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams LayoutParamsview = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(this);
button1.setText("Btn 1");
button1.setLayoutParams(LayoutParamsview);
linearLayout1.addView(button1);
table.addView(linearLayout1);
//
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
Button button2 = new Button(this);
button2.setText("Btn 2");
button2.setLayoutParams(LayoutParamsview);
Button button3 = new Button(this);
button3.setText("Btn 3");
button3.setLayoutParams(LayoutParamsview);
linearLayout2.addView(button2);
linearLayout2.addView(button3);
table.addView(linearLayout2);
//
LinearLayout linearLayout3 = new LinearLayout(this);
linearLayout3.setOrientation(LinearLayout.VERTICAL);
Button button4 = new Button(this);
button4.setText("Btn 4");
button4.setLayoutParams(LayoutParamsview);
linearLayout3.addView(button4);
table.addView(linearLayout3);
}
Horizontal linear layout supports left to right widgets approach, When
app developer selects horizontal orientation in linear layout then it
will automatically start adding components and widgets to left to
right side after adding them. So here is the complete step by step
tutorial for Creating horizontal linearlayout programmatically
android.
Write this code
//Creating LinearLayout.
LinearLayout Horizontallinearlayout = new LinearLayout(this);
//Setting up LinearLayout Orientation
Horizontallinearlayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams Horizontallinearlayoutlayoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
setContentView(Horizontallinearlayout, Horizontallinearlayoutlayoutparams);
LayoutParams LayoutParamsview = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Creating textview .
Button button1 = new Button(this);
Button button2 = new Button(this);
//Adding text to TextView.
button1.setText("First Button");
button2.setText("Second Button");
button1.setLayoutParams(LayoutParamsview);
button2.setLayoutParams(LayoutParamsview);
//Adding textview to linear layout using Add View function.
Horizontallinearlayout.addView(button1);
Horizontallinearlayout.addView(button2);
Hope this helps you
You should use setLayoutParams
Add setOrientation(LinearLayout.HORIZONTAL);
Set the layout parameters associated with this view. These supply
parameters to the parent of this view specifying how it should be
arranged. There are many subclasses of ViewGroup.LayoutParams, and
these correspond to the different subclasses of ViewGroup that are
responsible for arranging their children.
Code Structure
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
table.setOrientation(LinearLayout.HORIZONTAL);
Button Btn1 = new Button(Activity.this);
Btn1.setText("Button 1");
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam.weight = 0.5f;
Btn1.setLayoutParams(childParam);
Button Btn2 = new Button(Activity.this);
Btn2.setText("Button 2");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam2.weight = 0.5f;
Btn2.setLayoutParams(childParam2);
table.addView(Btn1);
table.addView(Btn2);
Respected OP
You can create another XML file, that contains a horizontal LinearLayout, then inflate it and insert the two buttons into it, and then add the whole horizontal LinearLayout to the vertical LinearLayout:
Create an XML file e.g. horizontal_layout.xml:
<LinearLayout
android:id="#+id/horizontal_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
// You will insert the buttons programmatically.
</LinearLayout>
In your Activity use this method to inflate the Layout:
LinearLayout verticalLinearLayout = rootView.findViewById(R.id.vertical_linear_layout);
// Add the first vertical Button regularly.
Button firstVerticalButton = new Button(Activity.this);
firstVerticalButton.setText("Vertical");
verticalLinearLayout.addView(firstVerticalButton);
// Inflate the horizontal LinearLayout and add the two horizontal buttons.
LinearLayout horizontalLinearLayout = getLayoutInflater().inflate(R.layout.horizontal_layout,
linearLayout, false);
Button horizontalOne = new Button(Activity.this);
horizontalOne.setText("Horizontal");
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
horizontalOne.setLayoutParams(childParam);
Button horizontalTwo = new Button(Activity.this);
horizontalTwo.setText("Horizontal");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
horizontalTwo.setLayoutParams(childParam2);
horizontalLinearLayout.addView(horizontalOne);
horizontalLinearLayout.addView(horizontalTwo);
// Insert the inflated layout to the main layout.
verticalLinearLayout.addView(horizontalLinearLayout);
// Add the second vertical Button
Button secondVerticalButton = new Button(Activity.this);
secondVerticalButton.setText("Vertical");
verticalLinearLayout.addView(secondVerticalButton);
use table.setOrientation(LinearLayout.HORIZONTAL); .
Hope it Helps.

Setting a layout to view.setEnabled(false) does not disable all child views

I have a layout with several nested relative layouts. Within the nested layouts I have form elements, such as TextViews, EditTexts and Buttons. The code is abbreviated just for this example:
Context con;
LinearLayout survey = new LinearLayout();
RelativeLayout question = new RelativeLayout();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnAnswer = new Button(con);
btnAnswer.setId(GlobalVars.getLatestId());
btnAnswer.addParams(params);
question.addView(btnAnswer);
TextView tvBtnLabel = new TextView(con);
btnAnswer.setId(GlobalVars.getLatestId());
tvBtnLabel.setText("Some Label");
tvBtnLabel.addParams(params);
question.addView(tvBtnLabel);
survey.addView(question);
question.setEnabled(false);
//^^^^does not set child views to disabled state
When I set the entire nested Relative Layout to false, the Button and the TextView are not disabled. I have to go in and set each child view to disabled individually. Bug in android? Is there a maximum nesting limit for disabling views? I've checked the state and the relative layouts are indeed set to disabled.
Try this:
Context con;
LinearLayout survey = new LinearLayout(con);
survey.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
RelativeLayout question = new RelativeLayout(con);
question.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnAnswer = new Button(con);
btnAnswer.setId(GlobalVars.getLatestId());
btnAnswer.addParams(params);
question.addView(btnAnswer);
TextView tvBtnLabel = new TextView(con);
btnAnswer.setId(GlobalVars.getLatestId());
tvBtnLabel.setText("Some Label");
tvBtnLabel.addParams(params);
question.addView(tvBtnLabel);
survey.addView(question);
[UPDATE]
for (int i = 0; i < question.getChildCount(); i++) {
View child = question.getChildAt(i);
child.setEnabled(false);
}
Hope this help!

Defining a Linear Layout progmatically

I am trying to dynamically create a linear layout with a dynamic number of buttons based on certain parameters. So far I have some code that compiles but when it runs it does not display anything.
public void displayMenu()
{
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();
//Test Button
Button btn1= new Button(this);
btn1.setText("Dylan");
lin.addView(btn1);
for (int i =0 ; i < 5; i++){
btnList.add(new Button(this));
btnList.get(i).setText("Hello:"+i);
lin.addView(btnList.get(i));
Log.i("CategoryMenu","Adding btn to view");
}
}
What am i doing incorrectly? I'm sure that i'm missing some thing silly here.
you have to add lin to the current hierarchy of view, or creating a new one calling setContentView(lin);
It seems that you're not adding that linear layout to a viewgroup parent.
After all your code you should add something like
myParentViewGroup.add(lin);
Before lin.addView(btn1);
You should define layoutparams for Button like:
// Defining the layout parameters of the Button
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Setting the parameters on the Button
tv.setLayoutParams(lp);
You have to add lin too the layout.
At present you have just created a Linearlayout, but you have not attached it to any layout.
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();

Android button layout

I have recently coded my button to be aligned to the right of the screen and it seems to not want to work.
I have also checked to see if my parent layout is set to cover the whole width of the screen and it is.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Vertical Layout - For the layout of the newSheetLayout.
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newSheetButtonLayoutH.addView(saveButton);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
If it is something simple and I'm very blind to the solution, I will punish myself accordingly.
Thank you for your help.
try
saveButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
if you are using xml and your need is not to do it in dynamic way then do
android:layout_gravity="right"
Edit:
i think this is what you want
LinearLayout newSheetLayoutV,dataShowV,dataShowH,newSheetButtonLayoutH,newlayout;
Button nextButton,backButton,saveButton;
//RelativeLayout ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
newlayout = new LinearLayout(this);
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setGravity(Gravity.RIGHT);
newlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//newSheetButtonLayoutH = new RelativeLayout(this);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
//nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
//backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
//saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newlayout.addView(saveButton);
newSheetButtonLayoutH.addView(newlayout);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
Try giving RelativeLayout params
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.ALIGN_RIGHT);
rel_lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
btn.setLayoutParams(rel_lp);
Try setting the layout_gravity of LinearLayout instead of your Button.

How to set Alert Dialog height and width in Android?

Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
AlertDialog alertChoice = new AlertDialog.Builder(context).create();
alertChoice.setTitle("Title");
alertChoice.setView(ViewDialogScreen("Test"));
alertChoice.show();
}
private View ViewDialogScreen(String strText) {
LinearLayout llay = new LinearLayout(context);
llay.setLayoutParams(new LayoutParams(320, 400));
TextView tv = new TextView(context);
tv.setText(strText);
llay.addView(tv);
return llay;
}
I got the output like the above.
I need to show the AlertDialog in full screen / 95% of the Screen size.
I need to handle more fields in the Dialog.
How do I enable the HorizontalScrollview in the AlertDialog?
to get the scrolling behaviour, add a surrounding ScrollView to your layout, which would make your ViewDialogScreen method to this:
private View ViewDialogScreen(String strText) {
ScrollView scroll = new ScrollView(context);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout llay = new LinearLayout(context);
llay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TextView tv = new TextView(context);
tv.setText(strText);
scroll.addView(llay);
llay.addView(tv);
return scroll;
}
Now try adding some more fields and it should scroll.
I am not sure this but you can set your own layout for dialog. Refer my answer Android: Dialog dismisses without calling dismiss
If you want to customize an AlertDialog you need to create a Custom Dialog.

Categories

Resources