Hello I'm trying to make an application that has multiple bottomnaviagtion tabs.
Currently it works because fragment data (for example, images) are static. However, if I want to make them dynamic, I'll have to create a preloader.
So I want to display a loading layout (for example R.layout.loading) UNTIL (async) function has completed and obtained data from server. Then I want to replace the layout in the fragment with a new layout (R.layout.datafragment)
Fragment onCreateView:
return inflater.inflate(R.layout.loading, container, false);
In summary it should work exactly like youtube bottom tabs.
You should create in your activity layout a placeholder for those dynamic fragments and a loader which is by default gone, but when you start loading data from the server you should make it visible (loader). Then, when you load necessary data, just start fragment transition and place whatever fragment you have loaded.
Your parent activity's layout should be like this:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Navigation View and Fragment Container-->
</RelativeLayout>
<RelativeLayout
android:id="#+id/progress_layout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
Then you simply show/hide progressLayout based on your requirement. Thanks
Related
I have a button in my MainActivity that opens FragmentA. FragmentA covers the whole screen, but I still see the button from MainActivity, and I can still click on it.
I've tried using clickable in my fragment layout but it's not working
MainActivty
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val fragmentManager = this#MainActivity.supportFragmentManager
fragmentManager.beginTransaction()
.add(R.id.fragment_container, AFragment())
.addToBackStack(null)
.commit()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/fragment_container">
<Button
android:text="Button Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
android:background="#color/colorPrimary"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A"/>
</LinearLayout>
This is happening because you placed your Button inside of the ConstraintLayout you're using as the container of your Fragment.
When you add a fragment into a container like what you're doing, it's simply adding it in the same manner as if it was a View.
Therefore, if you add a Fragment into a ConstraintLayout that already possesses a Button as a child, the Fragment will be shown alongside the Button due to ConstraintLayout allowing for overlapping Views.
This is also why, if your container was a LinearLayout, then adding a Fragment will place the fragment underneath your Button instead.
So, with that in mind, the solution would be to handle it as if they were Views.
If you added a View into a layout and you have another View overlapping, how would you get rid of it?
The most common solution would be to set the Button's visibility to INVISIBLE or GONE when the Fragment is added.
Another solution might be to raise the elevation of the Fragment, so it's now higher than your Button.
Of course, you may also remove the button from the Container and place it inside a Fragment too.
This way, you can use the replace() method in a FragmentManager to replace the Fragment containing your Button with the Fragment you want to show.
I'm building an app which has 2 fragment containers and takes screenshot feature.
the problem is whenever one of the 2 fragment containers update with another fragment(replaced) and I'm taking a screenshot it shows the previous fragments and not the updated ones.
I'm using getView().getRootView() within another fragment in the activity to take the screenshot.
any advice on why it happens?
Please be more elaborate about the problem or try to add the code.
If you are not using the FrameLayout, please use this.
Code for Adding FrameLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/your_placeholder"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></FrameLayout>
</android.support.constraint.ConstraintLayout>
To add or replace new Fragments use the FragmentTransaction.
// Code inside the onCreate method
getSupportFragmentManager().beginTransaction()
.replace(R.id.your_placeholder, new TheOtherFragment())
.commit();
To take screen Shot :
View view = findViewById(R.id.your_placeholder);
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
To take screen Shot :
View view = findViewById(R.id.id_of_constraintLayout);
// Here you assign the view Variable to the id of the View hosting all your Views.
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
In AndroidStudio, I selected "New->Fragment->Fragment (List)" to create a list of items. It works fine and displays all the items; however, I would like to have a title at the top.
If I add another TextView to the "fragment_item_list.xml" file, then the title appears in all the items in the list. From other questions I figured that if I create another LinearLayout xml layout file (something like "list_header.xml") and put a TextView in it, I can use a layout inflater to inflate this layout in the fragment activity, e.g. in onCreateView,
View header = inflater.inflate(R.layout.list_header,container,false);
but then I don't know what to do with header to make it display somewhere in the fragment. I can add it to the original activity by calling container.addView(header); but how can I add it to the fragment?
I don't want to change the ActionBar title, firstly just for aesthetics, and secondly because the fragment covers it up. Ideally there would be a header like in an alert dialog.
Let me try to understand you. You want to add a title to your fragment list (like an header), not in the ActionBar. Right?
Looking the fragment_item_list.xml we have this: (I follow your steps: "New->Fragment->Fragment (List)")
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="#layout/fragment_item" />
The RecyclerView is a container used for populate a list, in other words, the RecyclerView (or ListView) contain all the list only. So, you have to add another container (like LinearLayout, RelativeLayout, etc.) as root of the layout to add more views to the layout. Example using the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World! I'm a header!"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.veroapps.myapplication.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="#layout/fragment_item" />
</LinearLayout>
With this you will have a list with a title at the top. Now, you can manage the title TextView in the ItemFragment(name of the fragment generated by Android Studio). And of course there are more ways to do that, but this is an easy one.
Hope this help you.
In your Fragment java class, there should be a method called onCreateView this is where you set the layout for your Fragment (and where the list is added). It will look something like this:
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
In this Fragment the layout is inflated by this code inflater.inflate(R.layout.example_fragment, container, false); and this means there's is an xml file defining the layout called example_fragment.xml.
Your code will be the same, when you find this XML file. You can add other views inside it.
You will want to add another TextView above the RecyclerView or ListView already in that file.
Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
...
android:text="YOUR TITLE" />
<RecyclerView
... />
</LinearLayout>
I m totally new to program and I m trying to make a simple app.
I need to jump from one xml to another xml by clicking a button, and I m stopped here.
I only know how to use graphical layout and how should I do it???
I want to jump from Activity_Main.xml to 2bb.xml, and 2bb.xml to 2result.xml and at last from 2result.xml back to Activity_Main.xml.
my A_M.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1"
tools:context="com.example.soo.MainActivity"
tools:ignore="MergeRootFrame,NewApi,UnusedAttribute" >
<Button
android:id="#+id/button1"
android:layout_width="150dp"
android:layout_height="215dp"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:text="#string/B2"
android:onClick="2bb" />
There are a few ways to achieve this, depends on what your looking for.
You can call a new activity from your MainActivity and inflate 2bb.xml when the new activity is created, or you can use different fragments, and just transition between fragments in the same activity. I suggest going over some tutorials regarding Activities and Fragments in Android.
Try this
To go to other xml, you have to create one activity for one xml and each button has to put action startactivity.
I'm trying to define a fragment's layout in XML in the same way that I defined the layout of my view.
Is this possible? I tried several things, but none of them seem to work.
My activity layout looks as follows (main.xml):
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="full.lommeregner.Lommeregnerrv2Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lommeregnerv2">
</fragment>
</ListView>
Now, since I'm not a big fan of generating layouts through raw Java code, I tried defining my fragment as follows (fragment_lommeregner.xml):
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="full.lommeregner.Lommeregnerrv2Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lommeregnerv2">
<!-- my content was located here (some textviews, buttons and so on). -->
</fragment>
Am I doing something wrong? How can I define the layout of a fragment through XML?
A Fragment works much like an activity, in that you need a Java class file to go with it. you cannot create a Fragment just by creating a fragment layout - you need a class for your fragment:
Create a layout XML and an Activity subclass for your activity
Create a layout XML and a Fragment subclass for your fragment
Tie the two together in your Activity layout XML (or using FragmentTransaction if you want to do it in Java code)
If you haven't already done so, read, re-read and digest everything on this page:
https://developer.android.com/guide/components/fragments.html
There's a lot there, but Fragments are an essential part of Android apps now so it's required reading. The good news is that the basics of fragments is pretty simple.
Simply add into your <fragment> tag as property:
tools:layout="#layout/your_layout_xml"
And in your main.xml (or <fragment> tag container) into your parent container (in this case ListView):
xmlns:tools="http://schemas.android.com/tools"