Nikos Nikos - 5 months ago 40
Java Question

Android : Get fragment's background color

I am trying to get the color set on android:background attribute from a fragment's XML file through an activity's

onCreate
method.
I used both the
getSolidColor()
and the
getgetDrawingCacheBackgroundColor()
methods but both of them return the value of the color corresponding to the Layout containing the fragment..

Here is the Activities code part :

private SeekBar seekBar;
private View fragmentOne;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

fragmentOne = findViewById(R.id.fragment1);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
fragmentOne.getDrawingCacheBackgroundColor()


the fragment's xml file,

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:padding="5dp"/>


And the activity_main xml file containing the above, and also 4 other fragments and a seekbar:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">



<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
android:padding="5dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.maryland.modernartui_nikos.MainActivity">





<fragment
android:id="@+id/fragment1"
android:name="com.maryland.modernartui_nikos.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@layout/fragment_fragment_one" />

<fragment

android:id="@+id/fragment2"
android:name="com.maryland.modernartui_nikos.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="117dp"
tools:layout="@layout/fragment_fragment_two" />


</LinearLayout>


<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
android:padding="2dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:showDividers="beginning|middle|end"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.maryland.modernartui_nikos.MainActivity">


<fragment
android:id="@+id/fragment3"
android:name="com.maryland.modernartui_nikos.FragmentThree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="@layout/fragment_fragment_three" />

<fragment

android:id="@+id/fragment4"
android:name="com.maryland.modernartui_nikos.FragmentFour"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
tools:layout="@layout/fragment_fragment_four" />


<fragment

android:id="@+id/fragment5"
android:name="com.maryland.modernartui_nikos.FragmentFive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
tools:layout="@layout/fragment_fragment_five" />


</LinearLayout>



</LinearLayout>

<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />


The method returns the value from the
RelativeLayout
of
activity_main.xml
and not from
fragment_one.xml
.Why?

Answer

Found a solution to this problem :

        ColorDrawable colorDrawableOne = (ColorDrawable) fragmentOne.getBackground();
    ColorDrawable colorDrawableTwo = (ColorDrawable) fragmentTwo.getBackground();
    ColorDrawable colorDrawableFour = (ColorDrawable) fragmentFour.getBackground();
    ColorDrawable colorDrawableFive = (ColorDrawable) fragmentFive.getBackground();

    final int colorOne = colorDrawableOne.getColor();
    final int colorTwo = colorDrawableTwo.getColor();
    final int colorFour = colorDrawableFour.getColor();
    final int colorFive=   colorDrawableFive.getColor();

The fragment's getBackground() method must be called and stored to a ColorDrawable variable with type casting. Then you can call the getColor() methdod of this object to get the desired color.

Comments