I have a generic layout, to which I attach fragments:
<?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:background="#drawable/back"
android:orientation="vertical" >
<include
android:id = "#+id/login_header"
layout="#layout/header_layout"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I have a fragment which I want to display on the right side of the screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/two_third_width"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="#dimen/small_margin"
android:background="#drawable/stock_price_bg"
tools:context=".FoundItemFragment" >
</RelativeLayout>
The layout is displayed left-justified rather than right justified. When I color the fragment_container FrameLayout, I see that it takes the entire width of the screen. I attach the fragment with the following code in my "FoundItemFragment extends Fragment implements OnClickListener" class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragView = null;
try {
fragView = inflater.inflate(layoutID, container, false);
//LoadData(fragView);
} catch (Exception ex) {
ex.printStackTrace();
}
return fragView;
}
The container parameter passed to this routine has the id of the fragment_container layout, so I am not attaching it to some other object which might have settings that override the alignParentRight setting. Why is the fragment displayed left-justified rather than right-justified?
The layout parent should be Relative Layout so that alignParentRight will work .
You are using the alignParentRight for a root element RelativeLayout
Just edit your container xml to
<?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"
android:background="#drawable/back" >
<include
android:id = "#+id/login_header"
layout="#layout/header_layout"
android:layout_alignParentTop = "true"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:below = "#id/login_header"
android:layout_alignParentRight="true"/>
</LinearLayout>
<?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"
android:background="#drawable/back"
android:orientation="vertical" >
<include
android:id = "#+id/login_header"
layout="#layout/header_layout"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Since generic layout hold the container of the fragment, you should align/place the fragment there.
Placement of the views of the fragment should be done in fragment specific xml file
Related
I need to create i number of photo_view and give it a special amount every time (loop). number of photo_view in every time will be different. What exactly should I do in the code ?
my xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/exit_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="20dp"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_back_ltr" />
</android.support.design.widget.CoordinatorLayout>
Dynamically adding Views to a ViewGroup is what you are looking for. which is done by code not by XML.
In general, there are two ways to add a view dynamically,
either by inflating some layout file in a view then add this view to your parent viewgroup (LinearLayout for ex) by calling the add method on this parent view.
Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup.
See an example about way 1 here
See an example about way 2 here
in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. then add an id for the parent linearlayout
<LinearLayout
android:"#+id/parentViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
And in your code, loop by the count and add the custom inflated view to parent. like this:
View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);
for(int i = 0 ; i<count ; i++){
parentViewGroup.add(inflatedView);
}
I would like to get the top-level layout of this sample android xml layout. The top-level I'm referring to is the ViewGroup that is holding the whole activity together.
I am aware of
getWindow().getDecorView().findViewById(android.R.id.content);
But I don't need the DecorView.
Please refer to the xml below.
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout">
<WebView
android:id="#+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
I need to get the RelativeLayout (which is the top-level layout I'm referring to, please correct me if I'm wrong with my terms). I can get it by using the code below.
WebView webView = (WebView) findViewById(R.id.my_webview);
webview.getParent().getParent()
However, what if the Layout is nested deeper like
<Layout>
<Layout>
<Layout>
<Webview/>
<Layout>
<Layout>
<Layout>
How will be able to get the top-level Layout without using getParent(). Thanks!
In xml
<?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"
android:id="#+id/parentLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout">
<WebView
android:id="#+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
In your activity
RelativeLayout parentLayout=(RelativeLayout)findViewById(R.id.parentLayout);
or
getWindow().getDecorView().getRootView();
or
getWindow().getDecorView().findViewById(android.R.id.content);
or
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
I have a viewpager (with actionbar sherlock) containing 4 SherlockFragment, all inside a SherlockFragmentActivity.
Each tab has it's own backStack (i can stack several fragments ON each SherlockFragment)
What i'm trying to do is when i select an item in the list, i want to make visible a ViewStub (which is a player)
My problem is that the ViewPager Does not redim itself. The player become visible but behind the viewpager.
Here is the main SherlockFragmentActivity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/ActivityDark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context=".ActivityMain" >
<ViewStub
android:id="#+id/main_stubplayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:inflatedId="#+id/fragment_allzic_player"
android:layout="#layout/view_player" />
<android.support.v4.view.ViewPager
android:id="#+id/main_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/main_stubplayer" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
Here is the ViewStub :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/banner_player_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/cell_height"
android:background="#color/clr_player"
android:clickable="true" >
[...the content does not matter the height is fixe]
</RelativeLayout>
Here is how i make is visible :
public void showPlayerBanner(String title) {
if (mViewPlayer == null) {
ViewStub stub = (ViewStub) findViewById(R.id.main_stubplayer);
mViewPlayer = stub.inflate();
} else {
mViewPlayer.setVisibility(View.VISIBLE);
}
}
Try LinearLayout vertical instead of RelativeLayout and set the min/max height to the viewstub.
To know more about the layout differences, the article is here
I'm having an issue where my Fragment view inside a LinearLayout isn't streching to the parent's height and width. But it is a little bit more complicated since I have two Fragments inside that Fragment.
Here's the Activity's view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center|fill"
android:orientation="vertical"
android:padding="8dp" >
</LinearLayout>
Here's the parent fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:background="#FF00FF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/mtg_player1_fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000FF"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/mtg_player2_fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FF0000"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
And the child (player) fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mtg_player_life_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/mtg_player_life_count"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:gravity="center_vertical|center_horizontal"
android:text="0"
android:textSize="140sp" />
</LinearLayout>
I'm adding the parent fragment like this (via clicking on a list item):
getSupportFragmentManager().beginTransaction().replace(R.id.main_content, fragment, newFragmentClass.getSimpleName()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
And the children fragment like this (on the parent's onViewCreated()):
mPlayer1Frag = (PlayerFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.player1_fragment_wrapper);
if (mPlayer1Frag == null) {
mPlayer1Frag = new PlayerFragment();
}
if (!mPlayer1Frag.isVisible()) {
((RoboSherlockFragmentActivity)getSherlockActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.player1_fragment_wrapper, mPlayer1Frag).commit();
}
mPlayer2Frag = (PlayerFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.player2_fragment_wrapper);
if (mPlayer2Frag == null) {
mPlayer2Frag = new PlayerFragment();
}
if (!mPlayer2Frag.isVisible()) {
((RoboSherlockFragmentActivity)getSherlockActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.player2_fragment_wrapper, mPlayer2Frag).commit();
}
What I get is the following:
What basically happened is:
The parent Fragment view didn't stretch vertically to fill the entire activity's view (no pink appeared);
The TextView (green) didn't stretch to cover the children fragment layout (red/blue);
What I want to happen is for the parent UI to stretch in the entire activity, for the children to cover the entire parent and for the TextViews to cover the entire child. I could change it to use a single fragment (and may well do that), but I'd prefer if I could keep it this way, since it feels more organized :)
So, the question is, what am I doing wrong? Can't this be done using two fragments inside another?
In the onCreateView() method of a Fragment always inflate the layout file using the container parameter of that method like this:
inflater.inflate(R.layout.parent_view, container, false);
This way the inflated view will be given proper LayoutParams and it will behave as designed.
I am trying to create an activity with 2 Fragment
First fragment show a list of galleries using a LisFragment
Second fragment show the preview images of the gallery selected in the first fragment
When I declare the fragment in the view it works. But now I want to manage fragment dynamically :
GalleriesFragment gfs = new GalleriesFragment();
GalleryFragment gf = new GalleryFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.layout_m,gfs);
ft.add(R.id.layout_m,gf);
ft.commit();
My Layout:
<?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="horizontal"
android:id="#+id/layout_m">
</LinearLayout>
When doing this only the first fragment added is shown, I tried to invert their order when adding fragment...
When I was working on the layout I could set my fragment width with:
android:layout_width="400dp"
Now I have 2 questions :
How can I make my 2 fragments to display?
How can I set the width (programmatically?) of the 1st fragment and the second to fill the remaining width?
Thanks for your help!
PS:
GalleriesFragment doesn't have a layout and is not overriding onCreateView, but has an ArrayAdapter returning view like this for each rows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/idimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
android:contentDescription="preview"
>
</ImageView>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/texttitre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="7sp"
android:layout_marginTop="10sp"
android:background="#layout/roundcorner"
android:gravity="center"
android:text="Title here" >
</TextView>
<TextView
android:id="#+id/itemdescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:text="description" >
</TextView>
</LinearLayout>
</LinearLayout>
GalleryFragment :
<?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" >
<Gallery
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Update 2 :
I added an onCreateView in my GalleriesFragment class :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FrameLayout.LayoutParams lparams = new FrameLayout.LayoutParams(400, LayoutParams.MATCH_PARENT);
container.setLayoutParams(lparams);
return super.onCreateView(inflater, container, savedInstanceState);
}
This fixes the first fragment's width, but the second one is still not showing...
How can I make my 2 fragments to display ?
Your current approach will make the first fragment supplied to the add method to take all the space of your container layout. One solution is to use a layout that reserves space for each of the two fragments that get added, something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_m"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/frame1"
android:layout_width="100dp"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/frame2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Then in code:
ft.add(R.id.frame1, gfs);
ft.add(R.id.frame2, gf);
ft.commit();
How can I set the width (programmatically ?)of the 1st fragment and
the second to fill the remaining width?
Fragments are not just ordinary views but one way to do that is to use your single LinearLayout layout file and set the LayoutParams of the fragment's view in onActivityCreated:
getView().setLayoutParams(new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.WRAP_CONTENT));
Of course this will make your fragments to be tied to the parent container, also I don't know how well this will work in the overall fragment system.