Add background image to individual fragments - android

I have an app with multiple fragments and I would like to know how to add a background that is different for each fragment. The layout I am using has scrollable tabs which all use the same xml file. I also have a MainActivity that sets the view and an adapter for each fragment. I know you can add a background using the xml file with android:background or something of the sort as well as setting it to the view in the main activity but I can't figure out how to do it to each tab. Thank you for any help!

To add background to fragment, you have to wrap it in some container
<LinearLayout
android:id="#+id/linearlayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccc"
android:layout_weight="1"
android:orientation="vertical">
<fragment android:name="com.example.simplefragmentexample.LayOutOne"
android:id="#+id/frag_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
If you wish to use same xml file, you should set images programmatically
LinearLayout l = (LinearLayout) findViewById(R.id.linearlayout01);
l.setBackground(Image);
or use several xmls with android:background.

You can get the root View of the fragment with the Fragment getView() method. Then you can set the background of the View using one of the setBackground() methods of the View. For example to set a random background color to each fragment:
for ( Fragment f : fragments ) {
f.getView().setBackgroundColor ( (new Random()).nextInt() );
}
PS: I never used fragments, therefore my answer could be wrong.

Related

What is the best way to add footer to all fragments in Android

I have one Android application(Xamarin.Android) and it has many fragments.
All fragments have the link to website in bottom.
Ideally I'd like to create it as custom fragment with link to website and add to all fragments.
But I don't find any way to add this fragment to XML of every fragment without coding.
I don't want to add the custom fragment to FrameLayout of other fragments in code.
Please let me know if anyone knows best solution for this kind of footer.
Thanks.
You can add the footer to the activity and use fragments for other screens. (Fragment frame layout above the footer in the main activity obviously)
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer">
</FrameLayout>
<FrameLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:text="Footer content"/>
</FrameLayout>
You can make a custom layout with your footer view at the bottom of the view and extend it for all your parent layouts.
Here is the simple way that I found.
Create new XML layout for footer
Create custom fragment class for this footer
Add this footer fragment to end of layout as needed as following
That's all!

Two fragments and a button in Landscape mode in XML

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.

Reuse a group of buttons across several fragments

I have a fragment defined by the following xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_example"/>
</RelativeLayout>
Now I would like to reuse the ImageButton (and other buttons I will add) in another fragment, ideally
without copying its definition
by keeping the definition in xml and not adding it programmatically
I.e. the goal is to overlay the same set of buttons in different fragments.
Is there a way to define all buttons in a separate xml file and load them programmatically on fragment creation?
Yes, You can.
Define all buttons in different xml say layout_buttons.xml
and add them in each fragment layout using
<include layout="#layout/layout_buttons" />

Placing button or other widget at runtime on android app

Quick question: at runtime I do a boolean check, if it returns true I would like to have two buttons in a relative layout on my MainActivity class. If its false I want to instead have two other widgets where those buttons would be (or near enough). How do I do that?
you could also implement a ViewSwitcher where a more complicated set of buttons/widgets can be switched out very easily with a single call to
ViewSwitcher mViewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
if (some_logic == true) {
mViewSwitcher.showNext();
}
Set up your XML like this and the above will switch between the two LinearLayouts:
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
--- buttons, Views, whatever---
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
--- buttons, Views, whatever---
</LinearLayout>
If you have just those two alternatives put them both in your layout and hide / show the one you want. View#setVisibility()
If you want it more dynamic you can add and remove widgets programmatically. ViewGroup#addView()
Modifying a RelativeLayout during runtime is quite complicated since you need to set all those layout parameters so you could add a simple layout like a FrameLayout in the place where the buttons should go and put them inside the frame. Has the advantage that you can setup all the relative layout parameters for the frame in xml.

set fragment class at inflation time

i have about 60 fragments "ListFragment" , now i use each Listfragment with another fragment in the same Layout file like this.
<?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="horizontal" >
<fragment
android:id="#+id/carDetailedFragment"
android:name="com.ui.fragment.CarDetailedFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<fragment
android:id="#+id/carcategorylistfragment"
android:name="com.ui.fragment.CarCategoryListFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
the Question is :do i need to make 60 layout file for each fragment or can i make a templete layout that contains 2 fragment and set their class
android:name
attribute at inflation time like setting attribute for the View ex
setContentView(R.layout.somelayout);
View v = findViewById(R.id.src);
v.setVisibility(View.GONE);
Yes, but you can only do it in Java code.
In your XML, you'll have to have some sort of container for the Fragment. I believe that I just use a FrameLayout. Then you can do something like
YourFragment details = new YourFragment();
getFragmentManager().beginTransaction().add(R.id.container, details).commit();
EDIT: As a note. I don't believe that you really want to be newing up 60 fragments at once and keeping them and keeping them around in memory. The question you need to ask yourself is "Does this need it's own lifecycle?" If the answer is no then you can just create a custom View.
I have a ton of layouts for ListView Activities and made it that I only need one ListView Fragment. Use this - It will greatly reduce the amount of classes and redundant code needed.

Categories

Resources