Destroy layout after using setContentView - java

On one button I called a layout like so:
setContentView(R.layout.my_form);
On the other I wanna destroy that layout. How can I do it ?

U can use rootView.removeAllViews(); to remove all views from layout, for example
Your layout is
<LinearLayout
android:id="#+id/llRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
.......
</LinearLayout>
Then you can call llRootView.removeAllViews(); to remove all views from this linear layout
EDIT
To Temporary remove views from layout you can change its visibility using
llRootView.setVisibility(View.GONE);
To make it visible again use
llRootView.setVisibility(View.VISIBLE);

Related

Android RecyclerView item dynamic layout

Inside my recycler view item layout I want to add something like horizontal recyclerview layout, because I don't know how many ImageViews I need, that's why I can't create them as a static layout. The number of icons depends on server logic. How can achieve something like the picutre below ?
Thank you in advance!
You Need Add Horizontal Recyclerview Inside The Main Recyclerview.
Your First RecyclerView is Verticle list and inside every index create another RecyclerView It's a Horizontal. so you can display n number of Imageview.
You have to create Recyclerview inside the child layout of your main Recyclerview
Something like
Suppose this is the Main Recyclerview
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:listitem="#layout/item_demo" />
In your child layout, Let's say item_demo you have to add another Recyclerview for the imageView that you want to show in horizontal
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:listitem="#layout/item_child" />
item_child layout contains the Imageview that will display horizontally

What's the best practice to open a fragment on top of an activity

I have an activity with a bunch of elements already on it, and I want to open a fragment over the activity.
I want to use a fragment instead of an activity so I can pass a few objects that can't be serializable or parselable.
So far I manage to do this by doing something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"/>
<RelativeLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
.
.
.
In my activity I just change my fragment_container to VISIBLE, and I do a fragment transaction to add the fragment there, while also setting main_container that has all the views of my activity to GONE.
I know this is not a very nice solution, I've been thinking of moving all views inside of my main_container to a new fragment, so I can replace the container for one or another.
What would be the best practice to open a fragment on top of an activity?
A general rule of thumb that i use:
If you use fragments, then your activities should be extremely dumb.
Meaning that they should not contain any views themselves, and only contain logic to keep track of your fragments (and perhaps logic that only Activities can manage).
I'd take all the views and logic that's currently in your Activity, and move it into a separate Fragment.
Then you could simply replace the "standard" fragment with the new one. 2 fragments, clean and simple.

How to show a TextView inside a LinearLayout that is "visibility="gone""

i'm actually really new at coding in java and on AndroidStudio and i'm trying to update an application that already exist.
I have 3 LinearLayout that look like 3 square inside each other and inside the last one, i have a TewtView.
the first one is the main one and is always visible.
When i put my two last LinearLayout in visibility="GONE" my LinearLayout disapear, and i want that my textView will be always visible, even if the LinearLayout that contains it are invisible.
It is possible?
<LinearLayout
style="#style/ColPlayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
style="#style/SquareBogey"
android:visibility="gone"
android:layout_width="70dp"
android:layout_height="50dp"
android:orientation="horizontal"
android:id="#+id/outersquare">
<LinearLayout
style="#style/SquareBogey"
android:visibility="gone"
android:layout_width="60dp"
android:layout_height="40dp"
android:orientation="horizontal"
android:id="#+id/innersquare">
<TextView
android:id="#+id/PlayerCJ01"
style="#style/ColCJPlayer"
android:layout_height="wrap_content"
android:ems="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Just ask me if you need more informations.
Thanks
When setting the visibility of a parent layout to GONE or INVISIBLE, all its children will also become GONE or INVISIBLE.
If you want to show the TextView but not its parent LinearLayout, than you could either move the TextView out of the LinearLayout, or you could remove the styling of the LinearLayout so it will look like its gone, but obviously it's still there.
Setting the visibility of the LinearLayout to GONE and its child TextView's visibility to VISIBLE won't work and therefore is not an option.
Instead of setting the visibility as GONE for the LinearLayouts, maybe you can set their background/border as transparent by changing their style.
It depends on your use case, if it suits your need.

Fragment: overwrite activities margin

I have an Activity container for several different fragments. All fragments should have the same margin except one. This should has no margin. But how can I do that?
This is my activity_container.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" />
All fragments within this container have a margin of 15dp. But there is one fragment, that should have 0 dp. The fragment have to be in the container. So creating a container only for this fragment is no option. Do somebody has an idea to solve that problem?
The fragments cannot be displayed outside of their container, just as with any other view.
Your only option is to remove the margin from your relative layout and add it back as padding onto the individual fragment layouts

Android - Is there a way to put a ListView along with other controls within the same activity?

I want to create a ListView and a few other text boxes for filtering the list.
Is there a way to make it happen using one activity (i.e at the same page)?
One more question:
May I modify a ListView directly without creating a ListActivity? and how do I make the ListView in my ListActivity visible? (how do I link it with the xml?).
Yes. ListActivity seems to cause a lot of confusion for people, when all it is, is a regular activity with a ListView as the content, some helper methods, and that's it.
To add your own, create a new layout file and add all the widgets you need like you would any other layout file. Example:
<LinearLayout xmlns:android="http://schemas.android.com/res/apk/android"
android:weightSum="1.0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/whatever"
android:layout_weight="0" />
<ListView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
You certainly can create a layout which contains both a ListView and other controls! Make your Activity have a layout which contains both your ListView and your other controls.
<RelativeLayout>
<ListView android:id="#+id/listy"/>
<Button android:id="#+id/buttony"
android:layout_below="#id/listy"/>
</RelativeLayout>
In your Activity, you'll still need to hook up your data adapter to the ListView and so forth.
Yes, you can have ListView together with some other required views with in the same Activity. The way I would do this is to define an Activity and add ListView (with width and height set to fill_parent) and add a SlidingDrawer which contains all the options required to alter ListView. With this approach, your ListView will take up all the space on screen and offering user to interact freely. However, on the other hand, SlidingDrawer will give give extra space for all the list altering views/options.

Categories

Resources