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"
Related
I'd like to change and align the text, and possibly add features on appbar in the screen above.
However, it'd be best if I could find and modify the text and align it IF I don't intend to add features.
But then I wasn't able to find where in android studio I could change the text.
I thought I could find one in the main activity xml file but I couldn't. I looked up at themes.xml and AndroidManifest.xml but couldn't find a clue.
Here is first 13 lines of my main activity xml file just in case.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_height="480dp"
app:layout_constraintTop_toTopOf="parent"/>
So, to cut it short, where in android studio can I change the text in toolbar? or appbar? above in the screen without having to create toolbar.xml? (+ align the text in the center and change text color)
Apparantly you don't have toolbar implemented in your xml layout
In Your style file make sure you set it to noActionBar
<style name="Theme.ChatApp" parent="Theme.AppCompat.Light.NoActionBar">
Then you have to add your own toolbar
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toolbar"
android:background="#color/white"
app:navigationIcon="#drawable/ic_baseline_dashboard_24"
app:title="here you can put your title"/>
In your activity reference your toolbar in your onCreate
setSupportActionBar(findViewById(R.id.toolbar)
PS : If you are using MaterialToolbar please consider add this material library
implementation 'com.google.android.material:material:1.1.0'
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
I'm currently making an app for Android devices and I would like to know how can I change the way I display the fragments in differents display sizes (ex. Tablet).
At this point I have an activity for all my fragments and this causes me to replace the fragments when I change page but for tablets I would like to display 2 fragments inside that activity.
I've created a layout-large version for my main activity:
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".PaginaInicial"
tools:showIn="#layout/app_bar_pagina_inicial">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="pt.ipp.estg.schoolhelperapp.apontamento.ListaApontFragment"
android:id="#+id/a_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
class="pt.ipp.estg.schoolhelperapp.apontamento.VerApontFragment"
android:id="#+id/b_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
The fragment container is inside layout-small version of the main activity.
One of the fragments displays a list of items (#+id/a_fragment) and the other one displays the information of one specific item that I previously selected (#+id/b_fragment).
For further details refer to this document: https://developer.android.com/training/multiscreen/screensizes
Create a default alias resource value file in res/values/refs.xml and place the following code inside it. This value will refer to your layout file that has only one fragment.
<resources>
<item name="activity_masterdetail" type="layout">#layout/layout_with_single_fragment</item>
</resources>
Now need to create an alternative resource so that the activity_masterdetail alias will point to layout_with_two_fragment.xml on larger devices.
Create a new file with same name refs.xml but with resource qualifiers for Smallest Screen Width under Available qualifiers. Use smallest dimension of 600dp or more (sw600dp qualifier)
<resources>
<item name="activity_masterdetail" type="layout">#layout/layout_with_two_fragment</item>
</resources>
Your goal here is to have logic that works like this:
For devices that are under a specified size i.e mobile phones, use layout_with_single_fragment.xml.
For devices that are over a specified size, use layout_with_two_fragment.xml.
Now in your man activity which shows a list, use setContentView(R.layout.activity_masterDetail) i.e the name of referenced layout
If this code will run on mobile phones, normal refs.xml file will be referenced by activity_masterDetailbut if this code runs on a tablet, resource qualified refs.xml file will be used.
In your activity logic, where you want to show master and detail layout on the same screen on tables but open another activity on phones, use the following logic inside onClick() of your list items
if (findViewById(R.id.a_fragment) == null) {
Intent intent = A_Activity.newIntent(this, listItem.getId());
startActivity(intent);
} else {
Fragment newDetail = B_Fragment.newInstance(list_item.getId());
getSupportFragmentManager().beginTransaction()
.replace(R.id.b_fragment, newDetail)
.commit();
}
You should take a look at this before link it should help you understand for sure more about supporting different screen sizes.
And here is an example that you can try.
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.
My plan is creating an Activity includes Tabs+Swipe for all Android version. If we set it from a default Android project, it has just support for at least API 11.
In Sherlock we have two project named : Tab Navigation, Tab Navigation(collapsed) includes Tabs but not Swipe.
They have Issue #240 in their samples that has a bug (swipe left/right when the tabs are in collapsed mode (landscape) and the selected item does not update).
Do you know any sample code that solve this problem?
You made this now with the default Android Support Library (or the ABS), with a ViewPager and a PagerTabStrip:
<?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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="#+id/tabStrip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
Then create an Adapter to the ViewPager that extends of FragmentStatePagerAdapter (for example) and override the method public CharSequence getPageTitle(int position) to provide a title for each tab.
Hope it helps.
I also happened to have same situation. Follow below links and you will get all what you need.
See this tutorial
http://droidista.blogspot.com/2012/08/making-actionbarsherlock-and.html
And get demo code from here
https://github.com/zmdominguez/vpi-abs-demo
I found these links which might help you out.
A page flipper control - like the android home screens
https://github.com/JakeWharton/Android-ViewPagerIndicator