going to another xml from the main xml - java

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.

Related

Android fragment preloading data

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

How can I change the way I display the fragments in differents display sizes?

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.

How to use SwipeRefresh's refresh icon Without Listview?

My app consists of different activities where I use a SwipeRefreshLayout to refresh data in listViews by swiping. When this happen a refresh icon is displayed at the top of the screen.
However, for 2 other specific activities that contain no list view, I only want to display the refresh icon while the data is refreshing by using pullToRefresh.setRefreshing(true); & pullToRefresh.setRefreshing(false);. I don't need to use it for swiping.
This is how it looks in my list view layout xml file:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView"
android:dividerHeight="8dp"
android:divider="#000000"
android:layout_below="#+id/feedLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
Use SwipeRefreshLayout as a regular layout, and call pullToRefresh.setRefreshing(true) when you want to show the loading wheel, and pullToRefresh.setRefreshing(false) to hide it.

Android: Adding Image to Master Detail Flow, scrolling text

I'm working on a information orientated app using the master/detail flow and so far, so good. I would like to add images to the TextView, but it's formatted differently then what I've experienced in the past and I'm lost. from my understanding of what I've read while searching is that the scrolling text is "newer" when generating the Master/detail activity, therefore I haven't found any information on this specific issue. I would also like to pass the images in using the content activity, so it would be-
addItem(new Item(ID,Name,Detail,Image1,Image2));
what the detail XML file looks like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bobblehead_detail"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"
tools:context="com.example.johnson.fallout4bobbleheadlocations.BobbleheadDetailFragment" />
I tried adding ImageView's under it, but I received errors.
tl;dr I would like to add 2 images under the scrolling TextView.
I am not sure if I clearly understand what you mean but looking at your xml it seems to me that you need to add a layout (either relative or linear) to your xml file and then add a textview and two imageviews (or whatever you want) into that layout.
Something like this:
<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"
android:padding="16dp" tools:context="com.example.somefragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id2"
android:text="Here is your text"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id"
android:src="#drawable/image1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id3"
android:src="#drawable/image2"/>
</RelativeLayout>

Fragments are not running perfectly in real device

I have used fragments to define a simple splitview, in the right site i used a layout containing videoview the xml code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/tvT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
and in the left site in another fragment layout I have defined some buttons to play different videos, The program is running well in emulators, but when I run it on device, the videoview overlapped the whole area and creates a horrible look, what should I do?
You need to post more of your code if you can; what you have posted isn't really enough for people to help you. [Edit: Darn, I thought this was trivial enough to be automatically moved to a comment on the original question :-\ ]
May you could try with "fill_parent" because the video could be to big and uses the hole display:
<VideoView
android:id="#+id/tvT"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
If you can it would be good if you post some code how you setup your fragments.

Categories

Resources