Inheriting classes from one main class - java

I am new in programming and i don't have much experience in inheritance. Now a days I am working in android development. This is my class code. I want to inherit all classes from this class. Only difference is inherited class should change AlertDialog message and dial number in call() method. Please tell me how to inherit this class to another what is the code should be?
public class PrepaidChineTown extends AppCompatActivity {
Button chinaButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prepaidchinatown);
chinaButton = (Button)findViewById(R.id.chinaTownBtn);
chinaButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidChineTown.this);
builder.setTitle("China Town Offer");
builder.setMessage("Are you sure you want to activate China Town Offer? Terms and condition apply");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
call();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void call(){
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*2300#")));
try{
startActivity(in);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
}
}
}

Create a Constant.class
private String number = "";
public class Constant {
public static void alertDialogShow(Context context,String title,String message,String _number)
{
this.number = _number;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//to do your stuff
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void call(){
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
try{
startActivity(in);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
}
}
}
And you can call this in any activity.
like this.
Constant.alertDialogShow(CurrentActivity.this,"Title","Message","*2300#");

public class PrepaidChineTown extends AppCompatActivity implements OnClickListener
{
Button chinaButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prepaidchinatown);
chinaButton = (Button)findViewById(R.id.chinaTownBtn);
chinaButton.setOnClickListener(this);
}
public void alertDialogue(String title,String message,Context context)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
call();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void call(){
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*2300#")));
try{
startActivity(in);
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
}
}
Now Create object of PrepaidChineTown and call alertDialogue() by passing required parameters or by extending this class you can use that method to show dialogue

Related

call method from another class when button in pop up window is pressed

I have this code in Dialog class:
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("MyTitle")
.setMessage("MyMessage")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
/////////LINE OF MY QUESTION/////////////////////
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
and I need to call method "OK_Clicked" from another class (MainActivity) inside OnClickListener for "OK" button (///LINE OF MY QUESTION///). I have tried it with:
MainActivity xyz = new MainActivity();
xyz.OK_Clicked();
But every time when I click "OK" button, the app just crashes saying: "MyApp has stopped."
Define the DialogInterface.OnClickListener outside of where you need it (as in, assign it to a class level variable) and then create a method for calling its onClick method from outside the class.
//Just take your existing anonymous subclass definitions and put them here
private DialogInterface.OnClickListener listener1 = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
};
private DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("MyTitle")
.setMessage("MyMessage")
.setPositiveButton("OK", listener1)
.setNegativeButton("Cancel", listener2);
return builder.create();
}
// Call these methods from outside this class
public void onClick1(){
listener1.onClick();
}
public void onClick2(){
listener2.onClick();
}

Changing menu buttons to normal buttons

case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
How can i use this in an button click event ? This works when clicking menu button
//Initialize your button
//Set Click Listener to that like below
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Perform Some Action
//common method to be called
deleteConfirmation();
});
public void deleteConfirmation(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
}
});
}
You should use common method as you want same code to be executed multiple times. This is just a suggestion

Is Possible for on Back Pressed method with alert dialog in fragment

#Override
public void onBackPressed() {
Log.d("back button", "back button pressed");
AlertDialog.Builder ad1=new AlertDialog.Builder(getActivity());
ad1.setMessage("Are you sure you want to exit? ");
ad1.setCancelable(false);
ad1.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(getActivity(), LoginActivity.class);
startActivity(i);
}
});
ad1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(getActivity(), FrndsearchFragment.class);
startActivity(i);
}
});
AlertDialog alert=ad1.create();
alert.show();
}
By using the code i getting the error in #Override and i write the super.onBackPressed i got onBackPressed in super.onBackPressed .
Yes you can do the same by overriding onKeyDown method.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit the application?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
exit();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void exit() {
finish();
}

DialogFragment return onClick result to MainActivity

This has been asked and answered at least a dozen times yet I still can't get mine going. I've tried 4 or more of the listed answers and get no errors, a result simply isn't returned. here's the most recent code that I've tried. I really wanted this solution to work because it was the most readable to me.
I welcome any suggestions, Thanks.
MainActivity
...
private void showAlertDialog() {
FragmentManager fm = getSupportFragmentManager();
MyAlertDialogFragment alertDialog =
MyAlertDialogFragment.newInstance("Some title");
alertDialog.setTargetFragment(alertDialog, 1);
alertDialog.show(fm, "fragment_alert");
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intentdata)
{
// Stuff to do, dependent on requestCode and resultCode
if(requestCode == 1) // 1 is an arbitrary number, can be any int
{
// This is the return result of your DialogFragment
if(resultCode == 1) // 1 is an arbitrary number, can be any int
{
Toast.makeText(MainActivity.this, "result received",
Toast.LENGTH_SHORT).show();
Log.d("onActivityResult", "result received" + resultCode);
}
}
}
MyDialogFragment
...
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
...
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Are you sure?");
alertDialogBuilder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// on success
}
});
alertDialogBuilder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
getTargetFragment().onActivityResult(getTargetRequestCode(),
1, getActivity().getIntent());
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
I found it, added a onDialogOKPressed method to my MainActivity and put this inside the onClick of my dialog ((MainActivity)(MyAlertDialogFragment.this.getActivity())).onDialogOKPressed();
so now it looks like this
MyDialogFragment
...
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Are you sure?");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// on success
((MainActivity)(MyAlertDialogFragment.this.getActivity())).onDialogOKPressed();
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
getTargetFragment().onActivityResult(getTargetRequestCode(), 1, getActivity().getIntent());
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
MainActivity
...
public void onDialogOKPressed () {
// Stuff to do, dependent on requestCode and resultCode
Toast.makeText(MainActivity.this, "OK pressed", Toast.LENGTH_SHORT).show();
}

Java - Android Dialog Box via Opening Button

So I have a button with an on click listener. It's supposed to open a dialog window with options... but it doesn't.
Anyone know what the problem is?
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
}
});
EDIT (UPDATED CODE):
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
You just don't call the show method of your AlertDialog object

Categories

Resources