Good evening, programmers. I have a problem. Please help me to solve it.
I am writing an android application. I can't create custom dialogs. I have errors or my application looks very awfully. I have some dialogs in my application. Here is of a part of my programm:
The view is very terrible, isn't it? I want that one of the layouts to be in the dialog but it appears in the main activity. Why?
dialogtable.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:id="#+id/LinearLayout02"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<TextView
android:id="#+id/textRows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Amount of rows "/>
<TextView
android:id="#+id/textColumns"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Amount of columns"/>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout03"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="number"
android:gravity="right"/>
<EditText
android:id="#+id/EditText02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout04"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:id="#+id/textBorder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Width of border"/>
<EditText
android:id="#+id/EditText03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
The code from MainActivity
#Override
protected Dialog onCreateDialog(int id) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialogtable, (ViewGroup)findViewById(R.id.LinearLayout01));
AlertDialog.Builder alert = null;
alert = new AlertDialog.Builder(this);
alert.setView(layout);
alert.setTitle("Create table");
alert.setCancelable(false);
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Editable rows = fieldForRows.getText();
Editable columns = fieldForColumns.getText();
Editable border = fieldForWidthOfBroder.getText();
TableAction tableAction = new TableAction(MainActivity.this.visualPane);
tableAction.insertTable(rows, columns, border);
}
});
Dialog dialog = null;
dialog = new Dialog(this);
switch (id) {
case IDD_CUSTOM_INSERT_TABLE:
dialog.setTitle("Insert table");
dialog.setContentView(R.layout.dialogtable);
return dialog;
case IDD_CUSTOM_INSERT_IMAGE:
dialog.setTitle("Insert image");
return dialog;
default:
return alert.create();
}
}
The appearance of the application before calling showDialog():
http://s015.radikal.ru/i330/1101/d0/f7cc38852dc6.jpg
The appearance of the application after calling showDialog():
http://s003.radikal.ru/i201/1101/f0/d1ebc436427f.jpg
Why it looks so terrible? What is wrong?
In your case you should use RelativeLayout instead of LinearLayout. So that you can set android:layout_weight and other relative properties on the TextViews to make their width equal.
Have a look at http://developer.android.com/resources/tutorials/views/hello-relativelayout.html and http://developer.android.com/reference/android/widget/RelativeLayout.html
Related
I had problems with this when I was programming it. I thought I'd cured it by specifying a fixed percentage height for the body of the dialog. I thought this was bad form, because the user might have a slim display, or set large fonts, which would cut of the EditText box.
Anyway that solution has failed beyond the emulator.
It looks like Android is assigning a fixed height to a dialog, and if my custom title has devoured too much of this height, squeezing everything else off. Is this correct and how do I fix it?
Problem
public class GetUserNameDialogFragment extends DialogFragment {
final String TAG = "GetUserNameDialog";
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle);
//TODO getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View sunburst =inflater.inflate(R.layout.dialog_user_name, null);
builder.setView(sunburst);
builder.setCancelable(false)
.setPositiveButton("Let's go!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.wtf(TAG, "button press");
EditText name = (EditText) sunburst.findViewById(R.id.nameEditText);
String userName = name.getText().toString().trim();
//TODO needs to be validated
SharedPreferences sharedPref = getActivity().getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName );
editor.commit();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="#drawable/ic_ball_sunburst_classic"
android:background="#color/colorAccent"
/>
<LinearLayout
android:layout_width="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_height="125dp">
<EditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:hint="Enter your first name"/>
</LinearLayout>
</LinearLayout>
All help very much appreciated, the first impression of my app - what a disaster!
Why don't you use Dialog instead of AlertDialog ?
I built the example using a Dialog and it works perfectly
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_width=" 290dp"
android:layout_height="wrap_content"
android:background="#4CAF50">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/base"
android:background="#color/colorAccent"
/>
<EditText
android:textColor="#fff"
android:textColorHint="#fff"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first name"/>
<Button
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="#+id/letsGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Let's Go"
android:textColor="#fff"
android:background="#android:color/transparent"/>
final Dialog dialog = new Dialog(context);
if(dialog.getWindow() != null){
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
dialog.setContentView(R.layout.dialog);
Button letsGO = (Button) dialog.findViewById(R.id.letsGo);
EditText nameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
letsGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Lets GO!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.show();
I created an xml file (a dialog) and I wanted to reduce it with LayoutInflater but its always big almost as my screen. Here's the code:
protected Dialog onCreateDialog(int id) {
switch(id) {
case NICKNAME_DIALOG_ID:
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.nickname_dialog,
(ViewGroup) findViewById(R.id.linearLayout_root));
final EditText nickname =
(EditText)findViewById(R.id.editText_nickname);
AlertDialog.Builder nicknameBuilder = new AlertDialog.Builder(this);
nicknameBuilder.setView(layout);
nicknameBuilder.setTitle(R.string.nicname_dialog_title);
nicknameBuilder.setNegativeButton(R.string.nickname_dialog_cancel,
new DialogInterface.OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog, int whichButton) {
SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
}
});
nicknameBuilder.setPositiveButton(R.string.nickname_dialog_ok,
new DialogInterface.OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog, int which) {
String text = nickname.getText().toString();
Editor editor = m_GameSettings.edit();
editor.putString(VolimHrvatsku.GAME_PREFERENCES_NICKNAME,
text);
editor.commit();
SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
}
});
AlertDialog nicknameDialog = nicknameBuilder.create();
return nicknameDialog;
}
return null;
}
nickname dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sahovnica"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_enter_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/settings_enter_nick"
android:textColor="#color/dark_grey"
android:textSize="#dimen/scores_text_dim"
android:textStyle="bold" />
<EditText
android:id="#+id/editText_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<RelativeLayout
android:id="#+id/relativeLayout_nickname"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
email dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout_email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sahovnica"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_dialog_text"
android:textColor="#color/dark_grey"
android:textSize="#dimen/scores_text_dim"
android:textStyle="bold" />
<EditText
android:id="#+id/editText_body"
android:layout_width="match_parent"
android:layout_height="150dp"
android:ems="10" >
<requestFocus />
</EditText>
<RelativeLayout
android:id="#+id/relativeLayout_email"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
And I would like to ask btw, if it is possible to change the look of YES and NO buttons in AlertDialog?
Thanks in advance...
In emaildialog.xml do the layout width and height wrap_content
I think so, this might help you.
I'm trying to get a custom Dialog to work. It should have no Title, a basic TextMessage and my Custom Layout where normally Buttons appear.
I tried to accomplish this with an AlertDialog.Builder, extending Dialog, call methods on the Dialog and still not get the expected result.
This is the layout, I Like to use as a custom ButtonArea (layout/dialog_footer):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/dialog_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_show_again"
android:textColor="#color/black"
android:visibility="gone"
android:background="#color/dialog_button_background"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/dialog_checkbox"
android:layout_alignWithParentIfMissing="true"
android:orientation="horizontal"
android:background="#color/dialog_button_background"
android:paddingBottom="-10dip">
<Button
android:id="#+id/dialog_btn_positive"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/ok"
android:gravity="center"
android:visibility="gone"
/>
<Button
android:id="#+id/dialog_btn_negative"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="#string/btn_cancel"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
The simplest try goes like this:
AlertDialog.Buiilder builder = new AlertDialog.Builder(context);
builder.setMessage("my Message");
Dialog dialog = builder.create();
//builder has `setView for Message or setCustomTitle, but no setCustomFooter
LayoutInflater inflater = LayoutInflater.from(context);
View footer = inflater.inflate(R.layout.dialog_footer);
//either NullPointer Exception
dialog.addContentView(footer, footer.getLayoutParams);
//or AndroidRuntimeException (requestFeature() must be called befoire adding content)
dialog.addContentView(footer, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
If I try to emulate the "MessageArea" by putting a TextView on Top of my Layout it works, but has very ugly black borders at the bottom (I guess its the custom Dialog-Theme).
Whats the best way to keep the "look and feel" of a System Dialog, but replace the Buttons with my own View and handle anything myself?
Hello friend please check this post it may be helpful for your problem
Android - Custom AlertDialog Background Color
Here which i have build my own Custom Edit Dialog for my app see the Layout code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Name: "
android:textColor="#fff" />
<EditText
android:id="#+id/txtDelName"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Contact No :"
android:textColor="#fff" />
<EditText
android:id="#+id/txtDelAge"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="10" >
<requestFocus />
</EditText>
</TableRow>
<TableRow android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spinDiagDept"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_span="2"
android:prompt="#string/Rel_prompt"
android:visibility="visible" />
</TableRow>
</TableLayout>
And here is the alert dialog code
public class Alerts {
public static AlertDialog ShowEditDialog(final Context con,final Emergencydb emp)
{
AlertDialog.Builder b=new AlertDialog.Builder(con);
b.setTitle("Emergency Contact Details");
LayoutInflater li=LayoutInflater.from(con);
View v=li.inflate(R.layout.editdialog, null);
b.setIcon(android.R.drawable.ic_input_get);
b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
final TextView txtAge=(TextView)v.findViewById(R.id.txtDelAge);
final Spinner spin=(Spinner)v.findViewById(R.id.spinDiagDept);
Utilities.ManageDeptSpinner(con, spin);
for(int i=0;i<spin.getCount();i++)
{
long id=spin.getItemIdAtPosition(i);
if(id==emp.getDept())
{
spin.setSelection(i, true);
break;
}
}
txtName.setText(emp.getName());
txtAge.setText(String.valueOf(emp.getAge()));
b.setPositiveButton("Modify", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
emp.setName(txtName.getText().toString());
emp.setAge(String.valueOf(txtAge.getText().toString())); emp.setDept((int)spin.getItemIdAtPosition(spin.getSelectedItemPosition()));
try
{
#SuppressWarnings("rawtypes")
DatabaseHelper db=new DatabaseHelper(con);
db.UpdateEmp(emp);
Toast.makeText(getBaseContext(), "Modified Successfully", Toast.LENGTH_SHORT).show();
}
catch(Exception ex)
{
}
}
});
b.setNeutralButton("Delete", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
#SuppressWarnings("rawtypes")
DatabaseHelper db=new DatabaseHelper(con);
db.DeleteEmp(emp);
}
});
b.setNegativeButton("Cancel", null);
return b.create();
//diag.show();
}
protected static Context getBaseContext() {
// TODO Auto-generated method stub
return null;
}
public static void ShowEmpAddedAlert(
android.view.View.OnClickListener onClickListener) {
// TODO Auto-generated method stub
}
}
with the help of Aleks G I managed to get it work:
the code flow goes like this:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("MyMessage");
LayoutInflater inflater = LayoutInflater.from(context);
View footerView = inflater.inflate(R.layout.apo_plus_dialog_footer, null);
AlertDialog alertDialog = builder.create();
alertDialog.setView(footerView, 0,0,0,0);
if (!hasTitle){
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
alertDialog.show();
just in case someone wants to have a similar Dialog behavior than me.
I set a swith card with three tab in the main activity,there is a listview in the third tab,click the listview will pop up time options dialog box.
public void onSetWarn(){
LayoutInflater factoryInflater = LayoutInflater.from(MainActivity.this);
final View dialogView = factoryInflater.inflate(R.layout.dialog, null);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("set")
.setView(dialogView)
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "set success!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
/*
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
*/
dialog.show();
}
where should i put this code
timePicker = (TimePicker) this.findViewById(R.id.TimePicker1);
timePicker.setIs24HourView(true);
I trid several places,but all the NULL pointer errors.
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reminder the time"
/>
<TimePicker
android:id="#+id/TimePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remind way"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ring"
/>
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="shake"
/>
</LinearLayout>
</LinearLayout>
You should use dialogView.findViewById(R.id.TimePicker1) instead of this.findViewById(R.id.TimePicker1).That is because TimePicker1 exists in the view tree of dialogView (You load it from dialog.xml by LayoutInflator)
Does anyone know how to save the text that the user will input after a dialog Box pops up?
I'm first inflating a view for the dialog Box, then letting the user input a string and displaying it somehow. Here's my code for the Dialog Box inflating. My question is How do I save the text that someone inputs into my field from the AlertDialog Box. My XML is at the bottom
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TEXT_ENTRY:
LayoutInflater mInflater = LayoutInflater.from(this);
final View textEntryView = mInflater.inflate(
R.layout.alert_text_entry, null);
return new AlertDialog.Builder(AlarmList.this)
.setIcon(R.drawable.alert_icon)
.setTitle("Enter your name")
.setView(textEntryView)
.setPositiveButton("accept",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNegativeButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
}
return null;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="12dp"
android:textSize="16sp" >
</TextView>
<EditText android:id="#+id/alarm_name_text"
android:layout_height="3dp"
android:text=""
android:layout_below="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_alignLeft="#+id/rowTextView"
android:layout_alignRight="#+id/rowTextView">
</EditText>
<CheckBox android:id="#+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
As mentioned in EditText docs, you can call getText() to get the text from the EditText view.