0

I have two buttons "Employee" and "Employer" in an activity named "Selection", if "Employee" button is pressed then two buttons "addEmployee" and "generateReport" from "Home" activity should be disabled.

Selection Activity

public class Selection extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selection);

    final Button btn_Employee = (Button) findViewById(R.id.btn_Employee);
    Button btn_Employer = (Button) findViewById(R.id.btn_Employer);

    btn_Employee.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vEmployee) {
            if(btn_Employee.isPressed())
            {
                //markAttendance and generateReport button should be disabled

                Intent i = new Intent(Selection.this, HomeActivity.class);
                             startActivity(i);
            }

    }
    });

    btn_Employer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vEmployer) {

        }
    });
}
}

Home Activity

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    ImageButton markAttendance = (ImageButton) findViewById(R.id.markAttendance);
    ImageButton viewHolidays = (ImageButton) findViewById(R.id.viewHolidays);
    ImageButton addEmployee = (ImageButton) findViewById(R.id.addEmployee);
    ImageButton generateReport = (ImageButton) findViewById(R.id.generateReport);

    Toolbar toolbar = (Toolbar) findViewById(R.id.sign_in_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(getTitle());

    markAttendance.setOnClickListener(this);
    viewHolidays.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            holidays();
        }
    });
  addEmployee.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
            AddEmployee();
      }
  });
   generateReport.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           report();
       }
   });
}

private void AddEmployee() {
    Intent addIntent =  new Intent(this, EmployeeDetails.class);
    startActivity(addIntent);
}

private void report() {
    Intent webIntent = new Intent(this, TempWeb.class);
    startActivity(webIntent);
}

private void holidays() {
Intent holidayIntent = new Intent(this, Holiday.class);
    startActivity(holidayIntent);
}

@Override
public void onClick(View v) {
    attendance();
}

private void attendance() {
    Intent attendanceIntent = new Intent(this, MarkAttendance.class);
    startActivity(attendanceIntent);
}
}

Any help is appreciated!

AskNilesh
  • 58,437
  • 15
  • 99
  • 129
Vismay Patil
  • 46
  • 2
  • 13
  • 2
    Just pass which button is pressed in your Intent to Home activity and in your home activity get the data from intent and apply your disability of buttons using if condition – Kapil G Jul 24 '17 at 06:00
  • You can put your value in `sharedpreferences` and access it to main activity. !! Or you can use `Intent` to pass your data and get data on your main activity !!! – rushank shah Jul 24 '17 at 06:01

8 Answers8

2

You can do it by passing some data through your intent that can identify in Home Acitivity, which button was pressed in Selection Activity.

In Selection Activity on employee button pressed:

Intent i = new Intent(Selection.this, HomeActivity.class);
intent.putExtra("IS_EMPLOYEE", true);
startActivity(i);

In Home Activity check whether employee button if pressed or not, and do things accordingly:

if(getIntent().getExtras().getBoolean("IS_EMPLOYEE")){
    // do what you want to do on employee button press.
}
else{
    // do what you want to do on employer button press.
}

Also pass false value through intent when Selection Activity, employer button is pressed.

jack jay
  • 2,395
  • 1
  • 11
  • 26
1

Use setEnabled(false) to the buttons you want to disable from within the onClickListener

If you want to get more ease of use to know if the button is selected/pressed or not, you can use setSelected(true) (or false) functionality of button so that you can always ask it if its selected

Hope it helps :)

BNAndroid
  • 150
  • 4
0

Just pass which button is pressed in your Intent to Home activity and in your home activity get the data from intent and apply your disability of buttons using if condition.

So if your Home Activity is only opened by Selection activity then you can use Intents to pass this data, otherwise you can use SharedPreference to save the data and then retrieve this preference in your Home activity.

To pass data and retrieve data through intent refer here - Passing Through intent

To Store and retrieve through Shared Preference refer here - Shared Preference

Kapil G
  • 3,823
  • 1
  • 14
  • 29
0

Pass A Value Through Intent to check which button is called

btn_Employee.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View vEmployee) {
        if(btn_Employee.isPressed())
        {
            //markAttendance and generateReport button should be disabled

            Intent i = new Intent(Selection.this, HomeActivity.class);
              i.putExtra("BUTTON_CLICK","EMPLOYEE");
                         startActivity(i);
        }

}
});

btn_Employer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View vEmployer) {


            Intent i = new Intent(Selection.this, HomeActivity.class);
              i.putExtra("BUTTON_CLICK","EMPLOYER");
                         startActivity(i);
    }
});

And In home activity to get intent value

String buttonValue = getIntent().getStringExtra("BUTTON_CLICK");
0

Selection Activity

btn_Employee.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View vEmployee) {
        if(btn_Employee.isPressed())
        {
            //markAttendance and generateReport button should be disabled

            Intent i = new Intent(Selection.this, HomeActivity.class);
                         startActivity(i).putExtra("addEmployee", false).putExtra("reports", false));
        }

}
});

Home Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

ImageButton markAttendance = (ImageButton) findViewById(R.id.markAttendance);
ImageButton viewHolidays = (ImageButton) findViewById(R.id.viewHolidays);
ImageButton addEmployee = (ImageButton) findViewById(R.id.addEmployee);
ImageButton generateReport = (ImageButton) findViewById(R.id.generateReport);

Toolbar toolbar = (Toolbar) findViewById(R.id.sign_in_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getTitle());

//getExtra value
boolean addEmployee = getIntent().getBooleanExtra("addEmployee", false);
boolean reports = getIntent().getBooleanExtra("reports", false);

if(addEmployee){
    addEmployee.setEnable(false);
}

if(reports){
    generateReport.setEnable(false);
}

}
MinnuKaAnae
  • 1,614
  • 3
  • 20
  • 35
0

Interaction between two classes without Intent:

Create Helper class like this :

public class ChangeButtonStatusHelper {

    public interface ChangeButtonsListener{
        void twoButtonsDisabled();
    }

    public static ChangeButtonStatusHelper mInstance;

    private ChangeButtonsListener mListener;

    private boolean mState;

    private ChangeButtonStatusHelper(){
        //do nothing
    }

    public static ChangeButtonStatusHelper getInstance(){
        if(mInstance == null){
            mInstance = new ChangeButtonStatusHelper();
        }

        return  mInstance;
    }


    public void setListener(ChangeButtonsListener mListener){
        this.mListener = mListener;
    }

    public void stateChange(boolean mState){
        if(mListener!=null){
            this.mState = mState;
            notifyStateChange();
        }
    }

    public boolean getState(){
        return mState;
    }

    private void notifyStateChange() {
        mListener.onPriorityChanged();
    }

}

Write below code in onCreate () of HomeActivity.class

ChangeButtonStatusHelper.getInstance().setListener(new ChangeButtonsListener.twoButtonsDisabled() {
        @Override
        public void onPriorityChanged() {

            if (ChangeButtonStatusHelper.getInstance().getState()) {

                **Disable your buttons here**
            }

        }
    });

in SelectionActivity.class

btn_Employee.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View vEmployee) {
            if(btn_Employee.isPressed())
            {
                //markAttendance and generateReport button should be disabled

                 ChangeButtonStatusHelper.getInstance().stateChange(true);
            }

    }
    });
Dharma Sai Seerapu
  • 1,075
  • 2
  • 12
  • 29
0
//In Selection Activity
btn_Employee.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View vEmployee) {
        if(btn_Employee.isPressed())
        {
            //markAttendance and generateReport button should be disabled

            Intent i = new Intent(Selection.this, HomeActivity.class);
            i.putExtra("IsEmployeeBtnPressd",true);
                         startActivity(i);

        }

}


// In Home Activity.
class HomeActivity extends  extends AppCompatActivity {
 @Override
 oncreate(Bundle bundle){
 ImageButton markAttendance = (ImageButton) findViewById(R.id.markAttendance);
 ImageButton generateReport = (ImageButton) findViewById(R.id.generateReport)
  boolean isEmployeeBtnPressed=getIntent().getBooleanExtra("IsEmployeeBtnPressd",false);
  if(isEmployeeBtnPressed){
     //You should disable
      markAttendance.setEnabled(false);
      generateReport.setEnabled(false); 
   }
 }

}
Santhosh
  • 140
  • 7
0

If you want to that without having to pass an Intent you could achieve this behavior by registering to OnSharedPreferenceChangeListener. here's an example:

HomeActivity.java:

public class HomeActivity extends AppCompatActivity implements View.OnClickListener, 
SharedPreferences.OnSharedPreferenceChangeListener{

    public static final String BTN_EMPLOYEE_PREFS_TAG = "employeeTag";
    private ImageButton addEmployee;
    private ImageButton generateReport;
    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        addEmployee = (ImageButton) findViewById(R.id.addEmployee);
        generateReport = (ImageButton) findViewById(R.id.generateReport);
        .
        .
        .
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        .
        .
        .
     }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if(BTN_EMPLOYEE_PREFS_TAG.equals(key)){
        // listens to any changes made on this tag
            boolean enableOrDisable = prefs.getBoolean(BTN_EMPLOYEE_PREFS_TAG, false);
            addEmployee.setEnabled(enableOrDisable);
            generateReport.setEnabled(enableOrDisable);
        }
    }
    .
    .
    .
}

Selection.java:

public class Selection extends AppCompatActivity{

    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selection);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        findViewById(R.id.btn_Employee).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vEmployee) {
                if(btn_Employee.isPressed()){
                    prefs.edit().putBoolean(BTN_EMPLOYEE_PREFS_TAG, false).apply();
                    // rest of your logic goes here.
                }
            }
        });
        .
        .
        .
    }
    .
    .
    .
}
Royz
  • 145
  • 15