I'm learning Android and I'm using Google documentation to follow some examples and learn the basis. I was following this guide: "Android Dialogs" and using the same code they show but I cannot manage my app to show de AlertDialog properly. When I tap the button, the screen slightly darkens but any type of dialog appears.
I've created a specific class for the dialog with this code:
public class NuevaAveriaDialogo extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Mensaje")
.setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
dialog.dismiss();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
And this is the code in the MainActivity:
public class MainActivity extends AppCompatActivity {
DialogFragment dialogoNuevaAveria;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogoNuevaAveria = new DialogFragment();
dialogoNuevaAveria.show(getSupportFragmentManager(),"averia");
}
});
}
In MainActivity, create a new instance of NuevaAveriaDialogo instead of DialogFragment
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogoNuevaAveria = new NuevaAveriaDialogo();
dialogoNuevaAveria.show(getSupportFragmentManager(),"averia");
}
});
In your code you are just using the standard DialogFragment which returns a simple Dialog in the onCreateDialog method.
To use your NuevaAveriaDialogo use:
public class MainActivity extends AppCompatActivity {
NuevaAveriaDialogo dialogoNuevaAveria;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
//....
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogoNuevaAveria = new NuevaAveriaDialogo();
dialogoNuevaAveria.show(getSupportFragmentManager(),"averia");
}
});
}
You are instantiating the Parent DialogFragment class instead of your custom class NuevaAveriaDialogo.
Change dialogoNuevaAveria = new DialogFragment();
to dialogoNuevaAveria = new NuevaAveriaDialogo();
So your fab listener will be
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogoNuevaAveria = new NuevaAveriaDialogo();
dialogoNuevaAveria.show(getSupportFragmentManager(),"averia");
}
});
Related
I'm super new in Java! Pardon for this question although there are similar ones but I'm completely clueless on fixing my problem.
I'm trying to set an OnClickListener for each ImageButton to open a new activity.
The first ImageButton works but not for the subsequent ones, it is unclickable in AVD.
Would greatly appreciate some help on it!
public class CharacterSelect extends AppCompatActivity {
ImageButton arrowbtnright;
ImageButton contchibtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_select);
arrowbtnright = (ImageButton) findViewById(R.id.arrowbtnright);
contchibtn = (ImageButton) findViewById(R.id.contchibtn);
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
}
});
}
You are currently adding the listener of the second button, from inside the listener of the first button, this is probably not what you want to do .
Try putting both at the same level, i.e :
arrowbtnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, CharacterSelect2.class));
}
});
contchibtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(CharacterSelect.this, MiniChallenge1.class));
}
});
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I have added one button "btn". I have set onclicklistener, inside the button "btn" I have added another button "btnYes" to show display custom dialog when I add these "btnYes" apps get crash.
When I remove the "btnyes" button app is working.
Can we add onclicklistener for two button inside one button for different work?
Java Code
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btnYes = findViewById(R.id.yes);
btnNo = findViewById(R.id.no);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.customdialog.MainActivity$1.onClick(MainActivity.java:33)
If btnYes and btnNo are on the dialog, then you should initialize these buttons using the AlertDialog's View object.
You have to modify your code like following.
public class MainActivity extends AppCompatActivity {
private Button btn, btnYes, btnNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.click);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder dialogBox = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View myView = inflater.inflate(R.layout.custom_dialogbox, null);
// these button should be initialize here.
btnYes = myView.findViewById(R.id.yes);
btnNo = myView.findViewById(R.id.no);
dialogBox.setView(myView);
final AlertDialog mybuilder = dialogBox.create();
mybuilder.setCancelable(false);
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mybuilder.dismiss();
}
});
}
});
}
}
Hope it helps you.
if you want to display Dialog with choices yes or no, use this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for yes
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// action for no
}
});
AlertDialog alert = builder.create();
alert.show();
But, if you want to use a custom dialog layout, you should do it like this.
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.YOUR_LAYOUT_HERE);
Button btnyes= dialog.findViewById(R.id.btnyes);
btnyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
I'm trying to implement onClick function for a Button on a custom Dialog but nothing happen when i click on the Button .it's the butEditAdd
MainActivity.java :
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AddDialog addDialog = new AddDialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
addDialog.java:
public class AddDialog extends Dialog implements android.view.View.OnClickListener{
Activity a;
Dialog d;
Button add , cancel;
RadioButton owes,lent ,money,things ;
EditText name ,amount,object,note;
Spinner spin;
public AddDialog(Activity c) {
super(c);
this.a = c;
}
#Override
public void onClick(View v){
};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogedit);
add=(Button)findViewById(R.id.buttonAdd);
cancel=(Button)findViewById(R.id.buttonCancel);
owes = (RadioButton)findViewById(R.id.radioButtonOwes);
lent = (RadioButton)findViewById(R.id.radioButtonLent);
money = (RadioButton)findViewById(R.id.radioButtonAmount);
things =(RadioButton)findViewById(R.id.radioButtonThings);
name = (EditText)findViewById(R.id.editName);
object = (EditText)findViewById(R.id.editTextWhat);
amount =(EditText)findViewById(R.id.editTextAmount);
note =(EditText)findViewById(R.id.editTextNote);
spin=(Spinner) findViewById(R.id.spinnerdevise);
owes.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
lent.setChecked(false);
}
});
lent.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
owes.setChecked(false);
}
});
money.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
things.setChecked(false);
object.setEnabled(false);
amount.setEnabled(true);
spin.setEnabled(true);
object.setText("");
}
});
things.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
money.setChecked(false);
object.setEnabled(true);
amount.setEnabled(false);
spin.setEnabled(false);
amount.setText("");
}
});
object.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
things.performClick();
things.setChecked(true);
}
});
amount.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
money.performClick();
money.setChecked(true);
}
});
thanks for help!
Dialog fragment has separate set of life cycles.If you want to create a class adddialog,then extend dialog fragment and in oncreate of adddialog use setcontent view.
If you want to create simple dialog try this line of code.This will solve your problem.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.btn);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working", Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
Change AddDialog to Dialog
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
you need it like this
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
You should inflate the layout first, then find the view by its id.
LayoutInflater inflater = LayoutInflater.from(this);
View dialogView = inflater.inflate(R.layout.dialogedit, null);
final Button butEditAdd =(Button) dialogView.findViewById(R.id.buttonAdd);
.............
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do something
}
});
This should work.
use this instead of getApplicationContext().
Remove the setContentView(R.layout.dialogedit) from your dialog onCreate().
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And your rest of the code working fine.
i have a pop up which contains edit text..how can i prevent the softkeyboard from showing up on displaying of pop up's because my pop up is moving up as soon as it is displayed.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
i used this line in the activity in which a pop up is displayed on button click. please help...!
android:windowSoftInputMode="stateHidden"
i also used this line in manifest. but of no use.
public class MainActivity extends Activity implements OnClickListener {
AlertDialog dialog;
View checkBoxView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.des_button).setOnClickListener(this);
//checkBoxView = View.inflate(this, R.layout.popup_description, null);
}
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.popup_description);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn_Start = (Button) dialog.findViewById(R.id.ok);
btn_Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
#Override
public void onDestroy() {
if (dialog!=null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
Try
android:windowSoftInputMode="stateAlwaysHidden"
in your manifest in that activity. one more thing you can do call the following method after opening that dialog popup.
public static void hideKeyboard()
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextInPopup.getWindowToken(), 0);
}
I have a list of Clickable TextViews that are relatively doing the same thing. You click on it and it goes to that activity. Settings goes to the settings activity. About to the about and so forth. Is there an easier way to declare and set up these clickable TextViews besides this repetitious code?
TextView create,
edit,
settings,
about;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
create = (TextView) findViewById(R.id.create);
create.setTextColor(Color.parseColor("#000000"));
edit = (TextView) findViewById(R.id.edit);
edit.setTextColor(Color.parseColor("#000000"));
settings = (TextView) findViewById(R.id.settings);
settings.setTextColor(Color.parseColor("#000000"));
about = (TextView) findViewById(R.id.about);
about.setTextColor(Color.parseColor("#000000"));
create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
edit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
settings.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
about.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
If you have a small set of items you can take following approach:
public class MyActivity extends Activity implements AdapterView.OnItemClickListener {
private ArrayAdapter<Item> mAdapter;
private static enum Item {Create,Edit,Settings,About}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-- can set up from external layout also--
ListView list = new ListView(this);
setContentView(list);
list.setOnItemClickListener(this);
mAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1,Item.values());
list.setAdapter(mAdapter);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (mAdapter.getItem(i)){
case Create:
//--do stuff--
break;
case Edit:
//--do stuff--
break;
case Settings:
//--do stuff--
break;
case About:
//--do stuff--
break;
}
}
}
You can implement the View.OnClickListener .
public class YOURACTIVITY extends Activity implements OnClickListener
And in the onCreate()-
yourTextview.setOnClickListener(this);
and then
#Override
public void onClick(View v) {
// Based on the view , set the action
}