0

Hello i'm working on my android project. I have created an activity where the use gets four buttons, n i want the user to select any one. I have done this by setting other buttons to .setClickable(false). But when I restart or say relaunch the app again all the buttons get enabled. I want that when user selects a button it should save its choice, so that on retstart of app, the user doesn't get confused. Below is the piece of code:

Notifications.class

            package com.mateoj.multiactivitydrawer;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.NotificationCompat;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mateoj.multiactivitydrawer.department.dept_1styeartab;
import com.pushbots.push.Pushbots;

public class notifications extends BaseActivity {
    private WebView webView;

    Button clickButton;
    Button clickButton1;
    Button clickButton2;
    Button clickButton3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Pushbots.sharedInstance().init(this);
        Pushbots.sharedInstance().setCustomHandler(customHandler.class);
        setContentView(R.layout.activity_notifications);
        final SharedPreferences preferences = getSharedPreferences("com.mateoj.multiactivitydrawer", MODE_PRIVATE);
        final SharedPreferences.Editor editor = preferences.edit();

        clickButton = (Button) findViewById(R.id.ncs);
        clickButton1 = (Button) findViewById(R.id.nmech);
        clickButton2 = (Button) findViewById(R.id.nece);
        clickButton3 = (Button) findViewById(R.id.neee);



        clickButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent show = new Intent(getApplicationContext(), noti_cse.class);
                Pushbots.sharedInstance().tag("cse");
                Pushbots.sharedInstance().untag("mech");
                Pushbots.sharedInstance().untag("ece");
                Pushbots.sharedInstance().untag("eee");
                editor.putString("session", "cse").commit();
                editor.commit();
                startActivity(show);


            }
        });
        clickButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent show = new Intent(getApplicationContext(), noti_mech.class);

                Pushbots.sharedInstance().tag("mech");
                Pushbots.sharedInstance().untag("ece");
                Pushbots.sharedInstance().untag("eee");
                Pushbots.sharedInstance().untag("cse");
                editor.putString("session", "mech").commit();
                editor.commit();

                startActivity(show);


            }
        });
        clickButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent show = new Intent(getApplicationContext(), noti_ece.class);

                Pushbots.sharedInstance().tag("ece");
                Pushbots.sharedInstance().untag("mech");
                Pushbots.sharedInstance().untag("eee");
                Pushbots.sharedInstance().untag("cse");
                editor.putString("session", "ec").commit();
                editor.commit();
                startActivity(show);


            }
        });
        clickButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent show = new Intent(getApplicationContext(), noti_eee.class);

                Pushbots.sharedInstance().tag("eee");
                Pushbots.sharedInstance().untag("ece");
                Pushbots.sharedInstance().untag("mech");
                Pushbots.sharedInstance().untag("cse");
                editor.putString("session", "eee").commit();
                editor.commit();

                startActivity(show);


            }
        });
        onStartUp();



    }
    private void onStartUp()
    {
        SharedPreferences sharedPreferences = getSharedPreferences("com.mateoj.multiactivitydrawer", Context.MODE_PRIVATE);
        String str = sharedPreferences.getString("session", "");
        if (str.equals("cs")) {
            clickButton.setClickable(true);
            clickButton1.setClickable(false);
            clickButton2.setClickable(false);
            clickButton3.setClickable(false);

        } else if (str.equals("mech")) {
            clickButton.setClickable(false);
            clickButton1.setClickable(true);
            clickButton2.setClickable(false);
            clickButton3.setClickable(false);
        } else if (str.equals("ec")) {
            clickButton.setClickable(false);
            clickButton1.setClickable(false);
            clickButton2.setClickable(true);
            clickButton3.setClickable(false);
        } else if (str.equals("eee")) {
            clickButton.setClickable(false);
            clickButton1.setClickable(false);
            clickButton2.setClickable(false);
            clickButton3.setClickable(true);
        }

    }














  /*  private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

    }  */



    @Override
    protected boolean useDrawerToggle() {
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_credits)
            return true;

        if (item.getItemId() == android.R.id.home)
            onBackPressed();

        return super.onOptionsItemSelected(item);
    }


}

So what should be changed here

sunny
  • 127
  • 1
  • 6

3 Answers3

0

You can use the SharedPreferences class to save the user selection.This video can help you get started - https://www.youtube.com/watch?v=riyMQiHY3V4 .Just save the user selection of button in a key value pair with sharedpreferences class.On activity restart or relaunch just check for the value in the shared preferences instance and accordingly set the button setClickable(true) or setClickable(false).

EDIT:

clickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              //same code as above without the setClickable() on buttons
             //remove the button.setClickable(false); for rest of the buttons
             editor.putString("session", "akash").commit();
             editor.commit();
             startActivity(show);   //Start the activity here
       }
}):

Repeat this for all the click listeners and change the "akash" to something different (according to need) for each button click listener.Then use the method

private void onStartUp(){
  SharedPreferences sharedPreferences = getSharedPreferences("com.mateoj.multiactivitydrawer", Context.MODE_PRIVATE);
  String str=sharedPreferences .getString("session","akash");
  if(str.equals("akash")){
    //set buttons setClickable(true) or setClickable(false)
  } else if(str.equals("..."))     //replace "..." with other strings which can replace "akash"
   //set buttons setClickable(true) or setClickable(false)
 } //continue the else if logic till all conditions are met
} 

call onStartUp(); in onCreate(); after setting the on click listeners for the buttons .

mik dass
  • 381
  • 3
  • 16
  • @sunny Write the onstartup code after setting the clickButton3.setOnClickListener(); in the onCreate(); method. – mik dass Apr 19 '16 at 04:16
  • i have made changes please see – sunny Apr 19 '16 at 04:17
  • the code works, if i properly exit the app. But let's say somebody chooses the button and doesn't close the app properly. if the user presses the home button to exit the app directly then it doesn't save the button. what to do in that case – sunny Apr 19 '16 at 04:32
  • basically i'm talking about onPause(),onStop() and onDestroy() – sunny Apr 19 '16 at 05:06
  • @sunny Try to call the onstartup(); method also in the onResume(); and onStart(); methods.Tell me if it works. – mik dass Apr 19 '16 at 12:40
  • is it possible that when i select a button then the only intent inside it should fire up. Instead of again selecting the departemtns – sunny Apr 19 '16 at 13:42
  • @sunny I didn't get what you mean by the last comment.Maybe you can try to save the department selection in a SharedPreference and retrieve it from there. – mik dass Apr 19 '16 at 17:24
0

One thing you could do is save the state of the Button (Everytime you/user click on the Button save the true or false on your SharedPreferences) and everytime you start the Activity the value of setClickable() put the result of your SharedPrefernce.

That's an option, but there are a lot of ways to do it, that's one of them.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

I don't know this framework that you are using (Pushbots), but I think you are forgeting to check which tag is saved in Pushbots during the activity method onCreate() and enable or disable the buttons according to it. You are saving the tags on Pushbots button you never check which tag is saved on it to enable or disable the button.

I'd recommend you to use SharedPreferences directly instead of use this framework, even I don't know it. SharedPreferences is so simple to use that I don't see any reason to use frameworks.

Tássio Auad
  • 111
  • 5