Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do 'android:foreground' and 'android:foregroundGravity' in FrameLayout affect its appearance?

FrameLayout has the attributes android:foreground, android:foregroundGravity and android:measureAllChildren. I have tried these attributes, but couldn't make out how they affect the layout's appearance or how they work.

<FrameLayout 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:foreground="@android:color/holo_blue_dark"
 android:foregroundGravity="center"
 android:measureAllChildren="true"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.framelayoutdemo.MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/btn_dialog" />

</FrameLayout>

I have googled, but couldn't find a help with these attributes. Please provide me a link which I can refer or help me with an example. Thanks

like image 303
SimplyProgrammer Avatar asked Jan 26 '16 20:01

SimplyProgrammer


1 Answers

If measureallchildren is set to "true" then it will show actual width and height of the frame layout even if the views visibility is in gone state. For example

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/myframe"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:measureAllChildren="true"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:src="@drawable/ic_launcher"/>

</FrameLayout>

Below is the code of MainActivity.java . if we use Toast to display height and width on screen.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myframe_activity);
        FrameLayout frame=(FrameLayout)findViewById(R.id.frame);
        frame.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int width = frame.getMeasuredWidth();
        int height = frame.getMeasuredHeight();
        Toast.makeText(getApplicationContext(),"width="+width+"  height="+height,Toast.LENGTH_SHORT).show();

    }

}

if we run the App in the Emulator the output in the form of Toast message will be like this: width=72 height=72

Now if we change the value of measureAllChildren to false then Toast message output frame layout width and height will be: width=0 height=0

like image 99
Hidayat Ullah Avatar answered Sep 18 '22 13:09

Hidayat Ullah