SilSur
SilSur

Reputation: 509

Enable or Disable buttons programmatically based on the active (i.e. visible) fragments

I am relatively new to Android programming and really need assistance with this problem I am trying to solve. I have an application that revolves around three base activities. In the one activity - the largest by far - I have ten buttons in the UI that implement 16 different fragments (each with their own UIs) in a logical (i.e. step-by-step) fashion depending on what the user wants to accomplish.

So, for each of these 16 different fragments I need to activate and deactivate (enable and disable) various buttons in the UI depending on what the user is allowed to do when a specific fragment is active (i.e. at the front of the view stack or in view or visible to the user). In actual fact, I need to change (i.e. set) the states (i.e. enabled state) of all 10 buttons everytime a new fragment is loaded into the fragment placeholder/container to give the user a clear idea of where they are in this logical process of steps.

Please note: all 10 buttons are visible (i.e. should be visible) at all times (always) and must only be enabled or disabled depending on the fragment that is currently loaded or currently in view (i.e. displayed/visible to the user). OK, so let's look at some code...

Here is the Activity "DmpAct.java" (complete code to date) that I was referring to above...

package com.carzy.carzyapp;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DmpAct extends Activity implements OnClickListener {

Fragment fragment;
Fragment newFragment;
FragmentManager fragMan;
Button birtListBtn, evenListBtn, appoListBtn, todoListBtn, specListBtn, dmpExitBtn;

@SuppressLint({ "NewApi", "CommitTransaction" })
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // Hide the Title Bar of the Application --> Must come before setting the Layout...
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Hide the Status Bar of Android OS --> Can also be done later...
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


    // Now you can draw the second Layout --> HomeScreen of the Application...
    setContentView(R.layout.dmp_act);


    // Instantiate the FragmentManager and FragmentTranstion...
    FragmentManager fragMan = getFragmentManager();
    FragmentTransaction fragTrans = fragMan.beginTransaction();


    // Now you need to define or set the initial/start fragment to be loaded when the view is laid out...
    DMPWelcFrag startFragment = new DMPWelcFrag();
    fragTrans.add(R.id.dmpFragContainer, startFragment);
    fragTrans.commit();


    // Instantiate (or get references to) all buttons laid out in this Activity
    Button birtListBtn = (Button) findViewById(R.id.dmp_bir_btn);
    birtListBtn.setOnClickListener(this);

    Button evenListBtn = (Button) findViewById(R.id.dmp_eve_btn);
    evenListBtn.setOnClickListener(this);

    Button appoListBtn = (Button) findViewById(R.id.dmp_app_btn);
    appoListBtn.setOnClickListener(this);

    Button todoListBtn = (Button) findViewById(R.id.dmp_tod_btn);
    todoListBtn.setOnClickListener(this);

    Button specListBtn = (Button) findViewById(R.id.dmp_spe_btn);
    specListBtn.setOnClickListener(this);

    Button dmpExitBtn = (Button) findViewById(R.id.dmp_exi_btn);
    dmpExitBtn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dmp, menu);
    return true;
}

@SuppressLint("NewApi")
@Override
public void onClick(View v) {

    // Fragment newFragment;

    // Set the DMP Fragment state here and pass it on to the FragmentTransaction module that follows..
    if (v.getId() == R.id.dmp_bir_btn) {
        newFragment = new BirtListFrag();
    }
    else if (v.getId() == R.id.dmp_eve_btn) {
        newFragment = new EvenListFrag();
    }
    else if (v.getId() == R.id.dmp_app_btn) {
        newFragment = new AppoListFrag();
    }
    else if (v.getId() == R.id.dmp_tod_btn) {
        newFragment = new TodoListFrag();
    }
    else if (v.getId() == R.id.dmp_spe_btn) {
        newFragment = new SpecListFrag();
    }
    else {
        newFragment = new DMPWelcFrag();
    }


    if (v.getId() == R.id.dmp_exi_btn) {
        Intent go2Main = new Intent(DmpAct.this, MainAct.class);                  
        startActivity(go2Main);
    }
    else;


    FragmentTransaction transact = getFragmentManager().beginTransaction();
    transact.replace(R.id.dmpFragContainer, newFragment, "activeFrag");
    transact.addToBackStack("activeFrag");
    primeRelativeBtns();
    transact.commit();

};

@SuppressLint("NewApi")
public void primeRelativeBtns() {
    if (newFragment.equals("dmpBirtListFragTag")) {
        birtListBtn.setEnabled(false); 
        evenListBtn.setEnabled(true);
        appoListBtn.setEnabled(true);
        todoListBtn.setEnabled(true);
        specListBtn.setEnabled(true);
    } 
    else if (newFragment.getTag() == "dmpEvenListFragTag") {
        birtListBtn.setEnabled(true); 
        evenListBtn.setEnabled(false);
        appoListBtn.setEnabled(true);
        todoListBtn.setEnabled(true);
        specListBtn.setEnabled(true);
    } else;
}

}

Please note...at the time of submitting this question I had only completed 6 of the 10 buttons used by this activity (as is obvious in the code above)...

Anyway...on we go...

Here is the UI that is implemented for this activity by the "dmp_act.xml" file...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:context=".DmpAct">

<LinearLayout
    android:id="@+id/leftButtonColumn"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.2705"
    android:gravity="center"
    android:paddingTop="5pt"
    android:paddingBottom="10pt"
    android:paddingLeft="8pt"
    android:paddingRight="8pt"
    android:orientation="vertical">

    <ImageView 
        android:id="@+id/dmp_cat_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.0180"
        android:contentDescription="@string/dmp_cat_sign"/>

    <Button
        android:id="@+id/dmp_bir_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_eve_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_app_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_tod_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_spe_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/dmpFragContainer"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5350"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingTop="8pt"
    android:paddingBottom="8pt">

    <!-- <ImageView
        android:id="@+id/dmp_wel_sta_wal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:alpha="1"
        android:contentDescription="@string/dmp_welc_wall"/> -->

</LinearLayout>

<LinearLayout
    android:id="@+id/rightButtonColumn"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.1795"
    android:gravity="center"
    android:paddingTop="20pt"
    android:paddingBottom="20pt"
    android:paddingLeft="8pt"
    android:paddingRight="8pt"
    android:orientation="vertical">

    <Button
        android:id="@+id/dmp_edi_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_sav_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_add_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_del_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_exi_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"/>
</LinearLayout>

Before posting here I tried all the available solution already discussed here in SO...but to no avail. So to summarize the problem...I basically need a really good way (best-practise) of setting or changing all the buttons' enabled states everytime a new fragment is loaded (and displayed) to the user in the "dmpFragContainer".

I want to apologize beforehand if it seems like I am talking down to anyone, but I want to make sure everyone that reads this post will clearly understand the problem at hand. Please feel free to shred my code if you think I can implement better "best-practise" code structure - as I said before...I am new to Android coding and need all the help I can get.

Appreciate the help...

Cheers,

SilSur.

Upvotes: 3

Views: 11197

Answers (1)

SilSur
SilSur

Reputation: 509

Thweeet! I solved the problem. Hello all... I managed to solve this problem I had with enabling or disabling all ten buttons just the way I want them to be disabled everytime a new fragment is loaded and visible to the user. The solution is that you have to implement this "enable/disable" code in the class files for each of the fragments. Pretty straightforward actually... So without further ado here is the solution in a more understandable fashion [code]...

This is the code from only one "BirtListFrag.java" of the 16 fragments I have running through my application...

package com.carzy.carzyapp;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

@SuppressLint("NewApi")
public class BirtListFrag extends Fragment {

public static Button BirCatBtn;
public static Button EveCatBtn;
public static Button AppCatBtn;
public static Button TodCatBtn;
public static Button SpeCatBtn;
public static Button EdiFunBtn;
public static Button SavFunBtn;
public static Button AddFunBtn;
public static Button DelFunBtn;
public static Button ExiFunBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.dmp_birt_list_frag, container, false);

}

@Override 
public void onActivityCreated (Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    /*** Get references to all the buttons you want to manipulate everytime this Fragment is loaded & visible in the viewport ***/
    BirCatBtn = (Button) getActivity().findViewById(R.id.dmp_bir_btn);
    EveCatBtn = (Button) getActivity().findViewById(R.id.dmp_eve_btn);
    AppCatBtn = (Button) getActivity().findViewById(R.id.dmp_app_btn);
    TodCatBtn = (Button) getActivity().findViewById(R.id.dmp_tod_btn);
    SpeCatBtn = (Button) getActivity().findViewById(R.id.dmp_spe_btn);
    EdiFunBtn = (Button) getActivity().findViewById(R.id.dmp_edi_btn);
    SavFunBtn = (Button) getActivity().findViewById(R.id.dmp_sav_btn);
    AddFunBtn = (Button) getActivity().findViewById(R.id.dmp_add_btn);
    DelFunBtn = (Button) getActivity().findViewById(R.id.dmp_del_btn);
    ExiFunBtn = (Button) getActivity().findViewById(R.id.dmp_exi_btn);


    /*** Now you can manipulate whatever attributes (***See Below***) of the buttons u created references to ABOVE ***/
    BirCatBtn.setEnabled(false);
    EveCatBtn.setEnabled(true);
    AppCatBtn.setEnabled(true);
    TodCatBtn.setEnabled(true);
    SpeCatBtn.setEnabled(true);
    EdiFunBtn.setEnabled(false);
    SavFunBtn.setEnabled(false);
    AddFunBtn.setEnabled(true);
    DelFunBtn.setEnabled(false);
    ExiFunBtn.setEnabled(true);
     }
}

As you can see...it is pretty logical. Anyway. I owe this breakthrough to "Greensy" who answered a question posted by "ColorFrog" on a similar problem that I had ==> here is the jump if you want to check out what I am talking about..."Disabling buttons in a Fragment".

Anywho...at the time of posting this reply I had been a member of S.O. for only 5 days and thus I couldn't "upvote" Greensy's answer as I didn't have that privilege at the time. So, since his answer really helped me solve my problem, I decided the least I could do was post this reply and thank him/her upfront. So thanks mate...I definitely owe u something (hmm...a beer maybe?!). Who knows. Anyway, I hope a will get to return the favor one day.

Cheers,

Shore-T.

Upvotes: 2

Related Questions