In my application I have button to which displays an AlertDialog. The AlertDialog has a CheckBox. Now I have to call a function if the checkbox is checked, and a different function if its not checked.
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialogTheme);
builder.setTitle("Delete Completed Tasks:");
View view = getLayoutInflater().inflate(R.layout.del_completed,null);
builder.setView(view);
builder.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
CheckBox cb = findViewById(R.id.checkbox);
if(cb.isChecked()){
helper.delCompAndRec();
}
else helper.delCompleted();
}
});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
The problem is I always get the cb.isChecked() value as False, even if its checked. How do I solve this?
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialogTheme);
builder.setTitle("Delete Completed Tasks:");
View view = getLayoutInflater().inflate(R.layout.del_completed,null);
final CheckBox cb = view.findViewById(R.id.checkbox); //or declare as global variable
builder.setView(view);
builder.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(cb.isChecked()){
helper.delCompAndRec();
}
else helper.delCompleted();
}
});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
You need to define checkbox in your design xml. And then you can access that checkbox via view.findViewById(R.id.checkbox).
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat);
builder.setTitle("Delete completed task");
View view = getLayoutInflater().inflate(R.layout.del_completed, null);
final CheckBox cb = view.findViewById(R.id.checkbox); //like defined here
builder.setView(view);
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(cb.isChecked()){
helper.delCompAndRec();
}
else {
helper.delCompleted();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
In xml declare like this
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="30dp"/>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:text="Check this for test"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Related
I want to the show Full size of the image in the alert dialog, When I click on the imagebutton in the android project. How can I do?
In xml file -
<?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="match_parent"
android:orientation="vertical">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/my_image"/>
</LinearLayout>
In activity show your custom dialog box using below code -
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogView)
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
transaction to new fragment with a imageview in match parent.
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>
I have a large string and I need to add 2 hyperlinks. I did it, but it`s not clickable.
String:
<string name="freeasa"><![CDATA[<b><font color=#cc0022>text<font color=#2266bb> text </font></b> <br> text <a href=\'http://google.com\'>navigate to google.com</a><br><b><font color=#2266bb> text</font><font color=#cc0022> text </font></b><br> text <a href=\'http://yahoo.com\'> yahoo link<\a> ]]></string>
AlertDialog:
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getResources().getString(R.string.add_info));
builder.setMessage(Html.fromHtml(getResources().getString(R.string.freeasa)))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
There are hyperlinks, but they are not clickable. How to fix it ?
try custom alert dialog below way
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(promptsView);
final TextView userInput = (TextView) promptsView
.findViewById(R.id.textView);
userInput.setMovementMethod(LinkMovementMethod.getInstance());
userInput.setText(Html.fromHtml(getResources().getString(R.string.about_body)));
builder.setTitle(getResources().getString(R.string.app_name));
// builder.setMessage(Html.fromHtml(getResources().getString(R.string.about_body)))
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
prompts.xml
<?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="match_parent"
android:orientation="vertical"
android:padding="10dip">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
make sure your string is correct and links are working
Use this to set an heperlink to a TextView:
TextView website = new TextView(this);
website.setText("http://www.google.com");
Linkify.addLinks(website, Linkify.ALL);
Hope it helps
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();
}
});
So I have this dialog box that pops up.
The buttons, however, are unresponsive. The debug lines inside of the onClick methods aren't being reached.
What am I doing wrong or not doing?
Here's my code, followed by my XML for the dialog box.
lv = new ListView(this);
lv.setAdapter(aa);
lv.setId(getTaskId());
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
TextView v=(TextView) view.findViewById(R.id.info);
String str = (String) v.getText();
String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setView(getLayoutInflater().inflate(R.layout.loginbox,(ViewGroup) view));
b.setPositiveButton(R.id.loginbutton, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Log.i("Button","Log in");
}
});
b.setNegativeButton(R.id.cancelbutton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.i("Button","Cancel");
dialog.dismiss();
}
});
Log.i("Click",ssid);
}
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:orientation="horizontal" >
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="#string/ssidlabel"
android:inputType="textEmailAddress"
android:text="#string/ssidlabel" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/username"
android:layout_marginTop="14dp"
android:ems="10"
android:fontFamily="sans-serif"
android:hint="#string/password"
android:inputType="textPassword"
android:text="#string/password" >
<requestFocus />
</EditText>
<Button
android:id="#+id/loginbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/username"
android:layout_below="#+id/password"
android:text="#string/login" />
<Button
android:id="#+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/loginbutton"
android:layout_alignBottom="#+id/loginbutton"
android:layout_alignRight="#+id/username"
android:text="#string/cancel" />
TextView v=(TextView) view.findViewById(R.id.info);
String str = (String) v.getText();
String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setView(getLayoutInflater().inflate(R.layout.loginbox, null, false));
b.setPositiveButton(R.id.loginbutton, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Log.i("Button","Log in");
}
});
b.setNegativeButton(R.id.cancelbutton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.i("Button","Cancel");
dialog.dismiss();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
Log.i("Click",ssid);
You could try the following:
lv = new ListView(this);
lv.setAdapter(aa);
lv.setId(getTaskId());
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
TextView v=(TextView) view.findViewById(R.id.info);
String str = (String) v.getText();
String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setView(getLayoutInflater().inflate(R.layout.loginbox,(ViewGroup) view));
b.setPositiveButton("Login", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Log.i("Button","Log in");
}
});
b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.i("Button","Cancel");
dialog.dismiss();
}
});
b.show();
Log.i("Click",ssid);
}
});
And then you can delete your <Button>s from your xml. The .setPositiveButton and .setNegativeButton will create the buttons for you and there is no need to create them in the xml and reference them with R.id.