2
Well, I’m learning now about Fragments. From what I understand, fragments are components, something you want to repeat on several screens, without having to keep creating several classes or Activity’s.
I am trying to create a dialog, it will be using on a screen and will be displayed as soon as I press a certain button. I created all the Dialog code, however I do not know how I call it on the other screen.
-Dialogue:
package com.vuforia.samples.Books.Neoris.Componentes;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import com.vuforia.samples.Books.R;
public class DialogoStartGPS extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_start_gps)
.setPositiveButton(R.string.permitir, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
- Aboutscreen.java[SCREEN THAT WILL BE DISPLAYED THE DIALOG]:
/*=============================================================================== Copyright (c) 2016 PTC Inc. All Rights Reserved.
Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
Vuforia is a Trademark of PTC Inc., Registered in the United States and other countries. ===============================================================================*/
package com.vuforia.samples.Books.ui.ActivityList;
import com.vuforia.samples.Books.R;
import com.vuforia.samples.Books.Neoris.Componentes.DialogoStartGPS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AboutScreen extends Activity {
private Button mStartButton;
private String mClassToLaunch;
private String mClassToLaunchPackage;
private AlertDialog alertGPS;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about_screen);
Bundle extras = getIntent().getExtras();
mClassToLaunchPackage = getPackageName();
mClassToLaunch = mClassToLaunchPackage + "." + extras.getString("ACTIVITY_TO_LAUNCH");
alertGPS = new AlertDialog.Builder(DialogoStartGPS.class);
mStartButton = (Button) findViewById(R.id.button_start);
mStartButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertGPS.show();
/* Intent i = new Intent();
i.setClassName(mClassToLaunchPackage, mClassToLaunch);
startActivity(i);
finish();*/
}
});
}
}
then I inserted it like this in my Aboutscreen.java, but it asks to create a method on the screen of my Fragment like this: public void show(Fragmentmanager fragmentManager, String s) { } MY CÒDIGO: mStartButton = (Button) findViewById(R.id.button_start); mStartButton.setOnClickListener(new Onclicklistener() { @Override public void onClick(View v) { Dialogostartgps dsg = new Dialogostartgps(); dsg.show(getFragmentManager(), "Jon Snow"); } });
– Thiago Saad
@Thiagosaad you need to insert inside the button in your class
AboutScreen
, but the way I did in theDialogStartGPS
using the libsandroid.app.Dialog
andandroid.app.DialogFragment
– viana
@Thiagosaad replaces its class
DialogoStartGPS
by the one I shared in the reply, using the libs I mentioned.– viana
Gave error @acklay, so I click the button. FATAL EXCEPTION: main Process: com.vuforia.samples.Books, PID: 3646 java.lang.Illegalstateexception: You need to use a Theme.Appcompat Theme (or Descendant) with this Activity.
– Thiago Saad
thank you very much for the help, I got it! I had to take out such of . v4 and . v7
– Thiago Saad
@Thiagosaad beauty! Need, I’m around!
– viana