I wonder how to click a Button inside of AlertDialog in Android and this is my code
activity_float_info.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
MainActivity.java
QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null);
MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN);
MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(Boolean.TRUE)
.setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setView(R.layout.activity_float_info)
.show();
button = (Button)QR_.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("TEst");
}
});
}
});
I have inflate the layout..
I think the main problem is on
button = (Button)QR_.findViewById(R.id.button);
try this create a layout for dialog box
<TextView android:id="#+id/dialogtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/black"
android:text="Please enter the email address you used for the account"
/>
<EditText
android:id="#+id/emailedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ems="10"
android:padding="5dp"
android:cursorVisible="true"
android:singleLine="true"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:hint="Enter Mail id"
android:textSize="20dp" >
<requestFocus />
</EditText>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="#+id/cancelbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="CANCEL"/>
<Button
android:id="#+id/okbtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Ok"/>
</LinearLayout>
Then create a dialog box using this layout and handle button clicks
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.forgotpassword);
// Set dialog title
dialog.setTitle("ALERT!!");
// set values for custom dialog components - text, image and button
Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
dialog.show();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// if decline button is clicked, close the custom dialog
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=emailedittext.getText().toString();
//do something more here
}
});
Refer: https://coderzpassion.com/android-show-alertdialog/
Here is a hack to use AlertDialog:
public class CustomDialog extends AlertDialog {
protected CustomDialog(Context context) {
super(context);
}
}
And then in your Activity:
CustomDialog dialog = new CustomDialog(this);
View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout,null);
dialog.setView(view);
Button button = (Button)view.findViewById(R.id.custom_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(YourActivity.this,"Your message", Toast.LENGTH_LONG).show();
}
});
dialog.show();
And the layout of the dialog (which has nothing in it barring the Button):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="New Button" />
</RelativeLayout>
And you should be able to see a Toast when you click on the button. Hope this helps.
Dialog is like a popup window to show some options to users(options like accept/decline).
Using class android.app.Dialog to create dialog.
Using dialog.xml file to create custom dialog layout.
Example:
res/layout/dialog.xml
<?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="fill_parent" >
<ImageView
android:id="#+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="#+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/imageDialog"/>
<Button
android:id="#+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/textDialog"
android:layout_toRightOf="#+id/imageDialog"
/>
</RelativeLayout>
Java Code
// Create custom dialog object
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
Related
Guys I have create an Custom dialog with one edit text and one checkbox with this XML code
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/dialog_rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#000000"
>
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="5dp"
android:text="Velocidade de Trabalho"
android:textColor="#ffffff"
android:textSize="32dp" />
<EditText
android:id="#+id/et_name"
android:layout_width="946dp"
android:layout_height="105dp"
android:layout_below="#id/dialog_title"
android:fontFamily="#font/digital"
android:hint="Digite a Taxa Desejada"
android:inputType="number"
android:maxLength="3"
android:textColor="#ffffff"
android:textSize="50dp"
tools:ignore="HardcodedText" />
<CheckBox
android:id="#+id/checkbox"
style="?android:attr/textAppearanceMedium"
android:layout_width="1000dp"
android:layout_height="71dp"
android:text="Habilitar GPS"
android:textColor="#ffffff"
android:textSize="32dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="172dp" />
<Button
android:id="#+id/dialog_negative_btn"
android:layout_width="220dp"
android:layout_height="110dp"
android:layout_below="#+id/et_name"
android:layout_centerVertical="true"
android:layout_marginTop="92dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_toStartOf="#+id/dialog_positive_btn"
android:layout_toLeftOf="#id/dialog_positive_btn"
android:background="#ffffff"
android:text="CANCELAR"
android:textSize="22dp" />
<Button
android:id="#+id/dialog_positive_btn"
android:layout_width="220dp"
android:layout_height="110dp"
android:layout_below="#id/et_name"
android:layout_alignEnd="#+id/et_name"
android:layout_centerVertical="true"
android:layout_marginTop="90dp"
android:layout_marginEnd="1dp"
android:background="#ffffff"
android:text="CONFIRMAR"
android:textSize="22dp" />
in my fragment I created a method to show the dialog box, this dialog box is shown on click, after displaying the box I check the option in my checkbox and confirm using the positive button but when clicking again to show my dialogbox o checkbox state is not saved how can I save this state?
this is my method in fragment
private void showdialogGPS (){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_gps,null);
// Specify alert dialog is not cancelable/not ignorable
builder.setCancelable(false);
// Set the custom layout as alert dialog view
builder.setView(dialogView);
// Get the custom alert dialog view widgets reference
Button btn_positive = dialogView.findViewById(R.id.dialog_positive_btn);
Button btn_negative = dialogView.findViewById(R.id.dialog_negative_btn);
CheckBox btn_Habilita = dialogView.findViewById(R.id.checkbox);
final EditText et_name = dialogView.findViewById(R.id.et_name);
btn_Habilita.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
habilitagps = true;
}
else {
habilitagps = false;
}
}
});
// Create the alert dialog
final AlertDialog dialog = builder.create();
btn_positive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Dismiss the alert dialog
dialog.cancel();
String name = et_name.getText().toString();
Toast.makeText(getActivity(),
"Velocidade de Trabalho : " + name + ("\u2705"), Toast.LENGTH_LONG).show();
// Say hello to the submitter
Txaplicacao.setText(name);
if (habilitagps){
Toast.makeText(getActivity(),
"GPS Habilitado " + ("\u2705"), Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getActivity(),
"GPS Desabilitado " + ("\u2705"), Toast.LENGTH_LONG).show();
}
}
});
// Set negative/no button click listener
btn_negative.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Dismiss/cancel the alert dialog
//dialog.cancel();
dialog.dismiss();
Toast.makeText(getActivity(),
"Valor Não Alterado" + ("\u274c"), Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
just use SharedPreferences from Android Studio:
I am having troubles setting the two buttons on the lower part of the alert what I am trying to do is get a similar look to the IOS look of the buttons.
here is my current code:
public void openInstructions() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setNewDateOnView();
dialog.cancel();
}
}).setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).create();
TextView myMsg = new TextView(getActivity());
myMsg.setText("ExampleText ");
myMsg.setTextSize(15);
myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
dialog.setView(myMsg);
dialog.setTitle("Confirm Date of Purchase");
dialog.setOnShowListener( new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface arg0) {
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setTextColor(0xFFFF0000);
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(0xFF0000FF);
}
});
dialog.show();
}
Example of how it looks right now:
How I wanted it to look:
To design Dialog like ios , we can create custom dialog using custom layout like this :
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical"
app:layout_constraintHeight_max="wrap">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/custom_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/title"
android:gravity="center"
android:text="Confirm Date of Purchase"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="17sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/back_ll"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/rebooking_single_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textAlignment="center"
android:text="#string/dummuy"
android:textColor="#android:color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/back_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<LinearLayout
android:id="#+id/rebooking_back_button_ll"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/ok_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".49"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Ok"
android:textAlignment="center"
android:textColor="#FF6363"
android:textSize="17sp"
android:textStyle="bold" />
<View
android:layout_width="0dp"
android:layout_weight=".01"
android:layout_height="match_parent"
android:layout_below="#+id/end_time_recycle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#8F8F8F" />
<TextView
android:id="#+id/cancel_btn"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAlignment="center"
android:paddingRight="10dp"
android:text="Cancle"
android:textColor="#7BC5FF"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Main_Activity
final Dialog dialog=new Dialog(FirstActivity.this);
dialog.setCancelable(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.dialog_custom);
TextView cancel_btn=dialog.findViewById(R.id.cancel_btn);
cancel_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
TextView ok_btn=dialog.findViewById(R.id.ok_btn);
ok_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
dialog.dismiss();
}
});
dialog.show();
Well, as a start, google recommends you use the Android Design Guidelines when creating Android apps, really you should be sticking to those as that's what your users will be used to (not Apple's).
Also there's no reason to be assigning the dialog a TextView if all you're doing is showing text, the AlertDialog provides this functionality with setMessage.
If that hasn't convinced you to stick to the normal design rules, the only other option would be creating a Custom View and assign it to the AlertDialog with setView(similar to how you're doing it with the TextView).
Here's a tutorial on creating a Custom View
You can create a custom view such as the one sent and inflate it
Example:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View mView=getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
mBuilder.setView(mView);
Button close = (Button) mView.findViewById(R.id.close);
Button OK = (Button) mView.findViewById(R.id.ok);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//close the dialog
hideDialog1();
}
});
OK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
dialog = mBuilder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();
Use .setNeutralButton that make place left most
TextView textview;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_launcher_background)
.setTitle("Alert Dialog Box Title")
.setMessage("Are you sure( Alert Dialog Message )")
.setPositiveButton("YES", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("NO", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
}
===============================================
I've been trying to fix this for a while now including searching online but I just can't find solution. Everyone saying you should add onClickListener and I did but it just does not work. When I click the button nothing happens. No verbose log ever appears. This is all in one class which implements Runnable so it can do stuff in the background and this EditText onClickListener is implemented inside onCreate of a class.
//Setting up EditText onClick Listeners
oof.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating custom layout dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final View inflatedView = getLayoutInflater().inflate(R.layout.dialog_box_popup, null);
builder.setView(R.layout.dialog_box_popup);
builder.setTitle("Edit");
final EditText indc = (EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn = (Button)inflatedView.findViewById(R.id.incrementBtn);
Button decrementBtn = (Button)inflatedView.findViewById(R.id.decrementBtn);
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("increment", "Button clicked: " + MiscVariables.rocket);
}
});
//Displaying dialog
AlertDialog dialog = builder.create();
dialog.show();
}
});
And here is the custom layout for the dialog using ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/decrementBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="-"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/incrementBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="+"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/weightInDcEtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:gravity="center"
android:inputType="numberDecimal"
android:text="#string/editTextFromDialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/incrementBtn"
app:layout_constraintStart_toEndOf="#+id/decrementBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayou
From there I have no idea what to do. If anyone can help me out I would appreciate it thanks!
I've found a solution. Here is the code:
//Setting up EditText onClick Listeners
oof.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating custom layout dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.dialog_box_popup);
builder.setTitle("Edit");
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("increment", "Button clicked: " + MiscVariables.rocket);
}
});
//Displaying dialog
AlertDialog dialog = builder.create();
dialog.show();
final EditText indc =
(EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn =
(Button)dialog.findViewById(R.id.incrementBtn);
Button decrementBtn =
(Button)dialog.findViewById(R.id.decrementBtn);
}
});
Inflated view was useless here. All had to be accessed by doing dialog.findViewById.
final View inflatedView = View.inflate(getActivity(), R.layout.dialog_box_popup, null);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setView(inflatedView);
final EditText indc = (EditText) inflatedView.findViewById(R.id.weightInDcEtxt);
Button incrementBtn = (Button) inflatedView.findViewById(R.id.incrementBtn);
Button decrementBtn = (Button) inflatedView.findViewById(R.id.decrementBtn);
//Enabling the increment button in a dialog
incrementBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hellloo", Toast.LENGTH_SHORT).show();
}
});
alert.show();
Try this, Hope this solution will be work...!!!
Currently I have a activity that has a lot of buttons and each button pops up a popup that is used from this site
each button has its own image to view, i wanted to change the image rather than creating individual popup.
ActivityA
Button backbtn;
public ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hiraganaactivity);
final Button btnOpenPopup = (Button)findViewById(R.id.abutton);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button) popupView.findViewById(R.id.close);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 30, -250);
}
});
final Button ibutton = (Button)findViewById(R.id.ibutton);
ibutton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
image = (ImageView) findViewById(R.id.imageview);
image.setImageResource(R.drawable.hiraflash_i);
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.close);
btnDismiss.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 30, -250);
}});
backbtn = (Button) findViewById(R.id.backhira);
backbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(HiraganaActivity.this, Study_Menu.class));
finish();
}
});
}
When abutton is clicked, the popup shows normally with the default set image.
However when ibutton is clicked, the application crashes.
Edit:
HiraganaActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".HiraganaActivity"
tools:showIn="#layout/activity_hiraganaactivity"
android:background="#drawable/hiraganabg"
>
<Button
style="?android:attr/buttonStyleSmall"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop = "#drawable/adraw"
android:id="#+id/abutton"
android:layout_marginTop="100dp"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20sp"
android:text="a"
android:typeface="normal" />
<Button
style="?android:attr/buttonStyleSmall"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop = "#drawable/idraw"
android:text="i"
android:id="#+id/ibutton"
android:layout_alignTop="#+id/abutton"
android:layout_toRightOf="#+id/abutton"
android:layout_toEndOf="#+id/abutton"
android:textSize="20sp"
android:typeface="normal"
android:layout_marginLeft="1dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/backhira"
android:textStyle="bold|italic"
android:textSize="15sp"
android:layout_marginBottom="26dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_centerHorizontal="true"
android:backgroundTint="#b9f600"
android:textColor="#0414a3" />
And Popup.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="fill_parent"
android:orientation="vertical"
android:background="#00546e">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp"
android:background="#929292">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aflashhira"
android:id="#+id/imageview" />
<Button
android:id="#+id/close"
android:layout_marginTop="10dp"
android:layout_marginLeft="70dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
</LinearLayout>
</LinearLayout>
From Log:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.afropoker.kanaapp.HiraganaActivity$2.onClick(HiraganaActivity.java:56)
After you inflate your popup view
View popupView = layoutInflater.inflate(R.layout.popup, null);
Get the imageview using -
ImageView img = (ImageView)popupView.findViewById(R.id.img);// whatever is the id of your ImageView in your popup layout
img.setBackgroundResource(R.id.background_res); //any resource id which you want to set as a background
Using above mechanism you can change the image background any time you want
I have a button that brings up a dialog with an OK button, and I'm getting a nullpointerexception when I click on the OK button. Does anyone know what's wrong?
Here's my java code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
myListView = (ListView)findViewById(R.id.list);
new GetStuff().execute();
Button importButton = (Button)findViewById(R.id.doButton);
importButton.setEnabled(false);
importButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog pwdDialog = new Dialog(context);
pwdDialog.setContentView(R.layout.pwdentry);
pwdDialog.setTitle("Enter password");
TextView pwdText = (TextView)pwdDialog.findViewById(R.id.pwdText);
pwdText.setText("Enter password");
Button okBut = (Button)findViewById(R.id.okBut);
okBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pwdDialog.dismiss();
}
});
pwdDialog.show();
}
});
}
Here's the XML file
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/pwdText"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Enter password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/pwdEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/pwdText"
android:ems="10"
android:inputType="numberPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/okBut"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_below="#+id/pwdEntry"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:text="OK" />
</RelativeLayout>
Thanks!
The problem is okBut will be null
Button okBut = (Button)findViewById(R.id.okBut);
you need to make this as
Button okBut = (Button)pwdDialog.findViewById(R.id.okBut);
Here you are retrieving a Button by using the id, R.id.okBut from your main layout R.layout.mylist.
Button okBut = (Button)findViewById(R.id.okBut);