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)
Related
I want to create a custom alertdialog with Spinner .So i used the following code.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ActivityThu.this);
alertDialog.setTitle("Thêm khoản thu");
//final LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// final View dialogView = layoutInflater.inflate(R.layout.thu_thong_bao_them, null);//Tạo dialogview từ layout thông báo
View dialogView = getLayoutInflater().inflate(R.layout.thu_thong_bao_them, null);
spinner=(Spinner)dialogView.findViewById(R.id.spdanhmuc_them);
MyDatabaseHelper db = new MyDatabaseHelper(getApplicationContext());
List<String> lables = db.Load_danhmucthu();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
alertDialog.setView(dialogView);// đặt dialogview vào alertdialog
alertDialog.setIcon(R.drawable.add48x48);//đặt icon
alertDialog.setPositiveButton("Thêm",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setNegativeButton("Hủy",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
Layout for DialogAlert thu_thong_bao_them.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Danh mục"
android:id="#+id/tvdanhmuc_them"/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tvdanhmuc_them"
android:layout_marginLeft="20dp"
android:id="#+id/spdanhmuc_them"
android:spinnerMode="dropdown">
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nội dung"
android:id="#+id/tvnoidung_them"
android:layout_below="#+id/tvdanhmuc_them"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
android:id="#+id/edtnoidung_them"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tvnoidung_them"
android:layout_below="#id/spdanhmuc_them"
android:layout_marginLeft="20dp"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ngày thu"
android:id="#+id/tvngaythu_them"
android:layout_below="#+id/tvnoidung_them"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<EditText
android:id="#+id/edtngaythu_them"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:inputType="text"
android:layout_toRightOf="#id/tvnoidung_them"
android:layout_below="#id/edtnoidung_them"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Số tiền"
android:id="#+id/tvsotien_them"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvngaythu_them"
/>
<EditText
android:id="#+id/edtsotien_them"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tvsotien_them"
android:layout_below="#id/edtngaythu_them"
android:layout_marginLeft="30dp"
android:inputType="text" />
</RelativeLayout>
Image Befor I call AlertDialog
Image After call alertdialog (data has been load into spinner but main_layout was replaced by layout of alertdialog and spinner in Dialog hasn't data)
Please help me.
After create custom view , set it to alert dialog view, don't set anything related to dialog like icon,positive buttons etc.
I create a custom dialog box with this method:
public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_rate, null))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putBoolean("rate",false);
settingsEditor.apply();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=de.resper.pzcrettungsdienst"));
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putBoolean("rate",false);
settingsEditor.apply();
}
})
.setNeutralButton(R.string.later, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putInt("count",0);
settingsEditor.apply();
}
});
return builder.create();
}
I open the dialog like this:
Dialog dialog = onCreateDialog();
dialog.show();
This is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/dialog_message"
android:id="#+id/textView15"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center"
android:text="#string/resperLink"
android:layout_margin="20dp"
android:id="#+id/textView" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fbImg"
android:src="#drawable/fb_icon"
android:layout_margin="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gplusImg"
android:src="#drawable/gplus_icon"
android:layout_margin="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/twImg"
android:src="#drawable/tw_icon"
android:layout_margin="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pinImg"
android:src="#drawable/pin_icon"
android:layout_margin="5dp"/>
</LinearLayout>
</LinearLayout>
In the layout file are images, is it possible to set on the images in the layout "setOnClickListener"? The Buttons working, but I want to add some more "actions" for the images.
In the layout file are images, is it possible to set on the images in
the layout "setOnClickListener"?
yes it is possible. Keep a reference to the inflated view, and use this to findViewById
View view= inflater.inflate(R.layout.dialog_rate, null);
view.findViewById(R.id.yourid).setOnClickListener(...);
builder.setView(view)
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.
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.
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