Nested LinerLayout in Custom Dialog not showing up - java

Im trying to create a custom dialog box which contains main LinearLayout which has 2 nested LinearLayout inside it which contains ImageView and TextView.
But the problem is the None of the LinearLayout's are showing up.I'm not even getting any errors and Application is not crashing.
Thank You.
Below is the code segment related to this problem:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
ll_main = new LinearLayout(AddFreebie.this);
ll_main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll_main.setGravity(Gravity.CENTER);
ll_main.setOrientation(LinearLayout.VERTICAL);
ll_bpic = new LinearLayout(AddFreebie.this);
ll_bpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll_bpic.setGravity(Gravity.CENTER);
ll_bpic.setOrientation(LinearLayout.HORIZONTAL);
iv_bpic = new ImageView(AddFreebie.this);
iv_bpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_bpic = new TextView(AddFreebie.this);
tv_bpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_bpic.setText("Browse phone for pictures..");
ll_bpic.addView(iv_bpic);
ll_bpic.addView(tv_bpic);
ll_cpic = new LinearLayout(AddFreebie.this);
ll_cpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ll_cpic.setGravity(Gravity.CENTER);
ll_cpic.setOrientation(LinearLayout.HORIZONTAL);
iv_cpic = new ImageView(AddFreebie.this);
iv_cpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_cpic = new TextView(AddFreebie.this);
tv_cpic.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tv_cpic.setText("Take pictures using camera.");
ll_cpic.addView(iv_cpic);
ll_cpic.addView(tv_cpic);
ll_main.addView(ll_bpic);
ll_main.addView(ll_cpic);
builder.setView(ll_main);
builder.setCancelable(true);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// MyActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();

I could be incorrect, But I dont think an AlertDialog is what you really want to use in this case. If you read the Developer pages on AlertDialogs, they are really to be used for small 0, 1, 2, or 3 button alert windows, and arent really meant to have their own truly custom interfaces. Here are some links on AlertDialogs
http://www.helloandroid.com/tutorials/how-display-alertdialog-your-android-application
To do what you want to do I think you either want to use a Diaglog, or a PopupWindow
Link to creating custom dialogs:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
Link to creating a custom popover window (I have used these in the past):
http://www.mobilemancer.com/2011/01/08/popup-window-in-android/

Related

How To Add Changing Number Of EditText Objects in AlertDialog

I'm Developing a game so I want to get player name using an AlertDialog. But I don't Know certain number of players, it's Variable between 2 to 16!
I've added an spinner to ask about the NumberOfPlayers and a Button to Show AlertDialog then I tried to add certain number of EditText Using for loop. It doesn't have error but when I run application on phone, I just have Ok and Cancel Buttons. I couldn't resolve problem and become appreciate if someone help me.
This is My AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Opponents:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText[] input = new EditText[NumberOfPlayers];
for (int aux=0;aux==NumberOfPlayers;aux++) {
input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(input[aux]);
}
builder.setView(layout); // this is a set method, not add
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int aux=0;aux==NumberOfPlayers;aux++){
//PlayersTXT[aux].setText(input[aux].getText().toString());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Just change your code with below one and you will get dynamic edittext in alert dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Opponents:");
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
final EditText[] input = new EditText[NumberOfPlayers];
for (int aux=0;aux<NumberOfPlayers;aux++) {
input[aux] = new EditText(MainActivity.this);
input[aux].setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
input[aux].setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(input[aux]);
}
builder.setView(layout); // this is a set method, not add
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int aux=0;aux==NumberOfPlayers;aux++){
//PlayersTXT[aux].setText(input[aux].getText().toString());
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Let me highlight problems in your code are:
you are using "aux==NumberOfPlayers" in "for loop" which is wrong. it should be "aux < NumberOfPlayers"
you are not initializing edittext in "for loop" such as "input[aux] = new EditText(MainActivity.this);"
you are not giving height and width for both linear layout as well edittext after initializing such as ".setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));"

I am creating a alert box in android studio

I am trying to build a alert box in android studio, i have 2 xml and 1 activity class where on click of a button i want to show the other layout.
I am getting a error, here is my code of MainActivity
cancelBookingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alert_cancel_confirm, null);
final TextView disAgree = (TextView) alertLayout.findViewById(R.id.TextDisagree);
final TextView Agree = (TextView) alertLayout.findViewById(R.id.TextAgree);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();
}
});
In this line i am getting error
AlertDialog.Builder alert = new AlertDialog.Builder(this);
In Builder cannot be applied
Any idea why i am getting this.
AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
alert.setTitle("Current Booking");
// This will set the view from XML inside ALertDialog
alert.setView(alertLayout);
// disabling cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
AlertDialog dialog = alert.create();
dialog.show();

Showing custom view inside AlertDialog

I have a class that extends LinearLayout. It contains simply two EditBox. On some button click, i want to load this class inside the alert Dialog. When i click on button, the alert dialog is displayed but the view class that extends LinearLayout is not displayed.The code is as follows. I am stucked into this. Can I get some solution??
public class StudentDialog extends LinearLayout {
Context context;
LinearLayout layout;
public StudentDialog(Context context) {
super(context);
this.context = context;
createStudentDialog();
}
private void createStudentDialog() {
layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.setPadding(10, 10, 10, 10);
layout.setId(200);
EditText studentName = new EditText(context);
studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
studentName
.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);
EditText address = new EditText(context);
address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
address.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);
layout.addView(studentName);
layout.addView(address);
}
}
//Now i am calling this on some button click listener as follows. The alert dialog is displayed but not the StudentDialog.
StudentDialog dialog = new StudentDialog(this);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(dialog);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
It should work if you change
alertDialogBuilder.setView(dialog);
as
alertDialogBuilder.setView(dialog.layout);
Do you want to show two edit text in a alert view just like a login dialog with button, if yes then just create a layout which you want to show in dialog view. Create a class which extends Dialog and set this layout in setContentView of onCreate of this class

Android: put 2 buttons in custom dialog to appear in center

I have a custom dialog.
I want to put 2 buttons one near the other horizontally, to appear in center.
How can I achieve that?
Very similiar to Yes/No message box.
I am seeking a way to do it in layout xml file.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:width="wrap_content"
android:height="wrap_content"
android:layout_centerHorizontal="true">
... And two buttons here
From Android Dev
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
You may be looking for something similar to this. You can set the "Positive" and "Negative" button to say whatever you want and have each one do its own thing. In your case (and most cases) Yes and No would be perfect here!
Some examples of xml files can also be found by following that link as well as this one that I have used before.
Blog about custom dialogs. (Nice Example Code)
You can use an xml to define the layout of the content of the Dialog anyway you want, then you use an layoutInflater to set as a view in an AlertDialog:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Documentation source: http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Center message in android dialog box

I want the message text within my dialog box to be center aligned.
Of course, you can always set the gravity of the original text view. This allows you to not have to worry about formatting and padding.
For example
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();
// Must call show() prior to fetching text view
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
Create your own TextView object and then supply it to popup builder as View:
AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
popupBuilder.setView(myMsg);
You can control all other text parameters (style, color, size ...). To control margins you may programatically create LinearLayout, set LayoutParams, and then put TextView into it.
Building off of Chase's answer, here's how to also center the title. I think this is the easiest way. Why android doesn't center by default or make it a simple constructor param is beyond me.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("My Title");
builder.setMessage("My message");
builder.setPositiveButton("OK", listener);
AlertDialog dialog = builder.show();
// Must call show() prior to fetching views
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
TextView titleView = (TextView)dialog.findViewById(context.getResources().getIdentifier("alertTitle", "id", "android"));
if (titleView != null) {
titleView.setGravity(Gravity.CENTER);
}
we can use as like.
public static void showAlert(Activity activity, String message) {
TextView title = new TextView(activity);
title.setText("Your Title Here");
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setCustomTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.show();
TextView messageText = (TextView)alert.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.RED);
}
you can try this code
public void Info(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Info Aplication");
builder.setIcon(R.drawable.info_adp);
builder.setMessage("Jakarta Hospital");
builder.setCancelable(false);
builder.setPositiveButton("Exit", null);
AlertDialog dialog = builder.show();
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);
}
without using textview.
worked for me.
styles.xml
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:gravity">center</item>
</style>
Alertdialog
final AlertDialog builder = new AlertDialog.Builder(activity,
R.style.CustomAlertDialog)
try this in your TextView:
android:gravity = "center_vertical|center"

Categories