We are developing an app that in one point, we need a screen like Honeycomb Gmail application :
http://www.cnx-software.com/wp-content/uploads/2011/02/android_3.0_honeycomb_gmail_app_fragments_700px.png
We are trying to use fragments and includes a listview to show our items.
We did exactly the same thing on this link : http://www.vogella.com/articles/Android/article.html#fragments_tutorial
But in any way we did not able to view different layouts,
what i mean is that, the only thing that we can show on the right fragment a single textview.
but we need a listview there, that we can view a thumbnail,some explanations and this needs to be clickable.
anyone to help?
Should work. I think you want to instantiate a ListFragment that, upon the user selecting a row, instantiates another ListFragment.
Maybe read this article aswell.
Below is an example for a layout xml file that has two fragments next to each other with an even width.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<fragment
class="package.of.fragmentA"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/fragmentA"
/>
<fragment
class="package.of.fragmentB"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/fragmentB"
/>
</LinearLayout>
In the case of the GMail application the class fragmentA would be a ListFragment and fragmentB would be a normal fragment class with a custom layout file.
Related
I'm creating an Android app.
When I click on a menu item, I need to open an activity/view/fragment (I don't know what is the best). This activity/fragment/view need to be placed as an "overlay" of my app (it will have a transparent background and we could see the "regular" activity behind).
I'm using a navigation drawer and a couple of fragment inside my app.
First I tried this :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".AlertActivity"
android:id="#+id/alertLayoutRoot"
android:background="#android:color/transparent"
>
<TextView
android:layout_width="match_parent"
android:id="#+id/editText1"
android:text="#string/mes_favoris"
android:layout_height="match_parent"
>
</TextView>
</FrameLayout>
This is indeed showing my fragment, but this fragment isn't on top of all the others.
What could be the best way to do this (in working with Kotlin)?
EDIT : forgot to say, in this view, there will be a small slider.
Basicaly I want this : Overlay
You can use dialog fragment as it supports custom layouts like fragments and shows on top of another activity or fragment.
Here is a useful tutorial on dialog fragment.
I am a beginner at Android Studio.
What I am trying here is to make two fragments and the second fragment has a button.
I was wondering why this XML code below is not working..
I got this notification..
Rendering Problems: A <fragment> tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout.
My questions..
Is it okay to add Button element inside Fragment?
Can I add Frame inside Fragment as well?
How can I solve this problem?
Thank you for your help!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--first fragment(left screen) -->
<fragment android:name="com.example.android.fragments.ArticleListFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >
</fragment>
<!--second fragment(right screen) -->
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="#+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent">
<Button
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/article_new_fragment"
android:text="OK!" />
</fragment>
</LinearLayout>
You can add any view inside a fragment, just like you would do in an activity.
For what I can tell, that button is inside the activity layout.
So:
Go To com.example.android.fragments.ArticleFragment
Go to its method onCreateView (ctrl+F to look for it)
It should have a return statement, something like
inflater.inflate(R.layout.fragment_layout, container, false);
At this point, you should understand that this method returns the Fragment's layout.
Go to the layout specified in that line (in this case, it would be
R.layout.fragment_layout).
Put in that layout the button, run the app and you should see it.
Answering your questions:
Is it okay to add Button element inside Fragment?
Yes but in its own layout file. You define where is the layout file in you java fragment class.
Can I add Frame inside Fragment as well?
If you mean frameLayout, yes but also in the layout file of the fragment.
I want to display two different activities in a single screen how can i do that in android?Please if anybody has idea share it.And I don't wanna use fragments.
I want to display a screen which contains some fields and below(at the bottom of the screen) I want another screen with some buttons.
Is this possible in android?
If so, How can i do this ?
You can't have two activities in one screen. You can have only one. So, ultimate solution is Fragments.
An activity is not directly a visual component, so I'm thinking that what you're really asking is how to have a single activity display different views.
There's nothing that says you can't rerun setContentView() with a different layout/view ID. But there's another non-fragments way of doing what your probably want.
You can define more than one full-size (match_parent) view in a layout. What you want to do is set the visibility for one of them to "visible" with android:visibility="visible" and all the others to "gone" with android:visibility="gone".
Then when you want to switch the displayed view, you'll run setVisibility(View.GONE) on the outgoing view and setVisibility(View.VISIBLE) on the incoming. It's important to use GONE and not INVISIBLE or the layouts won't render correctly.
Sample layout file:
<FrameLayout 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"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<SurfaceView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
Sample Code to switch view:
video.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
web.setVisibility(View.GONE);
That said, you probably want to learn how to use fragments since you can handle switching the view along with other state in a single unit of work (a transaction). But the above approach above does work for simple view changes.
i am targeting an app on android 2.1. i am having a layout which contains three sections. the header, mainbody and the footer. the footer remains same all through the application lifecycle. the footer has four options for the user to select(like four tabs). when the user selects each option in the footer the content has to change in the mainbody. and when the user interacts with the UI in the mainbody, there is a need to change the content of just the mainbody(like activity replacing an activity). and the user selection in the footer has to remain highlighted untill user selects another option in the footer.i alomost have a need like, launching activities within the same tab, but the tabs are placed below. a lot of people have suggested using activitygroup but as it is deprecated how do i go about doing this?. if anybody needs more clarity about question i am ready to provide
If you want support from lower versions like 2.1 and higher I can propose my way. I always use separate XML layout for tray (footer in your case), for example (res/layout/tray.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#EEEEEE"
android:gravity="bottom|fill_horizontal"
android:orientation="horizontal"
android:padding="5dp" >
<ImageView
android:id="#+id/addBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/add" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/catalogBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/catalog" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/searchBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/search" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/settingsBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/settings" android:layout_weight="0.2"/>
<ImageView
android:id="#+id/infoBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/info" android:layout_weight="0.2"/>
</LinearLayout>
And includes it in every Activity I need:
<include layout="#layout/tray" android:id="#+id/tray" />
After that I can in java code hide/show some buttons in tray by ID, or select some of them with another color ...
To display the footer menu across all activities, you may create a custom layout - the layout of the footer - and include it in every activity, or to be more specific, include it in the layout of every activity with <include />
Also, all your activities should have a parent activity, let it be BaseActivity, where you will provide appropriate actions for you footer menu.
Then you will need just to inherit the BaseActivity and include the footer menu layout, into your current layout, to have the menu available for any Activity you would like.
Another possible approach is using fragments instead of a full activity per content page. Fragments are a lot like activities, except that they need to be embedded in an activity to be displayed and they give you the freedom to change the content of one part of an activity (that is, swapping one fragment for another), meaning you can have another part of the activity remain unchanged - for instance tabs for switching between these fragments. A nice bonus is that reusing that content page in another activity is very easy, and should you choose create a tablet-friendly version you can easily compose more complex views of your existing fragments.
Using a ViewPager together with some type of page indicator, such as the tab indicator here you can have an active fragment and easily switch between them.
Since you are targeting 2.1 you will need to use the android support library to support fragments.
I have 5 unique pages of xml that are fairly complex. I want to put the 5 pages inside a ViewPager. All of the samples I can find simply put identical contents in each page via code. I want to declaratively define the xml in the viewpager like the xml pasted below. But this does not work - the app stops with this xml.
If I can't declaratively define it, then can I load individual xml pages into the viewpager? I can find no examples that do this.
thanks,
Gary Blakely
<?xml version="1.0" encoding="utf-8"?>
<fragment >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/imageView1"
android:src="#drawable/flashright" android:adjustViewBounds="true" android:scaleType="fitXY">
</ImageView>
</LinearLayout>
</fragment>
<fragment >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/imageView1"
android:src="#drawable/flashleft" android:adjustViewBounds="true" android:scaleType="fitXY">
</ImageView>
</LinearLayout>
</fragment>
If I follow, you want to declare everything in XML and avoid any programmatic initialization from within your Activity containing the ViewPager. The only way I can think of doing this would be to define 3 different Fragment classes which reference 3 different xml layouts. You could then embed them in your above xml, replacing each <Fragment> element with <FragmentX> <FragmentY> <FragmentZ> etc. Personally, I would rather write 3 xml layouts and create a single Fragment implementation that takes a single int argument to designate which layout to load, and then do the small amount of programmatic initialization necessary to make such a solution work. The rationale being that there is less duplicated code.
EDIT:
Another approach that might more fully address your requirements is to replace your <Fragment> tags with <Layout> tags and give them id's. Then in your Activity code, store refs to them, remove them from the View in onCreate() and finally add each of the stored references into programmatically created instances of Fragment which you then add to your ViewPager. Very ugly but thats the only way I know of to declaratively define everything in XML. You'll still have the problem that its not clear from the XML that these elements are nested within a ViewPager so personally I don't see the point. ListView and ListFragment operate with the same kind of implicit association though so its not unprecedented.