libGDX Alert Dialog - android

I have used code below:
AlertDialog.Builder bld;
if (android.os.Build.VERSION.SDK_INT <= 10) {
//With default theme looks perfect:
bld = new AlertDialog.Builder(AndroidLauncher.this);
} else {
//With Holo theme appears the double Dialog:
bld = new AlertDialog.Builder(AndroidLauncher.this, android.R.style.Theme_Holo_Dialog_MinWidth);
}
bld.setIcon(R.drawable.ic_launcher);
bld.setTitle("Exit");
bld.setMessage("Are you sure you want to exit?");
bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); }
});
bld.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) { finish(); }
});
bld.setCancelable(false);
bld.create().show();
It seems fine, but it says "import android.app.AlertDialog cannot resolve".
It is a standard libGDX project in Android Studio.

In libgdx you should use a scene2d dialog instead of the native Android DialogInterface. Below is how you would add a completely skinned dialog to the stage in libgdx with custom button images and background image. You would just need to substitute your own background and button image textures and font, then call quitGameConfirm() when you're ready to display the dialog...
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
public void quitGameConfirm() {
LabelStyle style = new LabelStyle(_fontChat, Color.WHITE);
Label label1 = new Label("Are you sure that you want to exit?", style);
label1.setAlignment(Align.center);
//style.font.setScale(1, -1);
style.fontColor = Color.WHITE;
Skin tileSkin = new Skin();
Texture tex = new Texture(myButtontexture);
tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
tileSkin.add("white", tex);
tileSkin.add("default", new BitmapFont());
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = tileSkin.newDrawable("white");
textButtonStyle.down = tileSkin.newDrawable("white", Color.DARK_GRAY);
textButtonStyle.checked = tileSkin.newDrawable("white",
Color.LIGHT_GRAY);
textButtonStyle.over = tileSkin.newDrawable("white", Color.LIGHT_GRAY);
textButtonStyle.font = _myTextBitmapFont;
textButtonStyle.font.setScale(1, -1);
textButtonStyle.fontColor = Color.WHITE;
tileSkin.add("default", textButtonStyle);
TextButton btnYes = new TextButton("Exit", tileSkin);
TextButton btnNo = new TextButton("Cancel", tileSkin);
// /////////////////
Skin skinDialog = new Skin(Gdx.files.internal("data/uiskin.json"));
final Dialog dialog = new Dialog("", skinDialog) {
#Override
public float getPrefWidth() {
// force dialog width
// return Gdx.graphics.getWidth() / 2;
return 700f;
}
#Override
public float getPrefHeight() {
// force dialog height
// return Gdx.graphics.getWidth() / 2;
return 400f;
}
};
dialog.setModal(true);
dialog.setMovable(false);
dialog.setResizable(false);
btnYes.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
// Do whatever here for exit button
_parent.changeState("StateMenu");
dialog.hide();
dialog.cancel();
dialog.remove();
return true;
}
});
btnNo.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
//Do whatever here for cancel
dialog.cancel();
dialog.hide();
return true;
}
});
TextureRegion myTex = new TextureRegion(_dialogBackgroundTextureRegion);
myTex.flip(false, true);
myTex.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
Drawable drawable = new TextureRegionDrawable(myTex);
dialog.setBackground(drawable);
float btnSize = 80f;
Table t = new Table();
// t.debug();
dialog.getContentTable().add(label1).padTop(40f);
t.add(btnYes).width(btnSize).height(btnSize);
t.add(btnNo).width(btnSize).height(btnSize);
dialog.getButtonTable().add(t).center().padBottom(80f);
dialog.show(stage).setPosition(
(MyGame.VIRTUAL_WIDTH / 2) - (720 / 2),
(MyGame.VIRTUAL_HEIGHT) - (MyGame.VIRTUAL_HEIGHT - 40));
dialog.setName("quitDialog");
stage.addActor(dialog);
}

The problem is that you are trying to create an Android widget which I suspect you are doing it in the Libgdx-core implementation. The core implementation does not have any references to Android SDK.
That is because it is the Android project which inherits the core project. As a result the core project is not aware of any dependencies loaded to the Android implementation.
To overcome this you need to create an interface between Android project and Core Project. That will allow you to call methods inside the Android Project.
The interface must be created inside the core Project in order for both projects to have access to it.
For example you create CrossPlatformInterface.java inside core Project. But first let's create a callback to get feedback from the Ui Thread inside the Libgdx Thread. It is important to remember that Libgdx has a seperate thread that Android main thread!!! If you try to run Widgets of Android from Libgdx threads the Application will crush.
Let's make the callback for the AlertDialog. I will suggest an Abstract class here in order to be able to override only the methods you want because sometimes Alertdialog can have 1,2 or 3 buttons.
In Core Project create AlertDialogCallback.java:
public abstract class AlertDialogCallback{
public abstract void positiveButtonPressed();
public void negativeButtonPressed(){}; // This will not be required
public void cancelled(){}; // This will not be required
}
In Core Project also create CrossPlatformInterface.java:
public interface CrossPlatformInterface{
public void showAlertDialog(AlertDialogCallback callback);
}
You notice that in the showAlertDialog method we pass the callback to get feedback when buttons are pressed!
Then you create a Class inside Android project that will implement the CrossPlatformInterface like:
public ClassInsideAndroidProject implements CrossPlatFormInterface{
private AndroidLauncher mActivity; // This is the main android activity
public ClassInsideAndroidProject(AndroidLauncher mActivity){
this.mActivity = mActivity;
}
public void showAlertDialog(final AlertDialogCallback callback){
mainActivity.runOnUiThread(new Runnable(){
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle("Test");
builder.setMessage("Testing");
builder.setPositiveButton("OKAY", new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
callback.positiveButtonPressed();
}
});
builder.setNegativeButton(negativeButtonString, new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
callback.negativeButtonPressed();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
Important notes
The CrossPlatformInterface will be instantiated inside the MainActivity (AndroidLauncher) as you will see below.
The AlertDialog will be created inside the android UI thread. Because we are coming from the Libgdx thread to create the AlertDialog we need to use runOnUiThread to ensure the AlertDialog is created in ui thread.
Finally how to execute this:
Instantiate CrossPlatform interface inside Android main Activity and pass the Activity to the Interface instance which is passed inside the MyGdxGame:
public class MainActivity extends AndroidApplication {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new MyGdxGame(new ClassInsideAndroidProject(this)), cfg);
}
}
Finally when the MyGDxGame is created we get the instance of the crossplatform interface and we can the call any functions we want to the android ui thread.
public class MyGdxGame extends Game {
ClassInsideAndroidProject crossPlatformInterface;
public MyGdxGame(ClassInsideAndroidProject crossPlatformInterface){
this.crossPlatformInterface = crossPlatformInterface;
}
#Override
public void create() {
crossPlatformInterface.showAlertDialog(new AlertDialogCallback(){
#Override
public void positiveButtonPressed(){
//IMPORTANT TO RUN inside this method the callback from the ui thread because we want everything now to run on libgdx thread! this method ensures that.
Gdx.app.postRunnable(new Runnable().....)
}
#Override
public void negativeButtonPressed(){
}; // This will not be required
#Override
public void cancelled(){
}; // This will not be required
});
}
#Override
public void render() {
super.render();
}
public void dispose() {
super.dispose();
}
public void pause() {
super.pause();
}
}
I think it was much more writing I first intended. It might look daunting but actually is fairly simple. Well after you've done it everything looks simpler :).
The advantage of this effort is after you make this interface any call to android widget will be very easy and thread safe.
Hope it gives a good picture.

This works (tested). Simply pass in the FragmentActivity or Activity
via your game constructor. You have to pass something in (like
ClassInsideAndroidProject). Why not pass in a really useful element !.
//---------------------------------------------------------------------------
/** INSIDE the libgdc core, create a custom NATIVE android dialog
* :- breaks the rules somewhat for the core,
* but if you ONLY using Android, why not use android Native!
* #member_var private final FragmentActivity m_fa;
* #constructor public xx_your_app_xx(FragmentActivity m_fa)
*{
* this.m_fa = m_fa;
*}
* #called_with if(m_fa != null) showCustomDialog(m_fa);
* #param fa
*/
public static void showCustomDialog(final FragmentActivity fa) //or Activity
{
fa.runOnUiThread(new Runnable()
{
// boolean[] info;
#Override
public void run()
{
LinearLayout ll_Main = new LinearLayout(fa);
LinearLayout ll_Row01 = new LinearLayout(fa);
LinearLayout ll_Row02 = new LinearLayout(fa);
LinearLayout ll_Row09 = new LinearLayout(fa);
LinearLayout ll_Row10 = new LinearLayout(fa);
ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);
ll_Row09.setOrientation(LinearLayout.HORIZONTAL);
ll_Row10.setOrientation(LinearLayout.HORIZONTAL);
final CheckBox checkBox = new CheckBox(fa);
final CheckBox cb_debug = new CheckBox(fa);
final EditText et_User = new EditText(fa);
final EditText et_Pass = new EditText(fa);
TextView tv_Check = new TextView(fa);
TextView tv_Debug = new TextView(fa);
TextView tv_User = new TextView(fa);
TextView tv_Pass = new TextView(fa);
tv_Check.setText("rotation lock: ");
tv_Debug.setText("debug: ");
tv_User.setText("Username: ");
tv_Pass.setText("Password: ");
ll_Row01.addView(tv_Check);
ll_Row01.addView(checkBox);
ll_Row02.addView(tv_Debug);
ll_Row02.addView(cb_debug);
ll_Row09.addView(tv_User);
ll_Row09.addView(et_User);
ll_Row10.addView(tv_Pass);
ll_Row10.addView(et_Pass);
ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);
// ll_Main.addView(ll_Row09);
// ll_Main.addView(ll_Row10);
AlertDialog.Builder alert = new AlertDialog.Builder(fa);//this.getActivity()
alert.setTitle("Camera settings");
alert.setView(ll_Main);
alert.setCancelable(false);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// info1[0] = checkBox.isChecked();
// info1[1] = cb_debug.isChecked();
// String user = et_User.getText().toString();
// String pass = et_Pass.getText().toString();
//do something with the data
Gdx.app.log("INFO", "**** positiveButtonPressed works here too! ***");
Toast.makeText(fa,
"checkBox: " + checkBox.isChecked() +
", cb_debug: " + cb_debug.isChecked(),
Toast.LENGTH_LONG).show();
//IMPORTANT TO RUN inside this {} means everything now run's on libgdx thread!.
Gdx.app.postRunnable( new Runnable()
{
public void run()
{
//do something with the data
Gdx.app.log("INFO", "**** positiveButtonPressed works here ****");
}//run
});//postRunnable
}//onClick
});//setPositiveButton
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}//setPositiveButton
});//setNegativeButton
AlertDialog dialog = alert.create();
dialog.show();
}//run
});//runOnUiThread
}//showCustomDialog
//--------------------------------------------------------------------------------

Related

Dialog pops up very slow

In my app I have implemented this custom dialog (which has a fairly complex layout) by extending DialogFragment. I expect this dialog to pop up when I click a button in my layout. (Which I have successfully achieved). But the problem is that the dialog shows up in a janky manner.
My custom dialog class:
public class CustomizeDialog extends DialogFragment implements AdapterView.OnItemSelectedListener {
// field declarations go here
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.customize_dialog, null);
builder.setView(view)
.setTitle("Customize")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Let's go!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction("fromDialog");
intent.putExtra("ratio",getRatio(paperSizeSpinner.getSelectedItem().toString()));
if(isOrientationSpinnerVisible){
intent.putExtra("isCustom",false);
intent.putExtra("orientation",orientationSpinner.getSelectedItem().toString());
} else {
intent.putExtra("isCustom",true);
}
intentProvider.getIntent(intent);
}
});
widthEditText = view.findViewById(R.id.width_et);
heightEditText = view.findViewById(R.id.height_et);
widthEditText.setEnabled(false);
heightEditText.setEnabled(false);
paperSizeSpinner = view.findViewById(R.id.paper_size_spinner);
orientationSpinner = view.findViewById(R.id.orientation_spinner);
// ArrayList for populating paperSize spinner via paperSizeAdapter
ArrayList<String> paperSizes = new ArrayList<>();
paperSizes.add("A0");
paperSizes.add("A1");
paperSizes.add("A2");
paperSizes.add("A3");
paperSizes.add("A4");
paperSizes.add("A5");
paperSizes.add("Custom");
// ArrayList for populating orientation spinner via orientationAdapter
ArrayList<String> orientation = new ArrayList<>();
orientation.add("Portrait");
orientation.add("Landscape");
// arrayAdapters containing arraylists to populate spinners
ArrayAdapter paperSizeAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, paperSizes);
ArrayAdapter orientationAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_dropdown_item, orientation);
paperSizeSpinner.setAdapter(paperSizeAdapter);
orientationSpinner.setAdapter(orientationAdapter);
paperSizeSpinner.setSelection(4);
paperSizeSpinner.setOnItemSelectedListener(this);
orientationSpinner.setOnItemSelectedListener(this);
return builder.create();
}
// These are some important complex ui functionalities
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.paper_size_spinner) {
if (position == 6) {
widthEditText.setEnabled(true);
heightEditText.setEnabled(true);
orientationSpinner.setEnabled(false);
isOrientationSpinnerVisible = false;
} else {
widthEditText.setEnabled(false);
heightEditText.setEnabled(false);
orientationSpinner.setEnabled(true);
isOrientationSpinnerVisible = true;
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
// interface used to communicate with the parent activity
public interface IntentProvider {
// this method is used to provide the intent to the parent activity
void getIntent(Intent intent);
}
// instantiating the interface object and throwing error if parent activity does not implement this interface
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
intentProvider = (IntentProvider) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement IntentProvider");
}
}
}
MainActivity class:
public class MainActivity extends AppCompatActivity implements CustomizeDialog.IntentProvider {
// field declarations go here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image);
// instantiating the dialog
final CustomizeDialog dialog = new CustomizeDialog();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// showing the dialog on click
dialog.show(getSupportFragmentManager(),"");
}
});
}
// via this method I receive the intent from the dialog
// I know intent might not be the best option for this function but let's let it be here for now
#Override
public void getIntent(Intent intent) {
ratio = intent.getFloatExtra("ratio",3);
isCustom = intent.getBooleanExtra("isCustom",false);
orientation = intent.getStringExtra("orientation");
launchChooser();
}
}
Let me know in the comments if you want the layout code for the dialog.
What I tried:
Implementing threading so that my dialog is ready in a background thread and show it onButtonClick. But this is not allowed in general as any other thread except UI thread aren't supposed to touch UI related events.
Using onCreateView instead of onCreateDialog to inflate the layout directly.
Making the dialog a global variable, initialized it in onCreate and then show the dialog onButtonClick.
Switched to CONSTRAINT LAYOUT
Using an activity as a dialog by setting the dialog theme to the activity in the manifest file.
Launched my app in a device with better hardware than mine.
BUT NOTHING WORKED
What I want:
Why is my dialog janky? and what I need to do to make the dialog pop up faster?
In case anybody wants here's the link to my app repo on github.
AlertDialog and DialogFragment frameworks are slow because they need to some time to do calculations and fragment stuffs. So a solution to this problem is, using the Dialog framework straight away.
Use the Dialog framework's constructor to initialize a Dialog object like this:
Dialog dialog = new Dialog(context, R.style.Theme_AppCompat_Dialog);
// the second parameter is not compulsory and you can use other themes as well
Define the layout and then use dialog.setContentView(R.layout.name_of_layout).
Use dialog.findViewById(R.id.name_of_view) to reference views from the dialog's layout file
And then implement the logic just like anyone would do in an activity class. Find out the best implementation for your use case by reading the official documentation.

Skipped 844 frames! Too much work in MainThread caused by simple AlertDialog with Spinners [duplicate]

This question already has answers here:
The application may be doing too much work on its main thread
(21 answers)
Closed 1 year ago.
I use a AlertDialog with two spinners, the App itself is pretty fast, but when I start this Dialog the application slows down when the dialog is finished the speed rises.
One of the Spinners is really "heavy" it contains 60 elements consisting of one picture and one TextView. Picture (ImageView) is small, but the original picture is 512x512 PNG 12,54kB (is this to large?).
One ArralyList (for the small spinner) is initialized directly, the small one is initialized at start of the application.
This is the AlertDialog
public class dialog_CardWithdrawRequest extends AppCompatDialogFragment {
private static final String TAG = "dialog_CardWithdrawRequ";
private ArrayList<obj_PrintState> printStatesList;
private Spinner sp_DialogCardWithdrawRequest_Printer;
private Spinner sp_DialogCardWithdrawRequest_Country;
private sendChoosenCountryAndPrintState listener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (dialog_CardWithdrawRequest.sendChoosenCountryAndPrintState) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement sendChoosenCountryAndPrintState listener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
initArrays();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_cardwithdrawrequest, null);
final int Driver;
final int iCurrentDriverPosition;
Bundle receivingBundle = this.getArguments();
if(receivingBundle != null){
Driver = receivingBundle.getInt("Driver");
iCurrentDriverPosition = receivingBundle.getInt("CurrentCountryPosition");
}else{
Driver = INT_DEFAULT_VALUE;
iCurrentDriverPosition =0;
}
builder.setView(view)
.setTitle(R.string.dialog_CardWithdrawRequest_Title);
builder.setNegativeButton(R.string.Cancle, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
obj_Country selectedCountry = (obj_Country) sp_DialogCardWithdrawRequest_Country.getSelectedItem();
String sSelectedCountry = selectedCountry.getsHexCountryCode();
obj_PrintState selectedPrintState = (obj_PrintState) sp_DialogCardWithdrawRequest_Printer.getSelectedItem();
String sSelectedPrintRequest = selectedPrintState.getsPrinterRequestHex();
listener.choosenCountryAndPrintState(sSelectedCountry, sSelectedPrintRequest, Driver);
}
});
sp_DialogCardWithdrawRequest_Country = view.findViewById(R.id.sp_DialogCardWithdrawRequest_Country);
sp_DialogCardWithdrawRequest_Printer = view.findViewById(R.id.sp_DialogCardWithdrawRequest_Printer);
adapter_Spinner_Country adapterCountry = new adapter_Spinner_Country(getContext(), objCountryList);
adapter_Spinner_Printer adapterPrinter = new adapter_Spinner_Printer(getContext(), printStatesList);
sp_DialogCardWithdrawRequest_Country.setAdapter(adapterCountry);
sp_DialogCardWithdrawRequest_Printer.setAdapter(adapterPrinter);
sp_DialogCardWithdrawRequest_Country.setSelection(iCurrentDriverPosition);
return builder.create();
}
private void initArrays(){
printStatesList = new ArrayList<>();
printStatesList.add(new obj_PrintState(PRINTREQUEST_NONE, getText(R.string.dialog_CardWithdrawRequest_PrintRequest_None).toString()));
printStatesList.add(new obj_PrintState(PRINTREQUEST_UTC, getText(R.string.dialog_CardWithdrawRequest_PrintRequest_UTC).toString()));
printStatesList.add(new obj_PrintState(PRINTREQUEST_LOCAL, getText(R.string.dialog_CardWithdrawRequest_PrintRequest_Local).toString()));
}
public interface sendChoosenCountryAndPrintState{
void choosenCountryAndPrintState(String sSelectedCountry, String sSelectedPrintRequest, int Driver);
}
}
This is the call from mainActivity
helperThread = new HandlerThread("HelperThread");
helperThread.start();
Handler helperHandler = new Handler(helperThread.getLooper());
helperHandler.post(new Runnable() {
#Override
public void run() {
dialog_CardWithdrawRequest dialog_cardWithdrawRequest = new dialog_CardWithdrawRequest();
Bundle information = new Bundle();
information.putInt("Driver", iDriver);
information.putInt("CurrentCountryPosition", finalIPosition);
dialog_cardWithdrawRequest.setArguments(information);
dialog_cardWithdrawRequest.show(getSupportFragmentManager(),"CardWithdrawRequest");
}
});
As you can See, I already tried to post this "hard work" on a different thread, but I think an AlertDialog always runs in UI thread, I don't know.
I also tried to use:
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
... 512x512 PNG 12,54kB (is this to large?) ... Yes, it is.
Considering that the decompressed raw bitmap is x * y * 4 bytes (R, G, B and A components, each weighting 1 full byte).
Therefore, each image of yours weights 1 MB in memory.
By the way, there's no need to use an ImageView: just set the image as a compound drawable in your TextView and squeeze out some performance.
Here is an interesting link:
https://antonioleiva.com/textview_power_drawables/

Change textColor of textswitcher/viewswitcher based on int value?

I have a countdown timer starting at 60000 milliseconds and want to change the text color from Color.BLUE to Color.RED once the time is at and below 10000 milliseconds. I've tried the following without any success; attempted to setTextColor of TextSwitcher and add IF statement that would change color based on int value timerState.. I can't figure out how to make it work besides possibly stopping the timer and creating another one once the millisecondsUntilFinished hits 10000 which actually lead to my second issue where:
I click on an imageButton that initiates a dialog fragment (PauseFragment) and calling cancel() on my CountDownTimer via timerCDT.cancel(). I ran into some nullpointer issues hence the if statements checking for null in my code, but now once the PauseFragment dismisses my new timer starts back at 60000 rather than where it last left off. I was hoping that long timerState = 60000 would get updated to whatever millisUntilFinished is everytime onTick() was called but I'm not sure where I went wrong!
Therefore, can someone please assist me with changing TextSwitcher text color dynamically and assist in figuring out why my CountDownTimer isn't starting at the expected value. Any assistance is greatly appreciated.
THANKS in advance!
public class GameActivity extends FragmentActivity implements PauseFragment.FragmentCommunicator,{
public static long timerState = 60000;
public static boolean isTimerOn = false;
private String modeChoice = ModesActivity.mode;
private TextSwitcher timerTextSwitcher;
CountDownTimer timerCDT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...more code
timerTextSwitcher = (TextSwitcher) findViewById(R.id.timerTextSwitcher);
timerTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// Create a new TextView and set properties
TextView textView = new TextView(getApplicationContext());
textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(20);
textView.setTextColor(Color.BLUE);
if (timerState < 10001) {
textView.setTextColor(Color.RED);
}
return textView;
}
});
// Declare the animations and initialize them
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
// set the animation type to textSwitcher
timerTextSwitcher.setInAnimation(in);
timerTextSwitcher.setInAnimation(out);
}
timerCDT = new CountDownTimer(timerState, 1000) {
public void onTick(long millisUntilFinished) {
isTimerOn = true;
timerTextSwitcher.setText(String.valueOf(millisUntilFinished / 1000));
timerState = millisUntilFinished;
}
//TODO: assign highscores for players to beat
public void onFinish() {
timerTextSwitcher.post(new Runnable() {
#Override
public void run() {
createToast("GAME OVER!");
}
});
isTimerOn = false;
DialogFragment endDialog = new EndGameFragment();
endDialog.show(getSupportFragmentManager(), "EndGameDialogFragment");
}
};
timerCDT.start();
#Override
public void onPause() {
super.onPause();
Bundle args = new Bundle();
args.putInt(ARG_SCORE, scoreINT);
args.putLong(ARG_TIMER, timerState);
args.putString(GameActivity.ARG_MODE, modeChoice);
if (timerCDT != null) {
timerCDT.cancel();
}
else{
createToastExtended("onPause() - timerCDT is null; attempt to cancel");
}
}
//.!.other fun code here.!.
#Override
protected void onStop() {
super.onStop();
if (timerCDT != null) {
timerCDT.cancel();
}
else{
createToastExtended("onStop() - timerCDT is null; attempt to cancel");
}
}
//Player Response information
#Override
public void pauseFragmentResponse() {
if (timerCDT != null) {
timerCDT.start();
}
else{
createToastExtended("pauseFragmenResponse() - timerCDT is null; attempt to start");
}
}
public void pauseStartFrag(View view) {
DialogFragment dialog = new PauseFragment();
if (timerCDT != null) {
timerCDT.cancel();
}
else{
createToastExtended("pauseStartFrag() - timerCDT is null;attempt to cancel");
}
dialog.show(getSupportFragmentManager(), "PauseDialogFragment");
}
// Code for PauseFragment
//TODO: remove unuses imports on all files within project;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
public class PauseFragment extends DialogFragment {
public static boolean isPaused = false;
public FragmentCommunicator fComm;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
fComm = (FragmentCommunicator) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentCommunicator");
}
}
#Override
public void onDetach() {
super.onDetach();
fComm = null;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
isPaused = true;
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.fragment_pause, null))
.setMessage(R.string.dialog_pause)
.setPositiveButton(R.string.action_main_menu, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i4 = new Intent(getActivity(), StartActivity.class);
startActivity(i4);
}
})
.setNeutralButton(R.string.action_restart, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i4 = new Intent(getActivity(), ModesActivity.class);
startActivity(i4); }
})
.setNegativeButton(R.string.action_resume, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
fComm.pauseFragmentResponse();
dismiss();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
isPaused = false;
}
public interface FragmentCommunicator {
public void pauseFragmentResponse();
}
}
Lastly, Idk if it's of any help but I also tried starting the CountDownTimer timerCDT without the FragmentCommunicator interface but the system couldn't find the timer? If someone could shine light on why this happened I'd appreciate it as well.
Seriously, one last thing, if the timer is for a game and needs to be stopped and updated frequently, is it best to use CountDownTimer, TimerTask, a newThread that implements Runnable or a handler or some sort? I've tried them all but as I add components and features to the app I need more and more flexibility with changing the time and not quite sure if I'm headed down the right path. Hope this post isn't too vague. Please let me know if I need to separate into multiple posts or something...
Thanks as always!
just had a look on the developer website here http://developer.android.com/reference/android/os/CountDownTimer.html and it looks like you should probably be placing that if statement in the onTick method, so for every tick you do the check.
EDIT
ok this works perfectly for me
private TextSwitcher TextSw;
private TextView TextSwTextView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(com.game.test.R.layout.sample);
TextSw = (TextSwitcher) findViewById(R.id.TextSwitchView);
TextSw.setFactory(new ViewSwitcher.ViewFactory()
{
public View makeView()
{
// Create a new TextView and set properties
TextView textView = new TextView(getApplicationContext());
textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setTextSize(20);
textView.setTextColor(Color.BLUE);
return textView;
}
});
mtimer = new CountDownTimer(60000, 1000)
{
public void onTick(long millisUntilFinished)
{
TextSwTextView = (TextView) TextSw.getChildAt(0);
if(millisUntilFinished < 10001)
TextSwTextView.setTextColor(Color.RED);
TextSwTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
TextSwTextView.setText("done!");
}
}.start();
}
so the above is a simple version of yours, the text that is displaying the timer will change to Red when the timer hit 1000. You should be able to build this into yours.
But the main thing you have to do here is to check how much the timer has left in the in the onTick method and also change the text color in here to - see above
This thread helped me solve my problem more easily:
https://groups.google.com/forum/#!topic/android-developers/jdlUp_RlP2w
Your get a handle on the textviews within the textSwitcher like this:
TextView t1 = (TextView) mSwitcher.getChildAt(0);
TextView t2 = (TextView) mSwitcher.getChildAt(1);
Then you set whatever color you need based on your code logic.
TextView t1, t2;
textSwitcher = (TextSwitcher) findViewById(R.id.textView99);
textSwitcher.setInAnimation(this, R.anim.slide_in_right);
textSwitcher.setOutAnimation(this, R.anim.slide_out_left);
t1 = new TextView(this);
t2 = new TextView(this);
t1.setTextSize(20);
t2.setTextSize(20);
textSwitcher.addView(t1);
textSwitcher.addView(t2);

Activity not destroying to release Heap in android?

I am trying to release heap size by destroying the current activity, while going to another activity.
I am using finish(); on backPreess()
But this is not releasing the heap.
on setContentView()
The heap size increases 16Mb. I want to release this increase in the heap after going to another activity. Can any one help how to do this?
My code is as following:
package com.stancil.levels;
public class PaintActivity extends ZebraActivity implements
PaintView.LifecycleListener, PaintView1.LifecycleListener1 {
private static final int REQUEST_PICK_COLOR = 1;
....
....
public PaintActivity() {
_state = new State();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Constants.context = getApplicationContext();
setContentView(R.layout.paint);
..................
...................
...............
}
public void onPreparedToLoad() {
// We need to invoke InitPaintView in a callback otherwise
// the visibility changes do not seem to be effective.
new Handler() {
#Override
public void handleMessage(Message m) {
new InitPaintView();
Log.v("PaintActivity", "After InitPaintView Called");
}
}.sendEmptyMessage(0);
}
private class InitPaintView implements Runnable {
private Bitmap _originalOutlineBitmap;
private Handler _handler;
public InitPaintView() {
// Make the progress bar visible and hide the view
_paintView.setVisibility(View.GONE);
_progressBar.setProgress(0);
_progressBar.setVisibility(View.VISIBLE);
_state._savedImageUri = null;
_state._loadInProgress = true;
_originalOutlineBitmap=_imageBitmap;
_handler = new Handler() {
#Override
public void handleMessage(Message m) {
switch (m.what) {
case Progress.MESSAGE_INCREMENT_PROGRESS:
// Update progress bar.
_progressBar.incrementProgressBy(m.arg1);
break;
case Progress.MESSAGE_DONE_OK:
case Progress.MESSAGE_DONE_ERROR:
// We are done, hide the progress bar
// the paint view back on.
_state._loadInProgress = false;
_paintView.setVisibility(View.VISIBLE);
_progressBar.setVisibility(View.GONE);
initiatePopupWindow();
break;
}
}
};
new Thread(this).start();
}
public void run() {
Log.v("Wasimmmmmmmmmmmmmmmm", "qqqqq 22");
_paintView.loadFromBitmap(_originalOutlineBitmap, _handler);
}
}
private static class State {
// Are we just loading a new outline?
public boolean _loadInProgress;
// The resource ID of the outline we are coloring.
//public int _loadedResourceId;
//
// If we have already saved a copy of the image, we store the URI here
// so that we can delete the previous version when saved again.
public Uri _savedImageUri;
}
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Do you want to go to Main Menu?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Constants.check_new=true;
Intent i=new Intent(PaintActivity.this,MainActivity.class);
// i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
}
}
}
release yoru objects in onDestroy method, anyways, if there are no references to the detroyed activity, GC will automaticly clean up whenever its needed (it doesnt need to happen right after you closed your activity).
Alternatively theres a method to force running GC, but I wont even write about it cuz its not really a feature a typical application should use

Android Call Intent Null pointer exception

Im at my wits end here. I have a Class which Implements the OnClickListener cous i need the same action on Buttons accros my Application. This used to work just fine. But since I added some functionality by getting some needed data from the app preferences. startActivity throws a null pointer exception.Here is the class:
//Imports
public class CallClickListener extends Activity implements View.OnClickListener {
protected AppPreferences appPrefs;
String contactPersonName;
String contactPersonTelephone;
String name;
public CallClickListener(Context context){
Log.d("TRACE", "init CallClick");
appPrefs = new AppPreferences(context);
try {
JSONObject object = appPrefs.getConsultantObject();
contactPersonName = object.getString("contactPersonName");
contactPersonTelephone = object.getString("contactPersonTelephone");
name = object.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onClick(View view) {
final View v = view;
AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
alert.setTitle("Anrufen");
alert.setMessage("Kontakt für " + name + ", " + contactPersonName + " anrufen");
alert.setPositiveButton("Anrufen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+contactPersonTelephone));
startActivity(callIntent);// this line throws the exception
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
}
The Strings are all there from appPrefs, i also tried with hardcoding a phonenumber just incase. the Alert works fine, but as soon as i hit the positive Button, the app crashes
I add the Listener like this:
bCall.setOnClickListener(new CallClickListener(getApplicationContext()));
I added the necessary Call permissions.
I'm fairly new to Android dev, what am I missing?
Do this.... make the context object that you passed in the constructor into a field variable. and change startActivity to context.startActivity. It will work then.
EDIT: Highlighting the full solution.
bCall.setOnClickListener(new CallClickListener(getApplicationContext()));
should be changed to YourActivityClass.this instead of getApplicationContext.
Start Activity in the same task does not work with a context object that is not an Activity. So you need to either change the context to Activity or you start the activity in a new task. Also without calling startActivity on the context provided to your constructor you were getting the NPE because your CallClickListerner has no context.
Use activity context. Also check if you have initialized bCall. If you have not you will get NullPointerException.
bCall.setOnClickListener(ActivityName.this);
Also check this link to know when to use activity context and when to use application context
When to call activity context OR application context?
Edit:
Make sure you have added permission in manifest file
<uses-permission android:name="android.permission.CALL_PHONE" />
For reference use the below. My Class extends Activity
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v1) {
// TODO Auto-generated method stub
final View v = v1;
AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
alert.setTitle("Anrufen");
alert.setMessage("Kontakt für " );
alert.setPositiveButton("Anrufen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:8095992052"));
startActivity(callIntent);// this line throws the exception
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
});

Categories

Resources