The value of the variable has been suddenly set to 0 - android

I'm doing an activity to measure how long it takes a person to do an exercise, but it has a bug that I couldn't resolve yet...
The TrainingFragment shows a list of exercises that the user can click and then my ExerciseActivity is launched and runs until the variable "remainingsSets" is setted to 0.
When I click in the first time at any exercise, everything works fine, the ExerciseActivity works correctly end return to the TrainingFragment. But then, if I try to click in another exercise, the ExerciseActivity is just closed.
In my debug, I could see that the variable "remainingSets" comes with it's right value (remainingSets = getIntent().getIntExtra("remaining_sets", 3)), but when the startButton is clicked, I don't know why the variable "remainingSets" is setted to 0 and then the activity is closed because this condition: if (remainingSets > 0){...}.
Here is my TrainingFragment:
public class TrainingFragment extends Fragment {
private final static int START_EXERCISE = 1;
private Training training;
private String lastItemClicked;
private String[] values;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Bundle bundle = getArguments();
if (bundle != null) {
training = bundle.getParcelable("training");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return (ScrollView) inflater.inflate(R.layout.template_exercises, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LinearLayout exercisesContainer = (LinearLayout) getView().findViewById(R.id.exercises);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List<Exercise> exercises = training.getExercises();
values = new String[exercises.size()];
if (savedInstanceState != null) {
values = savedInstanceState.getStringArray("values");
}
for (int i = 0; i < exercises.size(); i++) {
final View exerciseView = inflater.inflate(R.layout.template_exercise, null);
exerciseView.setTag(String.valueOf(i));
TextView remainingSets = (TextView) exerciseView.findViewById(R.id.remaining_sets);
if (savedInstanceState != null) {
remainingSets.setText(values[i]);
} else {
String sets = exercises.get(i).getSets();
remainingSets.setText(sets);
values[i] = sets;
}
exerciseView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ExerciseActivity.class);
intent.putExtra("remaining_sets",
Integer.valueOf(((TextView) v.findViewById(R.id.remaining_sets)).getText().toString()));
lastItemClicked = v.getTag().toString();
startActivityForResult(intent, START_EXERCISE);
}
});
exercisesContainer.addView(exerciseView);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArray("values", values);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
View view = ((LinearLayout) getView().findViewById(R.id.exercises)).findViewWithTag(lastItemClicked);
if (requestCode == START_EXERCISE) {
if (resultCode == Activity.RESULT_OK) { // the exercise had been
// finished.
((TextView) view.findViewById(R.id.remaining_sets)).setText("0");
view.setClickable(false);
values[Integer.valueOf(lastItemClicked)] = "0";
} else if (resultCode == Activity.RESULT_CANCELED) {
String remainingSets = data.getStringExtra("remaining_sets");
((TextView) view.findViewById(R.id.remaining_sets)).setText(remainingSets);
values[Integer.valueOf(lastItemClicked)] = remainingSets;
}
}
}
}
My ExerciseActivity:
public class ExerciseActivity extends Activity {
private Chronometer chronometer;
private TextView timer;
private Button startButton;
private Button endButton;
private int remainingSets;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
ExerciseEvents.addExerciseListener(new PopupExerciseListener());
chronometer = (Chronometer) findViewById(R.id.exercise_doing_timer);
timer = (TextView) findViewById(R.id.timer);
startButton = (Button) findViewById(R.id.start_exercise);
startButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ExerciseEvents.onExerciseBegin();
}
});
endButton = (Button) findViewById(R.id.end_exercise);
endButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ExerciseEvents.onExerciseRest();
}
});
}
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("remaining_sets", String.valueOf(remainingSets));
setResult(RESULT_CANCELED, intent);
super.onBackPressed();
}
public class PopupExerciseListener implements ExerciseListener {
public PopupExerciseListener() {
remainingSets = getIntent().getIntExtra("remaining_sets", 3);
}
#Override
public void onExerciseBegin() {
if (remainingSets > 0) {
chronometer.setVisibility(View.VISIBLE);
timer.setVisibility(View.GONE);
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startButton.setVisibility(View.GONE);
endButton.setVisibility(View.VISIBLE);
} else {
ExerciseEvents.onExerciseFinish();
}
}
#Override
public void onExerciseFinish() {
setResult(RESULT_OK);
finish();
}
#Override
public void onExerciseRest() {
chronometer.setVisibility(View.GONE);
endButton.setVisibility(View.GONE);
timer.setVisibility(View.VISIBLE);
long restTime = getIntent().getLongExtra("time_to_rest", 60) * 1000;
new CountDownTimer(restTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timer.setText(String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
ExerciseEvents.onExerciseBegin();
}
}.start();
remainingSets--;
}
}
}
And my ExerciseEvents:
public class ExerciseEvents {
private static LinkedList<ExerciseListener> mExerciseListeners = new LinkedList<ExerciseListener>();
public static void addExerciseListener(ExerciseListener listener) {
mExerciseListeners.add(listener);
}
public static void removeExerciseListener(String listener) {
mExerciseListeners.remove(listener);
}
public static void onExerciseBegin() {
for (ExerciseListener l : mExerciseListeners) {
l.onExerciseBegin();
}
}
public static void onExerciseRest() {
for (ExerciseListener l : mExerciseListeners) {
l.onExerciseRest();
}
}
public static void onExerciseFinish() {
for (ExerciseListener l : mExerciseListeners) {
l.onExerciseFinish();
}
}
public static interface ExerciseListener {
public void onExerciseBegin();
public void onExerciseRest();
public void onExerciseFinish();
}
}
Could anyone give me any help?

After you updated your code, I see you have a big memory leak in your code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
ExerciseEvents.addExerciseListener(new PopupExerciseListener());
....
}
The call ExerciseEvents.addExerciseListener(new PopupExerciseListener()) adds a new PopupExerciseListener to a static/global list: ExcerciseEvents.mExerciseListeners. Since the class PopupExerciseListener is an inner-class, it implicitly holds a reference to its enclosing ExcerciseActivity. This mean your code is holding on to each instance of ExcerciseActivity forever. Not good.
This may also explain the weird behavior you see. When one of the onExcersizeXXX() methods is called, it will call all ExcerciseListeners in the linked-list, the ones from previous screens and the current one.
Try this in your ExcerciseActivity.java:
....
ExerciseListener mExerciseListener;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
....
....
mExerciseListener = new PopupExerciseListener()
ExerciseEvents.addExerciseListener(mExerciseListener);
....
....
}
#Override
protected void onDestroy() {
ExerciseEvents.removeExerciseListener(mExerciseListener);
super.onDestroy();
}
....
In onDestroy, you deregister your listener, preventing a memory leak and preventing odd multiple callbacks to PopupExerciseListeners that are attached to activities that no longer exist.

Related

why tts isn't changing the languge in Android Fragment but works fine in Activity?

So I have been trying to change the TTS language inside a fragment but it isn't working but same code works fine inside MainActivity. I don't understand why. I have checked other solutions but none worked for me. I have tried different solution , solution but even this isn't working.
public class HomeFragment extends Fragment implements TextToSpeech.OnInitListener{
private static final int TTS_DATA_CHECK = 1;
private TextToSpeech engine;
private SettingsViewModel settingsViewModel;
private EditText textMsg;
private Button button;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
engine = new TextToSpeech(getContext(),this);
settingsViewModel = new ViewModelProvider(getActivity()).get(SettingsViewModel.class);
return inflater.inflate(R.layout.fragment_home, container, false);
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textMsg = view.findViewById(R.id.textMsg);
button = view.findViewById(R.id.playButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
engine.speak(textMsg.getText().toString(), TextToSpeech.QUEUE_FLUSH, null,null);
}
});
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
settingsViewModel.getLanguage().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(String s) {
Log.d("LANGUAGE_SELECTED",s);
switch (s){
case "ENGLISH":
engine.setLanguage(Locale.ENGLISH);
break;
case "FRENCH":
engine.setLanguage(Locale.FRENCH);
break;
}
}
});
}
#Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS){
int result = engine.setLanguage(Locale.ENGLISH);
if(result == TextToSpeech.LANG_MISSING_DATA
|| result== TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(getContext(),"Not supported",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getContext(),"TTS is missing",Toast.LENGTH_LONG).show();
}
}
}
First you need to initialize TTS in main activity like this -
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private static final int TTS_DATA_CHECK = 101;
static TextToSpeech engine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
engine = new TextToSpeech(this,this);
}
#Override
public final void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101)
{
if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
final Intent tnt = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(tnt);
}
}
}
#Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS){
if(TTS_DATA_CHECK == TextToSpeech.LANG_MISSING_DATA
|| TTS_DATA_CHECK== TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(this,"Not supported",Toast.LENGTH_LONG).show();
}
}
}
static public TextToSpeech getInstance() {
return engine;
}
}
Then you have to call the TTS instance inside Fragment like this. Note that, MainActivity is the parent activity of the given Fragment.
Now replace the following code in your Fragment with this code -
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("VMA","OnActivityCreated");
settingsViewModel.getLanguage().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(String s) {
Log.d("VMA","LANGUAGE_SELECTED " + s);
switch (s){
case "FRENCH":
MainActivity.getInstance().setLanguage(Locale.FRENCH);
break;
case "ENGLISH":
MainActivity.getInstance().setLanguage(Locale.ENGLISH);
break;
}
}
});
}
Also, you can remove the rest of the TTS code from Fragment. You don't that anymore.

How to make 500 Questions Quiz in android with single activity?

I am creating an android app, where I'll be asking for multiple types of questions using RadioButtons. I don't want to make multiple Activities for these questions. Can anyone please tell me how to do that with a short example, of at least two questions?
You can use multiples fragments... or call the activity itself multiple times...
I did an app like yours and i choose the first method!
This is some fragment of a project that i wrote, and the activity that manipulate it, you will have to change it according to your needs.
Activity
public class CollectActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
NonSwipeableViewPager pager;
SpringIndicator springIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collect);
List<Fragment> fragments = getFragments();
pager = (NonSwipeableViewPager) findViewById(R.id.view_pager);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
PagerModelManager manager = new PagerModelManager();
manager.addCommonFragment(fragments, getTitles());
ModelPagerAdapter adapter = new ModelPagerAdapter(getSupportFragmentManager(), manager);
pager.setAdapter(adapter);
springIndicator = (SpringIndicator) findViewById(R.id.indicator);
springIndicator.setViewPager(pager);
springIndicator.setOnTabClickListener(new TabClickListener() {
#Override
public boolean onTabClick(int position) {
return false;
}
});
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(CollectFragment.newInstance("Fragment 1"));
fList.add(CollectFragment.newInstance("Fragment 2"));
fList.add(CollectFragment.newInstance("Fragment 3"));
//add your fragments with a loop
return fList;
}
private List<String> getTitles() {
return Lists.newArrayList("1", "2", "3");
}
public void swipeFragment() {
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
public int getFragment() {
return pager.getCurrentItem();
}
}
Fragment
public class CollectFragment extends Fragment {
private Button openButton;
private Button confirmationCloseButton;
private Button yesRenew;
private Button noRenew;
private BroadcastReceiver udpMessages;
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static final CollectFragment newInstance(String message) {
CollectFragment f = new CollectFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = null;
if (message.compareTo("Fragment 1") == 0) {
v = inflater.inflate(R.layout.fragment_collect_open, container, false);
openButton = (Button) v.findViewById(R.id.open_button);
openButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i2 = new Intent();
i2.setComponent(new ComponentName("qira.com.locker", "qira.com.locker.Service.MessageService"));
i2.putExtra("Message", "CONFIRM_LOCKER_1_CLOSED");
getContext().startService(i2);
}
});
}
if (message.compareTo("Fragment 2") == 0) {
v = inflater.inflate(R.layout.fragment_collect_close, container, false);
confirmationCloseButton = (Button) v.findViewById(R.id.confirmation_close_button);
confirmationCloseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i2 = new Intent();
i2.setComponent(new ComponentName("qira.com.locker", "qira.com.locker.Service.MessageService"));
i2.putExtra("Message", "OPEN_LOCKER_1");
getContext().startService(i2);
}
});
}
if (message.compareTo("Fragment 3") == 0) {
v = inflater.inflate(R.layout.fragment_collect_renew, container, false);
yesRenew = (Button) v.findViewById(R.id.yes_button);
noRenew = (Button) v.findViewById(R.id.no_button);
yesRenew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((CollectActivity) getActivity()).swipeFragment();
}
});
noRenew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getContext(), ReserveActivity.class);
startActivity(i);
}
});
}
return v;
}
#Override
public void onResume() {
super.onResume();
udpMessages = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals("UDP.MESSAGES.COLLECT")) {
if (intent.getExtras().getString("Type").compareTo("OPEN_LOCKER_1-LOCKER_OPENED") == 0) {
if (((CollectActivity) getActivity()).getFragment() != 0) { // onCreateView called twice, dont know why... workaround to solve this problem
((CollectActivity) getActivity()).swipeFragment();
}
}
if (intent.getExtras().getString("Type").compareTo("CONFIRM_LOCKER_1_CLOSED-TRUE") == 0) {
if (((CollectActivity) getActivity()).getFragment() != 1) { // onCreateView called twice, dont know why... workaround to solve this problem
((CollectActivity) getActivity()).swipeFragment();
}
}
}
}
};
getContext().registerReceiver(udpMessages, new IntentFilter("UDP.MESSAGES.COLLECT"));
}
#Override
public void onPause() {
super.onPause();
getContext().unregisterReceiver(udpMessages);
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
}

how should use getApplication method in PagerAdapter

I have a problem when I want to use getApplication() for this class, error is take plaaaaaaace...what should I use instead of getApplication() (Becaus I want to use the method of TestClass is named setNamePermit) or how I should setNamePermit() method of test class.
public class CustomSwipeAdapter01 extends PagerAdapter{
private int[] image_Resources = {R.drawable.sample_01,R.drawable.sample_02,R.drawable.sample_03,R.drawable.sample_04,R.drawable.sample_05,R.drawable.sample_06,R.drawable.sample_07};
private Context ctx;
private LayoutInflater layoutInflater;
public TestClass app;
public CustomSwipeAdapter01(Context ctx) {
this.ctx = ctx;
}
#Override
public int getCount() {
return image_Resources.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return (view == (RelativeLayout) o);
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View item_view=layoutInflater.inflate(R.layout.activity_story01,container,false);
ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
TextView textView=(TextView)item_view.findViewById(R.id.image_count);
Button btn_back_story01 = (Button) item_view.findViewById(R.id.btn_back_story01);
imageView.setImageResource(image_Resources[position]);
int itemNo=position+1;
textView.setText(itemNo + "/" + getCount());
container.addView(item_view);
//what should use instead of getApplication() in below line:
app = (TestClass)getApplication();
btn_back_story01.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Activity) ctx).finish();
app.setNewPermit(false);
ctx.startActivity(new Intent(ctx, MainStory01.class));
}
});
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
}
Test Class is:
public class TestClass extends Application {
public Boolean getMedia_state() {
return media_state;
}
public void setMedia_state(Boolean media_state) {
this.media_state = media_state;
}
Boolean media_state;
Boolean checkPlaying;
public Boolean getNewPermit() {
return newPermit;
}
public void setNewPermit(Boolean newPermit) {
this.newPermit = newPermit;
}
Boolean newPermit;
MediaPlayer media;
#Override
public void onCreate() {
super.onCreate();
setMedia_state(true);
setNewPermit(true);
media = new MediaPlayer();
media = MediaPlayer.create(getApplicationContext(), R.raw.music);
}
public void musicRestart() {
media = MediaPlayer.create(getApplicationContext(), R.raw.music);
media.start();
media.setLooping(true);
}
public void musicPlay() {
media.start();
media.setLooping(true);
}
public boolean checkPlaying() {
if (media.isPlaying()) {
checkPlaying = true;
} else {
checkPlaying = false;
}
return checkPlaying;
}
public void musicStop() {
media.stop();
}
}
TestClass tc = new TestClass();
Accessing methods in TestClass:
tc.setNewPermit(false);
UPDATE: in your pager adapter, you can now pass any of those values around. For example, change your btn_back_story01 onClick() to:
btn_back_story01.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CustomSwipeAdapter01.this, MainStory01.class);
intent.putExtra("is_new_permit", tc.getNewPermit());
startActivity(intent);
}
});
In MainStory01 activity's onCreate() you can now get the extras passed in your Intent, via Bundle...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
boolean isNewPermit = bundle.getBoolean("is_new_permit");
}
}
There may be some errors in the code, I am not at my work computer at the moment, but this should give you an idea of how to proceed.

I can not create buttons for possible answers android studio

I have an issue with a small project in android studio where on animal drawings guess things etc. But guess I have to write the name to accept a button and image lights up and goes to the next , what I want you to show me an answer with buttons say 3 button 1 is the correct answer and the other two false I took a long time with this and even I can not do it if I would appreciate any help
Here the code of the class where the shadows and images run
public class Categoria extends Activity {
public static String[] nombre_cosa={"cerdo","ave","caballo","conejo","elefante","gallina","gato",
"rana","perro","pato","oveja","leon","jirafa",
"raton","vaca","autobus","automovil","avion","bicicleta","camioneta",
"casa","celular","guitarra","motocicleta","silla","television","durazno","fresa","mango",
"uvas","sandia","platano","coco","pera","naranja","manzana",
"bart","batman","cerebro","chavo","goku","homero","marge",
"patricio","pepa","phineas","quico","spiderman","thor","superman"};
public static String[] sombra_cosa={"s_cerdo","s_ave","s_caballo","s_conejo","s_elefante","s_gallina","s_gato",
"s_rana","s_perro","s_pato","s_oveja","s_leon","s_jirafa",
"s_raton","s_vaca","s_autobus","s_automovil","s_avion","s_bicicleta","s_camioneta",
"s_casa","s_celular","s_guitarra","s_motocicleta","s_silla","s_television","s_durazno","s_fresa","s_mango",
"s_uvas","s_sandia","s_platano","s_coco","s_pera","s_naranja","s_manzana",
"s_bart","s_batman","s_cerebro","s_chavo","s_goku","s_homero","s_marge",
"s_patricio","s_pepa","s_phineas","s_quico","s_spiderman","s_thor","s_superman"};
public static boolean[] estado={false,false,false,false,false,false,
false,false,false,false,false,false,
false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false};
public static int cosas_adivinadas=0;
private int intentos=3;
private Button aceptar;
private TextView mensaje_intentos,mensaje_cuenta;
private EditText usuario_cosa;
private int numero_generado=0;
private ImageView miimagen;
private MediaPlayer reproductor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_categoria);
aceptar=(Button) findViewById(R.id.btnaceptar);
mensaje_intentos=(TextView) findViewById(R.id.lblintentos);
mensaje_cuenta=(TextView) findViewById(R.id.lblcuenta);
usuario_cosa=(EditText) findViewById(R.id.txtcosa);
miimagen=(ImageView) findViewById(R.id.imgcosa);
CargarPreferencias();
new MiTarea().execute();
reproductor= MediaPlayer.create(this,R.raw.yansha);
reproductor.setLooping(true);
reproductor.start();
mensaje_intentos.setText("Tiene " + intentos + " intentos");
aceptar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String nombre=usuario_cosa.getText().toString().toLowerCase();
if(nombre.equals(nombre_cosa[numero_generado]))
{
establecer_cosa(numero_generado);
estado[numero_generado]=true;
cosas_adivinadas++;
esperar();
}
else
{
Toast.makeText(getApplicationContext(), "Incorrecto", Toast.LENGTH_SHORT).show();
intentos=intentos-1;
mensaje_intentos.setText("Tiene " + intentos + " intentos");
}
if (intentos==0)
{
removerPreferencias();
Intent i = new Intent(Categoria.this,Perder.class);
startActivity(i);
finish();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
reproductor.start();
}
public void esperar()
{
new CountDownTimer(5000,1000)
{
#Override
public void onTick(long millisUntilFinished) {
mensaje_cuenta.setText("Generando en " + (millisUntilFinished/1000));
}
#Override
public void onFinish() {
if (cosas_adivinadas==nombre_cosa.length)
{
finish();
}
else
{
new MiTarea().execute();
mensaje_cuenta.setText("");
usuario_cosa.setText("");
}
}
}.start();
}
public void CargarPreferencias()
{
SharedPreferences mispreferencias = getSharedPreferences("PreferenciaCosa", Context.MODE_PRIVATE);
intentos=mispreferencias.getInt("intentos",3);
cosas_adivinadas=mispreferencias.getInt("adivinados",0);
for (int i=0;i<nombre_cosa.length;i++)
{
estado[i]=mispreferencias.getBoolean(nombre_cosa[i],false);
}
}
public void GuardarPreferencias()
{
SharedPreferences mispreferencias = getSharedPreferences("PreferenciaCosa", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mispreferencias.edit();
editor.putInt("intentos",intentos);
editor.putInt("adivinados",cosas_adivinadas);
for (int i=0;i<nombre_cosa.length;i++)
{
editor.putBoolean(nombre_cosa[i], estado[i]);
}
editor.commit();
}
private void establecer_cosa(int numero)
{
int resId = getResources().getIdentifier(nombre_cosa[numero], "drawable", getPackageName());
miimagen.setImageResource(resId);
}
private void establecer_sombra(int numero)
{
int resId = getResources().getIdentifier(sombra_cosa[numero], "drawable", getPackageName());
miimagen.setImageResource(resId);
}
private void removerPreferencias()
{
SharedPreferences settings = getSharedPreferences("PreferenciaCosa", Context.MODE_PRIVATE);
settings.edit().clear().commit();
}
#Override
protected void onStop() {
if (intentos==0)
{
removerPreferencias();
}
else
{
GuardarPreferencias();
}
reproductor.pause();
super.onStop();
}
#Override
protected void onDestroy() {
if (reproductor.isPlaying())
{
reproductor.stop();
reproductor.release();
}
super.onDestroy();
}
private class MiTarea extends AsyncTask<Void, Void, Void> {
private int valor_generado;
#Override
protected Void doInBackground(Void... params) {
do {
valor_generado=((int)(Math.random()*nombre_cosa.length));
}while(estado[valor_generado]);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
numero_generado = valor_generado;
establecer_sombra(valor_generado);
super.onPostExecute(aVoid);
}
}
}
Create a variable like this:
boolean isTextCorrect = false;
In default, the button is invisible.
Implement a listener. If the text in the textfield changed and its correct switch isTextCorrect to true and make the button visible (clickable).
if(isTextCorrect){
button.setVisible(View.VISIBLE);
}

How to update a textview in a dialog fragment from an activity?

It may be a silly question but I didn't find a good way to update a dialogfragment's textview from an activity in my android app.
What I'd like to do is to update the textview every second with a counter value and once the time elapsed, a Runnable closes the dialog fragment.
The dialog is closed once the time is elapsed, no problem but I cannot update the textview I want.
Here's my code for the dialog:
public class AlertDialog extends DialogFragment {
private String message = null;
private String title = null;
private ImageView imgV = null;
private TextView msgTv = null;
private TextView counterTv = null;
private Button okBtn = null;
private int imageId = 0;
public static int AUTOMATIC_CLOSE = 100001;
private AlertDialogListener mDialogListener;
public void setImage(int i){
imageId = i;
}
public void setContent(String ttl, String msg){
message = msg;
title = ttl;
}
public boolean hasContent(){
return message != null && title != null;
}
public AlertDialog(){
}
public void performClick(){
okBtn.performClick();
}
public void updateField(String text){
counterTv.setText(text);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog, container);
msgTv = (TextView)v.findViewById(R.id.textDialog);
imgV = (ImageView)v.findViewById(R.id.imageDialog);
counterTv = (TextView)v.findViewById(R.id.timeCounterDialog);
if(imageId != 0)
imgV.setImageResource(imageId);
else
imgV.setImageResource(R.drawable.error_icon);
if(hasContent()){
msgTv.setText(message);
getDialog().setTitle(title);
}
else{
getDialog().setTitle("ERROR");
msgTv.setText("An unexcepted error occured");
}
okBtn = (Button)v.findViewById(R.id.validateButton);
okBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDialogListener != null){
mDialogListener.onFinishedDialog();
}
}
});
return v;
}
#Override
public void onStart() {
mDialogListener.onStartedDialog();
super.onStart();
}
#Override
public void onAttach(Activity activity) {
mDialogListener = (AlertDialogListener) activity;
super.onAttach(activity);
}
#Override
public void onDetach() {
mDialogListener = null;
super.onDetach();
}
public interface AlertDialogListener{
void onStartedDialog();
void onFinishedDialog();
}
}
And this is how I launch it:
class myActivity extends Activity implements AlertDialogListener{
protected void onCreate(Bundle savedInstanceState){
"""some init stuff"""
button.setOnClickListener(new View.OnClickListener() {
showAlertDialog();
}
}
#Override
public void onStartedDialog() {
AutoCloseRunnable mAutoClose = new AutoCloseRunnable();
mHandler.postDelayed(mAutoClose, 1000);
}
#Override
public void onFinishedDialog() {
this.finish();
}
private void showAlertDialog(){
FragmentManager fm = getFragmentManager();
mAlertDialog = new AlertDialog();
mAlertDialog.setContent("No Connection available", "Please enable your internet connection.");
mAlertDialog.setImage(R.drawable.error_icon);
mAlertDialog.show(fm, "fragment_alert");
}
private void updateAlertDialog(String text){
mAlertDialog.updateField(text);
}
private void autoCloseAlertDialog(){
mAlertDialog.performClick();
}
public class AutoCloseRunnable implements Runnable{
#Override
public void run() {
int closeCpt = 10;
while(closeCpt >= 0){
try {
Thread.sleep(1000);
updateAlertDialog("Will close automatically in " + closeCpt + " seconds.");
closeCpt--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
autoCloseAlertDialog();
}
}
}
Does anyone know how to proceed?
I solved it, simply use asynctask it's easier to handle UI updates like this.

Categories

Resources