In your XML File you must to write in order:
<FrameLayout
android:id="@+id/frameLayoutXML"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
<ImageView
android:id="@+id/imageViewXML"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatB_XML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
In this case, the imageView
will be back of the FloatingActionButton
.
If you want change something, and you will do it programmatically then:
public class SomeActivity extends AppCompatActivity {
//declare variables
ImageView imageView;
FrameLayout frameLayout;
FloatingActionButton floatingActionButtonNew;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Here is to call what already exists at XML file
frameLayout=findViewById(R.id.frameLayoutXML);
imageView = findViewById(R.id.imageViewXML);
//Here is to declare a new Element to add to the view, but is not at XML File
floatingActionButtonNew=New FloatingActionButton(this);
frameLayout.removeAllViews(); //here you remove all views.
frameLayout.addView(imageView);//And you add again the imageView first
floatingActionButtonNew.setLayoutParams(new
RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)); //Then you can give some format to your floatingButtonNew
//And finally add to the frameLayout the new View in this case the floatingButtonNew
frameLayout.addView(floatingActionButtonNew);
}
}
In this order of view declarations, you will have again the image back from the floating button. So the view that is declared first will be back from the view that is declared secondly, and the key to achieve the order, is to removeAllViews
of a FrameLayout
or LinearLayout
and add again this views when you are adding programmatically.