How to get value of textfield in alertdialog with custom layout - android

This is my layout for the dialog :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/date"
android:layout_weight="1" />
</LinearLayout>
I call the dialog from a class extending android.support.v4.app.Fragment. Here is my class extending DialogFragment:-
public static class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.date_settings, null));
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
//The problem starts
//How do I get the reference to the EditText
}catch (Exception e){
e.printStackTrace();
}
}
});
return builder.create();
}
}
I call the MyDialogFragment from my class as follows:-
MyDialogFragment dial = new MyDialogFragment();
dial.show(getActivity().getSupportFragmentManager(),"tag");
Now, How do I get the text in the EditText ?

use dialog for getting text from EditText which is in AlertDialog:
public void onClick(DialogInterface dialog, int id) {
try {
EditText editdate = (EditText) ((Dialog)
dialog).findViewById(R.id.date);
}catch (Exception e){
e.printStackTrace();
}
}
or you can also do it using inflated view :
View view = inflater.inflate(R.layout.date_settings, null);
builder.setView(view);
....
EditText editdate = (EditText)view.findViewById(R.id.date);

First you need to give id of EditText
<EditText
android:id="#+id/txtDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/date"
android:layout_weight="1" />
Do some changes in code like following:
public static class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.date_settings, null);
builder.setView(v);
EditText txtDate = (EditText) v.findViewById(R.id.txtDate);
txtDate.setText("Ben Lind");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
//The problem starts
//How do I get the reference to the EditText
}catch (Exception e){
e.printStackTrace();
}
}
});
return builder.create();
}
}

Related

In Custom action bar if we hide any icon that space need to used by other icons or text view in action bar

In my app I am using custom action bar. In that action bar I have 4 icons and 1 textview. For action bar I am using linear layout.
I have 4 activities in my app, each activity will have different action bar. If I open 1st activity one icon will set visibility gone. Every one of these activities will have different icons.
Question: My requirement is when icon is disable that space need to used by textview. I try everything but always space will end of the action bar I don't want that space.
Here is my code.
MainActivity.class:-
public class MainActivity extends AppCompatActivity {
ImageView Image1,Image2,Image3,Image4;
TextView title;
Button btn1,btn2,btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
/*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
*/
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
View view = getSupportActionBar().getCustomView();
Image1=(ImageView)findViewById(R.id.cst_ok);
Image2=(ImageView)findViewById(R.id.cst_del);
Image3=(ImageView)findViewById(R.id.cst_edt);
Image4=(ImageView)findViewById(R.id.cst_srh);
title=(TextView)findViewById(R.id.cst_txt);
Image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showdailog();
}
});
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1=new Intent(MainActivity.this,first_activity.class);
startActivity(intent1);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2=new Intent(MainActivity.this,second_activity.class);
startActivity(intent2);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent3=new Intent(MainActivity.this,third_activity.class);
startActivity(intent3);
}
});
private void showdailog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Custom dialog");
dialogBuilder.setMessage("Enter text below");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// et1.setText(edt.getText());
title.setText(edt.getText());
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
FirstActivity.class:-
public class first_activity extends AppCompatActivity {
ImageView Image1,Image2,Image3,Image4;
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment1);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
View view = getSupportActionBar().getCustomView();
Image1=(ImageView)view.findViewById(R.id.cst_ok);
Image2=(ImageView)view.findViewById(R.id.cst_del);
Image3=(ImageView)view.findViewById(R.id.cst_edt);
Image4=(ImageView)view.findViewById(R.id.cst_srh);
title=(TextView)view.findViewById(R.id.cst_txt);
Image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showdailog();
}
});
Image3.setVisibility(View.GONE);
}
private void showdailog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Custom dialog");
dialogBuilder.setMessage("Enter text below");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// et1.setText(edt.getText());
title.setText(edt.getText());
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
SecondActivity.class:-
public class second_activity extends AppCompatActivity {
ImageView Image1,Image2,Image3,Image4;
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment3);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
View view = getSupportActionBar().getCustomView();
Image1=(ImageView)view.findViewById(R.id.cst_ok);
Image2=(ImageView)view.findViewById(R.id.cst_del);
Image3=(ImageView)view.findViewById(R.id.cst_edt);
Image4=(ImageView)view.findViewById(R.id.cst_srh);
title=(TextView)view.findViewById(R.id.cst_txt);
Image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showdailog();
}
});
Image4.setVisibility(View.GONE);
}
private void showdailog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Custom dialog");
dialogBuilder.setMessage("Enter text below");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// et1.setText(edt.getText());
title.setText(edt.getText());
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
ThirdActivity.class:-
public class third_activity extends AppCompatActivity {
ImageView Image1,Image2,Image3,Image4;
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment2);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_actionbar);
View view = getSupportActionBar().getCustomView();
Image1=(ImageView)view.findViewById(R.id.cst_ok);
Image2=(ImageView)view.findViewById(R.id.cst_del);
Image3=(ImageView)view.findViewById(R.id.cst_edt);
Image4=(ImageView)view.findViewById(R.id.cst_srh);
title=(TextView)view.findViewById(R.id.cst_txt);
Image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showdailog();
}
});
Image2.setVisibility(View.GONE);
}
private void showdailog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Custom dialog");
dialogBuilder.setMessage("Enter text below");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// et1.setText(edt.getText());
title.setText(edt.getText());
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
Custom_Actionbar_layout:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="40dp"
android:weightSum="5">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ok"
android:id="#+id/cst_ok"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text="Custom ActionBar"
android:id="#+id/cst_txt"
android:singleLine="false"
android:maxLines="2"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/delete1"
android:id="#+id/cst_del"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/edit"
android:id="#+id/cst_edt"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/search"
android:id="#+id/cst_srh"/>
</LinearLayout>
And my theme is <style name="AppTheme" parent="Theme.AppCompat.Light">
And text view is dynamically changed text it is given by user. If any one need more details I will update.
You're setting the value of the android:weightSum attribute of your LinearLayout to 5.
When you're removing a View from that layout (setting its visibility to View.GONE), the weightSum of your LinearLayout is still 5, but the actual sum of your Views is only 4, hence the empty space.
Removing the android:weightSum attribute from your LinearLayout should solve the problem:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ok"
android:id="#+id/cst_ok"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:text="Custom ActionBar"
android:id="#+id/cst_txt"
android:singleLine="false"
android:maxLines="2"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/delete1"
android:id="#+id/cst_del"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/edit"
android:id="#+id/cst_edt"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/search"
android:id="#+id/cst_srh"/>
</LinearLayout>

View.OnClickListener in a custom DialogFragment

I am trying to implement a custom DialogFragment, following this tutorial. My problem is that I fail to handle my custom's view's button.setOnClickListener event. The strangest part is that I have no problem in geting the .getText() of my button, I just can't find a way to handle the click event. Bellow is my code:
SettingsDialogFragment.java
public class SettingsDialogFragment extends DialogFragment
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_settings, null);
final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
String s = colorButton.getText().toString();
System.out.println("its working "+s);
//NOT working
colorButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
System.out.println("OnClick");
}
});
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id)
{
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
`
My custom view code (dialog_settings.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:scaleType="center"
android:background="#00CCCC"
android:contentDescription="#string/app_name"
android:text="#string/dialog_settings_title"
android:id="#+id/editText"/>
<Button
android:id="#+id/colorButton_dialogSettings"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/color_picker_title"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stroke"
android:layout_marginLeft="55dp"
android:id="#+id/radioButtonStroke"
android:checked="false"
android:layout_below="#+id/colorButton_dialogSettings"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill"
android:id="#+id/radioButton_fill"
android:checked="false"
android:layout_below="#+id/colorButton_dialogSettings"
android:layout_toRightOf="#+id/radioButtonStroke"
android:layout_toEndOf="#+id/radioButtonStroke"
android:layout_marginLeft="10dp"
/>
I am just showing you the important part.. i hope you find their respective lines in your code
final View view = inflater.inflate(R.layout.dialog_settings, null);
// inflating your view..for drawback, this line is [A]
your colorButton has a reference to view.findViewById(R.id.colorButton_dialogSettings) which is from the viewgroup view..which you reference an onclick listener for it..
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
this code right here sets the content view for your dialog. it inflates a the layout and does it's work.. so in the end your builder is not referencing it's content view to view but a new inflated R.layout.dialog_settings layout..
so to solve it just do this
builder.setView(view) // hope you know the view parameter
the view is what you instantiated at line [A] ..
Hope i made sense, and lucid enough for you..let me know if it helps
New Answer
Change your onCreateDialog to this:
import android.view.View.OnClickListener;
public class SettingsDialogFragment extends DialogFragment implements onClickListener
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_settings, null);
final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
String s = colorButton.getText().toString();
System.out.println("its working "+s);
colorButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.colorButton_dialogSettings
System.out.println("OnClick");
break;
default:
break;
}
});
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id)
{
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
In your activity:
private Button colorButton = (Button) findViewById(R.id.colorButton_dialogSettings);
**Old answer that requires you to write your own showDialog method**
Try deleting your button code in the onCreateDialog and add this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
Button colorButton =(Button) v.findViewById(R.id.colorButton_dialogSettings);
public void onClick(View v) {
// When button is clicked, call up to owning activity.
((FragmentDialog)getActivity()).showDialog();
System.out.println("OnClick");
}
});
return v;

Using Drawables inside an Dialog

I am running into an issue and don't know the best way to use a set of drawables inside an AlertDialog. The AlertDialog is presented when a user presses a button and is prompted to choose from a list of drawables.
Currently it shows Buttons I have placed in a dialog_view layout when the dialog is created. Ideally I would like to be able to use some type of listview, but if that is not possible I would like to be able to handle the selection/pressing of an item when inside the AlertDialog.
What should I do to handle any actions from within the AlertDialog?
DIALOGFRAGMENT
public class PicturePickerFragment extends DialogFragment {
ArrayList<Integer> imageList = new ArrayList<Integer>();
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// fill an array with selected images
String title = "Picture";
imageList.add(R.drawable.barbershop);
imageList.add(R.drawable.wedding);
imageList.add(R.drawable.meeting);
imageList.add(R.drawable.barbershop);
// return alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_view, null))
.setTitle(R.string.event_type)
.setPositiveButton(R.string.select_picture,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// call the method on the parent activity when
// user click the positive button
}
});
return builder.create();
}
}
DIALOG VIEW
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_baby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/baby"
android:text="Baby Shower" />
<Button
android:id="#+id/btn_baking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/baking"
android:text="Baking" />
<Button
android:id="#+id/btn_barber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/barbershop"
android:text="Barbershop" />
</LinearLayout>
You can use some ListAdapter such as an ArrayAdapter where you present a drawable for every item in the list and then use the adapter when you build the AlertDialog:
builder.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//which is the position of the item clicked
}
})
Edit: This is an example that would work with your imageList:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setAdapter(new ArrayAdapter<Integer>(getActivity(), R.layout.dialog_image, imageList) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.dialog_image, parent, false);
} else {
view = convertView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.image);
int resId = getItem(position);
imageView.setImageResource(resId);
return view;
}
}, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Dialog", "selected position " + which);
}
});
return builder.create();
where R.layout.dialog_image contains an ImageView with id android:id="#+id/image" as root element. For example:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:minHeight="?attr/listPreferredItemHeight" >
</ImageView>

How can can I add custom buttons into an AlertDialog's layout?

I have AlertDialog with Positive and Negative buttons. In AlertDialog layout I have EditText and two Buttons (btnAdd1, btnAdd2). I want when user click at the Button btnAdd1 or btnAdd2 add same text to EditText in AlertDialog (but no close AlertDialog). Is this possible do in AlertDialog or I have to use only Dialog?
This is layout (R.layout.prompt) of AlertDialog:
<LinearLayout>
<EditText
android:id="#+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnAdd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
<Button
android:id="#+id/btnAdd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
</LinearLayout>
And this is source code:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
I want get acces to the btnAdd1 and btnAdd2 from the layout. Set the OnClickListener() to these two buttons.
The following code will inflate a view from R.layout.prompt and set it to the AlertDialog. The positive and negative buttons will not be used. You can set the onClick behaviors for btnAdd1 and btnAdd2:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
EditText userInput = (EditText) promptView.findViewById(R.id.userInput);
Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);
Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);
btnAdd1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
btnAdd2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
what you want to do is;
alertD.show();
Button button = (Button)promptView.findViewById(R.id.buttonId);
button.setOnClickListener(....)
using the view to call findViewById, rather than the activity, which will look for the id in the layout that is being displayed.
According to this approach i am able to create the image button but if i want to dismiss or cancel dialog on Cancel button then what i have to do..
public static void alertDialogShow(final Context context,
final String resultMobile) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompt,
null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
userInput.setText(resultMobile);
userInput.setEnabled(false);
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
My solution for your question.
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
Button btn_1= (Button)promptView.findViewById(R.id.btnAdd1);
btn_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do required function
// don't forget to call alertD.dismiss()
}
});
Button btn_2 = (Button)promptView.findViewById(R.id.btnAdd2);
btn_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do required function
}
});
alertDialogBuilder
.setCancelable(false)
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
This is the way I did.
custom_alert_dialog.xml file created
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2"
app:layout_constraintTop_toBottomOf="#+id/text1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK"
app:layout_constraintTop_toBottomOf="#+id/text2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
In activity file
LayoutInflater layoutInflater = getLayoutInflater();
View alertLayout = layoutInflater.inflate(R.layout.custom_alert_dialog, null);
Button button = alertLayout.findViewById(R.id.button1);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setCancelable(false);
alertDialog.setView(alertLayout);
AlertDialog dialog = alertDialog.create();
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
You could try something like this :
dialog.setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.show();
}
});

InvocationTargetException in DialogFragment & NumberPicker

I'm trying to show a NumberPicker in a dialog in order to let the user select a value between 0 and 10. It's my second day spent trying to make it work.
This is what I have:
fragment_number_picker (layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numberPickerFragmentLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<NumberPicker
android:id="#+id/numberPickerInFragment"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="horizontal" />
</LinearLayout>
DialogFragment definition:
public class NumberPickerCustomDialog extends DialogFragment {
Context context;
public NumberPickerCustomDialog() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
context = getActivity().getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater li = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate and set the layout for the dialog
View view = li.inflate(R.layout.fragment_number_picker, null);
builder
// Set view:
.setView(view)
// Add action buttons
.setPositiveButton(R.string.accept, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int id) {
// Accept number
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
NumberPicker np = (NumberPicker) view.findViewById(R.id.numberPickerInFragment);
np.setMaxValue(200);
np.setMinValue(0);
np.setFocusable(true);
np.setFocusableInTouchMode(true);
return builder.create();
}
}
Call from the main Activity:
public void openNumberPicker(){
FragmentManager fm = getSupportFragmentManager();
NumberPickerCustomDialog npc = new NumberPickerCustomDialog();
npc.show(fm, "fragment_number_picker");
}
I'm getting an InvocationTargetException and I can't make it work.
Any ideas? Thanks!
Try changing the context you're using:
context = getActivity().getApplicationContext();
Instead, you need the activity's own context:
context = getActivity();

Categories

Resources