title: How do I change my Activity's fragment container's background with a custom dialog with code?
body: <p>Fairly new to Android and I am trying to do some background color changes. Basically I have a main activity that only has a FrameLayout in it's xml. When the activity is created it opens up a fragment for my program. I have a menu item that when clicked pops a dialog box with 3 seekbars(red, green, blue). I want to change the background color to whatever the seekbars position is. I have all the code finished for the seekbars and I know it works on a simple app I created. For reasons to me unknown my app fails when i try to open the dialog box. What is the proper way to set this up in the Main Activity? I want the user to be able to change the background color whenever they want. All my fragment layouts are transparent. This is the tutorial I have been working off of. <a href="http://android-er.blogspot.com/2009/08/change-background-color-by-seekbar.html" rel="nofollow">http://android-er.blogspot.com/2009/08/change-background-color-by-seekbar.html</a> Any advice would be great. I think my problem is I do not fully understand how to access my main_activity's FrameLayout from with-in my MainActivity java class.</p>
  
  <p>activity_main.xml</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"
      tools:context=".MainActivity"
      android:orientation="vertical"
      android:background="#e3a153"&gt;
  
      &lt;FrameLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/fragmentView"&gt;&lt;/FrameLayout&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Color_seekbar_selecter.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/myScreen"
      &gt;
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Change color"
          /&gt;
      &lt;SeekBar
          android:id="@+id/mySeekingBar_R"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:max="255"
          android:progress="0"/&gt;
      &lt;SeekBar
          android:id="@+id/mySeekingBar_G"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:max="255"
          android:progress="0"/&gt;
      &lt;SeekBar
          android:id="@+id/mySeekingBar_B"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:max="255"
          android:progress="0"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>menu_main.xml</p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".MainActivity"&gt;
  
          &lt;item android:id="@+id/menu_settings"
              android:title="Green" /&gt;
          &lt;item android:id="@+id/menu_red"
              android:title="Red" /&gt;
          &lt;item android:id="@+id/menu_blue"
              android:title="Blue" /&gt;
          &lt;item android:id="@+id/menu_tan"
              android:title="Tan" /&gt;
  
  
  
  &lt;/menu&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>import android.annotation.TargetApi;
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.Context;
  import android.content.res.Configuration;
  import android.graphics.Color;
  import android.os.Build;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.text.Layout;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.FrameLayout;
  import android.widget.LinearLayout;
  import android.widget.SeekBar;
  import android.widget.Toast;
  
  import java.util.zip.Inflater;
  
  
  public class MainActivity extends ActionBarActivity {
  
      //public CategoryFragment categoryFragment;
      //public RecipeFragment recipeFragment;
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if(savedInstanceState == null)
          {
             CategoryFragment categoryFragment = new CategoryFragment();
             getSupportFragmentManager().beginTransaction()
                     .add(R.id.fragmentView, categoryFragment, "categoryFrag")
                     .commit();
          }
  
  
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
  
          if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
              Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
          } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
              Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
          }
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
      }
  
      @Override
      protected void onPostResume() {
          super.onPostResume();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate( R.menu.menu_main, menu);
          return true;
      }
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
  
          switch(item.getItemId())
          {
              case R.id.menu_green:
  
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  
  }
  </code></pre>
  
  <p>I have tried for hours to figure this out, but I just don't know where to put what.</p>
  
  <p>This is the code from the example that I found in the link posted above.</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.widget.LinearLayout;
  import android.widget.SeekBar;
  
  public class SeekColorActivity extends Activity {
  
  private int seekR, seekG, seekB;
  SeekBar redSeekBar, greenSeekBar, blueSeekBar;
  LinearLayout mScreen;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      mScreen = (LinearLayout) findViewById(R.id.myScreen);
      redSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_R);
      greenSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_G);
      blueSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_B);
      updateBackground();
  
      redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
      greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
      blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
  
  }
  
  private SeekBar.OnSeekBarChangeListener seekBarChangeListener
  = new SeekBar.OnSeekBarChangeListener()
  {
  
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
  // TODO Auto-generated method stub
   updateBackground();
  }
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
  }
  
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
  }
  };
  
  private void updateBackground()
  {
   seekR = redSeekBar.getProgress();
   seekG = greenSeekBar.getProgress();
   seekB = blueSeekBar.getProgress();
   mScreen.setBackgroundColor(
    0xff000000
    + seekR * 0x10000
    + seekG * 0x100
    + seekB
    );
  }
  }
  </code></pre>
  
  <p>categoryFragment.java</p>
  
  <pre><code>package com.example.mikesgamerig.finalproject;
  
  import android.app.AlertDialog;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  import java.util.ArrayList;
  import java.util.List;
  
  public class CategoryFragment extends Fragment {
  
      private ArrayList&lt;String&gt; categoryNameArrayList;
      private ArrayAdapter&lt;String&gt; adapter;
      private AlertDialog alertDialog;
      private AlertDialog alertDialogDelete;
      private EditText categoryEditText;
      private String getCategoryName;
      private List&lt;Category&gt; cats;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          //create view
          View rootView = inflater.inflate(R.layout.fragment_category, container, false);
  
          //initialize all variables and widgets
          inflater = getLayoutInflater(savedInstanceState);
          alertDialog = new AlertDialog.Builder(getActivity()).create();
          alertDialog.setView(inflater.inflate(R.layout.dialog_add_category, null));
          alertDialogDelete = new AlertDialog.Builder(getActivity()).create();
          alertDialogDelete.setView(inflater.inflate(R.layout.dialog_delete_category, null));
          Button buttonAddCategory = (Button) rootView.findViewById(R.id.addCategoryButton);
          ListView categoryListView = (ListView) rootView.findViewById(R.id.list);
  
          //Array list to store names of categories
          categoryNameArrayList = new ArrayList&lt;String&gt;();
  
          //List of Category Objects
          cats = Category.listAll(Category.class);
  
          getCategoryNames();
          //iterate through the CategoryList and attach to the ArrayList
  
  
          //create adapter and fill the listView with all the name of categories
          adapter = new ArrayAdapter&lt;String&gt;(getActivity(), R.layout.rowlayout, R.id.label, categoryNameArrayList);
          categoryListView.setAdapter(adapter);
  
          //set OnClick listener for the add category Button.
          // This calls another method that will open a custom dialog box
          buttonAddCategory.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  DisplayAddCategoryDialogBox();
              }
          });
  
          //set an onItemLongClick listener for the ListView.
          //First have to setLongClickable to true.
          //the OnItemLongClick listener will call a method to open a custom Dialog to delete a category.
          categoryListView.setLongClickable(true);
          categoryListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                  DeleteCategory(i);
                  return true;
              }
          });
  
          //opens up a new fragment with a list of recipes for the specific category.
          categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  
  
                  String name = categoryNameArrayList.get(i);
                  RecipeFragment fragment = new RecipeFragment();
                  fragment.SetTitleName(name);
                  getFragmentManager().beginTransaction()
                          .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
                          .replace(R.id.fragmentView, fragment)
                          .addToBackStack(null).commit();
  
  
              }
          });
          return rootView;
  
      }
  
  //This method will Display a custom add category Dialog Box.
      public void DisplayAddCategoryDialogBox(){
  
          //Show the Dialog box to enter a new category name.
          alertDialog.show();
          categoryEditText = (EditText) alertDialog.findViewById(R.id.categoryEditText);
          Button saveCategoryDialogBtn = (Button) alertDialog.findViewById(R.id.saveBtn);
          Button cancelDialogButton = (Button) alertDialog.findViewById(R.id.cancelBtn);
          saveCategoryDialogBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  getCategoryName = categoryEditText.getText().toString();
                  //Log.d("STRING VALUE:", getCategoryName);
                  Category test = new Category(getCategoryName);
                  test.save();
                  categoryNameArrayList.add(test.getName());
                  //Log.d("added Value: ", test.getName());
                  adapter.notifyDataSetChanged();
                  alertDialog.hide();
              }
          });
  
          cancelDialogButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  alertDialog.hide();
              }
          });
  
      }
  
  //this method will display a custom Delete Category Alert Dialog box.
      public void DeleteCategory(final int i)
      {
          alertDialogDelete.show();
          Button noBtn = (Button) alertDialogDelete.findViewById(R.id.noBtn);
          noBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  alertDialogDelete.hide();
              }
          });
          Button yesBtn = (Button) alertDialogDelete.findViewById(R.id.yesBtn);
          yesBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  String tempString = categoryNameArrayList.get(i);
                  //Log.d("VALUE OF STRING= ", tempString);
                  categoryNameArrayList.remove(i);
                  for (Category category : cats) {
                      String name = category.getName();
                      if (name.equals(tempString)) {
                          category.delete();
                      }
                  }
  
                  adapter.notifyDataSetChanged();
                  alertDialogDelete.hide();
              }
          });
  
  
      }
  
      //Filles the Array
      public void getCategoryNames()
      {
          for(Category category : cats)
          {
              String name = category.getName();
  
              categoryNameArrayList.add(name);
          }
  
      }
  
  
  }
  </code></pre>