title: Enable or Disable buttons programmatically based on the active (i.e. visible) fragments
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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... </p>
  
  <p>Here is the Activity "DmpAct.java" (complete code to date) that I was referring to above...</p>
  
  <pre><code>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 --&gt; Must come before setting the Layout...
      requestWindowFeature(Window.FEATURE_NO_TITLE);
  
      // Hide the Status Bar of Android OS --&gt; Can also be done later...
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
  
      // Now you can draw the second Layout --&gt; 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;
  }
  
  }
  </code></pre>
  
  <p>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)...</p>
  
  <p>Anyway...on we go...</p>
  
  <p>Here is the UI that is implemented for this activity by the "dmp_act.xml" file...</p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;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"&gt;
  
      &lt;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"/&gt;
  
      &lt;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"/&gt;
  
      &lt;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"/&gt;
  
      &lt;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"/&gt;
  
      &lt;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"/&gt;
  
      &lt;Button
          android:id="@+id/dmp_spe_btn"
          android:layout_width="wrap_content"
          android:layout_height="23pt"
          android:layout_weight="0.0164"/&gt;
  &lt;/LinearLayout&gt;
  
  &lt;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"&gt;
  
      &lt;!-- &lt;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"/&gt; --&gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;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"&gt;
  
      &lt;Button
          android:id="@+id/dmp_edi_btn"
          android:layout_width="wrap_content"
          android:layout_height="0pt"
          android:layout_weight="1"
          android:layout_marginBottom="15pt"/&gt;
  
      &lt;Button
          android:id="@+id/dmp_sav_btn"
          android:layout_width="wrap_content"
          android:layout_height="0pt"
          android:layout_weight="1"
          android:layout_marginBottom="15pt"/&gt;
  
      &lt;Button
          android:id="@+id/dmp_add_btn"
          android:layout_width="wrap_content"
          android:layout_height="0pt"
          android:layout_weight="1"
          android:layout_marginBottom="15pt"/&gt;
  
      &lt;Button
          android:id="@+id/dmp_del_btn"
          android:layout_width="wrap_content"
          android:layout_height="0pt"
          android:layout_weight="1"
          android:layout_marginBottom="15pt"/&gt;
  
      &lt;Button
          android:id="@+id/dmp_exi_btn"
          android:layout_width="wrap_content"
          android:layout_height="0pt"
          android:layout_weight="1"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>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".</p>
  
  <p>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.</p>
  
  <p>Appreciate the help...</p>
  
  <p>Cheers, </p>
  
  <p>SilSur.</p>