android button onClick don't do anything - java

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.

Related

I want to do INVISIBLE on SubActivity with the button on MainActivity

SubActivity cannot INVISIBLE the button in MainActivity I tried to use intent, but I'm not sure.
it SubActivity java code
try to INSIBLE the resetB button in Sub Activity
public class SubActivity extends Activity {
Button resetH, resetH2;
Button resetB, miC;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ImageButton back = (ImageButton) findViewById(R.id.back);
resetH = (Button) findViewById(R.id.resetH);
resetH2 = (Button) findViewById(R.id.resetH2);
resetB = findViewById(R.id.resetB);
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.VISIBLE);
}
});
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.INVISIBLE);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
I used intent, but I can't move the button

Android AlertDialog Not Showing Any Content

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");
}
});

OnClickListener for Multiple ImageButton

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));
}
});

Adding second onClick method in Android Studio?

I've just started to learn Java and I've been stumped on adding the code for a second button in an activity. I apologize for my (possible dumb question) and any wrong terminology.
Here is the MainActivity Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, GoActivity.class));
}
});
}
}
How can I add to this code for btnEscape to go to EscapeActivity?
You just have to do exacly the same thing you do with btnGo - this is find your button by id, and then set clickListener to it. It could looks like that:
Button btnEscape = (Button) findViewById(R.id.btnEscape);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
}
});
The optimised way to do that is implement your class with View.OnClickListener and override the method onClick and inside it use switch cases to switch between views and apply on clicks like this:
public class SampleActivity extends AppCompatActivity implements View.OnClickListener{
Button btnGo,btnEscape;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
btnEscape= (Button) findViewById(R.id.btnEscape);
btnGo.setOnClickListener(this);
btnEscape.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnGo:
startActivity(new Intent(MainActivity.this, GoActivity.class));
break;
case R.id.btnEscape:
startActivity(new Intent(MainActivity.this, EscapeActivity.class));
break;
default:
break;
}
}
}
In code, what the two comments by Shiram and Nik above are saying is to add the block below that begins Button btnEscape... after Button btnGo.setOnclick's block.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Button btnGo = (Button) findViewById(R.id.btnGo);
...
Button btnEscape = (Button) findViewById(R.id.<<name of button in xml>>);
btnEscape.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});
}
In short, the following is a really good pattern to have at your fingertips:
Button btn___ = (Button) findViewById(R.id.<<name of button in xml>>);
btn___.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... whatever ... }
});

Error setonclickListener undefined for the type button

Hello i'm getting the error:
the method setonclickListener(new onclickListener(){}) is undefined
for the type button
Here's my code:
public class Sliding extends Activity {
/** Called when the activity is first created. */
private LinearLayout MenuList;
private Button btnToggleMenuList;
private int screenWidth;
private boolean isExpanded;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding);
MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
btnToggleMenuList = (Button) findViewById(R.id.button1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
if (isExpanded) {
isExpanded = false;
MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}else {
isExpanded = true;
MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}
}
});
}
}
can anyone tells me why?
Replace this
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
By
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Also make sure you have the below import statement
import android.view.View.OnClickListener;
Change it to
View.OnClickListener()
Write this:
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rather than:
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
Try this:
btnToggleMenuList.setonclickListener(new onclickListener() {
#Override
public void onclick(View v) {

Categories