Android LinearLayout, set background to Transparent - java

I am inflating a bottom_sheet_layout. It contains a LinearLayout as the root element. It has one child CardView.
I've tried the following:
setting `android:background` property to
1. #00FFFFFF
2. #00000000
3. #android:color/transparent
setting the background color of LinearLayout programatically
`LinearLayout l = container.findViewById(R.id.root_element);
l.setBackgroundColor(Color.TRANSPARENT);`
This is the BottomSheet.Class that is linked to the bottom_sheet_layout
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
LinearLayout l = container.findViewById(R.id.root_element);
l.setBackgroundColor(Color.TRANSPARENT);
return v;
}
This is the full bottom_sheet_layout
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
android:id="#+id/root_element"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp">
<!-- A CardView that contains a TextView -->
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/poiName"
android:fontFamily="#font/comfortaa_bold"
android:text="#string/poiName"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"/>
<!--Location-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:fontFamily="#font/work_sans_medium"
android:text="#string/ui_location"
android:textFontWeight="800" />
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:fontFamily="#font/work_sans"
android:text="#string/ui_location_addr" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/share"
android:scaleType="center"
card_view:backgroundTint="#color/colorPrimary" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This is the Map Fragment. Here is where I inflate the bottom_sheet_layout
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
Following several guides, I still could not get the #00FFFFFF method to work. Programmatically setting the color to transparent, crashes the application.
It seems that in earlier questions on StackOverflow, setting the background to the hex code with alpha value used to work. Now I only get a dim gray background.
Screenshots of the application
Image Layout
App ScreenShot

If you are not using MapView, I supposed that you are inflating the Google Map + bottom_sheet_layout in the same fragment. If Yes, I think that your issue is not the LinearLayout transparency but you are inserting bottom_sheet_layout to the bottom of the fragment container, not over the map.
You can use this layout for your Map fragment
(androidx, but very similar if you are using android)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- use this fragment for your google map -->
<fragment
android:id="#+id/google_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- use this fragment for your bottom_sheet class -->
<fragment
android:id="#+id/bottom_sheet"
android:layout_margin="16dp" // with this you do not need the LinearLayout root_element
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Then, you can play with your CardView (or LinearLayout) visibility to show the card or not.
Other layouts are possible like using directly the card view instead of a Fragment.

I think I can help you with some sort of solutions, which you can also try and see if that works or not.
Make use of alpha property in your .java file
Range the value as per your ease
LinearLayout l = container.findViewById(R.id.root_element);
l.setAlpha(0.4);
You can make use of alpha in your XML file too:
<LinearLayout
android:id="#+id/l1"
android:alpha="0.5"
android:layout_width="190dp"
android:layout_height="match_parent">
0.0 is for fully transparent and 1.0 is for fully opaque.
Let me know if any of the solutions work for you. However, do more research here, you will definitely get a solution. Happy coding.

Try background null.
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#null">
......
</LinearLayout>

Are you sure that the child layout elements doesn't have backgrounds?

Related

Centre text inside a Cardview - Android Studio

I'm currently using a Card View inside a recycler and the text is left-alligned despite my efforts.
Here is the Book Item Layout.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/bookTitleTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Here is the recycler itself.
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bookRecycler" />
This is the current output.
enter image description here
As you can see the text is not centered.
I tried using layout gravity to center the text which didn't work.
I also tried using a constraint layout on the text view and using constraints to center the text without any luck.
android:layout_gravity = "center" has already been attempted.
Any advice on what I'm overlooking would be great.
Thank you.
add android:gravity="center" for LinearLayout and you are good to go
I see your LinearLayout have only one child and no special attributes, so you can even remove whole LinearLayout - place TextView straight inside CardView, which extends FrameLayout. For centering childs inside FrameLayout you may also use android:gravity="center" for parent and/or android:layout_gravity="center" for child
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
app:cardCornerRadius="4dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#color/white">
<TextView
android:layout_gravity="center"
... rest of code
android:gravity="center" is the correct code, instead of android:layout_gravity="center_horizontal"

Android View below another in FrameLayout not work

I have such code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:padding="15dp"
android:background="#drawable/button"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#FFF" />
<pl.jawegiel.endlessblow.other.GameSurface
android:id="#+id/gameSurface"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
(...)
</RelativeLayout>
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/left_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#22FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/right_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#99FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.drawerlayout.widget.DrawerLayout>
The problem is that my Button is not below GameSurface.
I put this Button as first one counting on that it will be below GameSurface but it does not work.
How to achieve that?
You need to change FrameLayout to another kind of ViewGroup. FrameLayout is a a type of ViewGroup where one View is on top of another one. Like Layers in a cake.
Putting one below another is not possible in FrameLayout
The easiest solution might be to change FrameLayout with LinearLayout and of course, remember to add android:orientation attribute to it.
NOTE: I suggest you to learn ConstraintLayout, which can replace nearly every type of nested ViewGroups including FrameLayout and RelativeLayout :-)
This you can't achieve with FrameLayout, this is because FrameLayout is usually used to show only one child. Even tho it can show more child views they are just placed on top of each other. This is explained in the answer above. Each ViewGroup has its own pros and cons and the best Layout for this is LinearLayout. LinearLayout can place your child's one below another or one next to another, based on android:orientation attribute you can use. Values are vertical and horizontal. You understand from that which one should be used here.
Therefore, what you need to do is simply change your FrameLayout to LinearLayout. So this is how it looks in my XML:
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
After that, you just place your child views in order you want to show them. So, in my case I have an ImageView and a Button, placed in that order, like this:
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/gameSurface"
android:src="#drawable/ic_user"
android:layout_width="200dp"
android:layout_gravity="center_horizontal"
android:layout_height="200dp" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:layout_marginTop="20dp"
android:padding="15dp"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#000" />
</LinearLayout>
Then I get this:

Android layout collapse

I use a XWalkView to load webpage and a IjkVideoView to play live video in my app. I want the IjkVideoView playing the video in front the XwalkView, so I put these two view in a relativelayout and IijVideoView behind the XWalkView.
In the android studio's Component Tree of Design window the layout seems ok as below image,
While when I run the app in my device, the layout is in mess as below image. You can see the background of IjkVieoView is in front of XWalkView, but the video playing in the IjkVideoView is behind the XWalkView, as the upper part of the video has been covered by the XWalkView.
layout xml,
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
<!-- This could be your fragment container, or something -->
<org.xwalk.core.XWalkView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xWalkView"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:background="#color/blue"
android:isScrollContainer="true">
</org.xwalk.core.XWalkView>
<com.xxxxxx.app.widget.media.IjkVideoView
android:id="#+id/video_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:background="#color/colorAccent" />
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="#xml/web_tabs" />
</RelativeLayout>
</LinearLayout>
activity java code,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("INFO", "entered");
setContentView(R.layout.activity_web);
mXWalkView = (XWalkView) findViewById(R.id.xWalkView);
XWalkSettings webSettings = mXWalkView.getSettings();
webSettings.setJavaScriptEnabled(true);
mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
videoView = (IjkVideoView) findViewById(R.id.video_view);
videoView.setAspectRatio(IRenderView.AR_ASPECT_FILL_PARENT);
videoView.setVideoURI(Uri.parse("http://live.xxxxxx.com/live/40xx27.flv"));
videoView.start();
final Bundle extras = getIntent().getExtras();
mXWalkView.load("http://xxxxxxxxxxxxxx", null);
}
Your layout seems to be correct. It might be broken from the libs. It already has 1k+ issues.
Maybe you can use a custom dialog that has the video in it and show that dialog. This way the video will be in front of the images. Then you can try to remove the shadow or bgColor = transparent, so that the user might not realise that is a dialog. This hack may work.
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
<!-- This could be your fragment container, or something -->
<org.xwalk.core.XWalkView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xWalkView"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:background="#color/blue"
android:elevation="5dp"
android:isScrollContainer="true">
</org.xwalk.core.XWalkView>
<com.xxxxxx.app.widget.media.IjkVideoView
android:id="#+id/video_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:elevation="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:background="#color/colorAccent" />
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:elevation="5dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="#xml/web_tabs" />
</RelativeLayout>
</LinearLayout>
Try this layout i modify it with elevation may it will help you.I have same problem by using elevation it solve.
I think it is due to relative layout. Why don't you try this code by using the Frame Layout. The most convenient and maintainable way to design application user interfaces is by creating XML layout resources. This method greatly simplifies the User Interface design process, moving much of the static creation and layout of user interface controls and definition of control attributes, to the XML, instead of littering the code.
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<org.xwalk.core.XWalkView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xWalkView"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:background="#color/blue"
android:isScrollContainer="true">
</org.xwalk.core.XWalkView>
<com.xxxxxx.app.widget.media.IjkVideoView
android:id="#+id/video_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:layout_gravity="Bottom"
android:gravity="center"
android:background="#color/colorAccent" />
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="#xml/web_tabs" />
</FrameLayout>
</LinearLayout>

Android - what kind of view/xml I've to study

Problem I want view-layout like the following photo, on top I want image view, in the middle a text view and bottom list of items. All this I want to be scrollable (black arrow in the picture). in this way, the user for first see image-text-and part of list items, if scroll down, see the list of item.
For the bottom I thought to use listView, but in this way I think I'll have 2 scrollable view (???) and then I want to make the list in 2 columns, and I don't know if it is possible with ListView
Question
I didn't write code, because I want to know what kind of view or somethings else I've to study.
I sketchy something like this, but the result is really horrible
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="#drawable/nope"
android:id="#+id/imageView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<View
android:id="#+id/centerShim5"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView2"
android:textColor="#color/ame_default_cluster_circle_color_medium"
android:textSize="26dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</ScrollView>
I can't use listview in Scrollview, and inspired by the matryoshka I found the solution.
I define xml1 file in which I have only ScrollView and LinearLayout inside
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linear">
</LinearLayout>
</ScrollView>
after, for each kind of items that I want to add inside, I define another xml2 and after I add element:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ame_default_cluster_text_color">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:textSize="32sp"
android:textColor="#color/ame_default_cluster_circle_shadow_color"
android:id="#+id/text1"
android:textAlignment="center" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/nope"
android:id="#+id/photo" />
<TextView
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="#+id/descrtiption"
android:textColor="#color/ame_default_cluster_circle_shadow_color"
android:layout_margin="10sp" />
</LinearLayout>
with code:
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view=layoutInflater.inflate(R.layout.xml1,container,false);
LinearLayout linearLayout=(LinearLayout)item_view.findViewById(R.id.linear);
LayoutInflater inflater= LayoutInflater.from(ctx);
View view1 = inflater.inflate(R.layout.xml2, linearLayout, false);
TextView text1=(TextView)view1.findViewById(R.id.text1);
text1.setText();
ImageView imageview=(ImageView)view1.findViewById(R.id.photo);
and obviously I can define XML for element to add like listview and add it with normal foreach ,Hope this will help someone.

Adding a CardView to a FrameLayout ignores LayoutParams defined in the CardView's XML file

In my MainActivity I'm using a FloatingActionButton to add CardViews to an existing container (a FrameLayout) programatically.
But when I add the CardView all LayoutParams (e.g. width, height, margin, etc) are ignored. Is there a way to use the params specified in the XML File?
Here is my code:
card_view.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
card_view:cardCornerRadius="2dp"
card_view:cardBackgroundColor="#color/cardViewBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="example Text" />
</android.support.v7.widget.CardView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:id="#+id/textViewDate"
android:text="#string/default_main_activity_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textAlignment="center"
android:padding="16dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_fab_add"
android:layout_gravity="end"
android:onClick="addCard"
android:visibility="gone"/>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.life_hacks.liftnotes.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
and finally the code I use to add the CardView:
public void addCard(View v){
View cardView = View.inflate(getApplicationContext(), R.layout.card_view, null);
FrameLayout container = (FrameLayout) findViewById(R.id.container_body);
if(container != null){
container.addView(card_view);
}
}
For the layout_* attributes of an XML-defined View to be applied correctly, the ViewGroup that will hold it must be supplied to the LayoutInflater. In this case, it means that you need to pass your container as the last argument in the View.inflate() call. Doing this will also cause the inflated View to be automatically added to the ViewGroup, so you must not call addView() with it, or you'll get an IllegalStateException.
Also, since you'll be adding multiple Views to your container, it would seem that a LinearLayout would be more appropriate than a FrameLayout. I would also mention that if you plan on adding a lot of additional Views, a ListView or a RecyclerView might end up being a better option.

Categories