Setting Background For a Fragment? - android

I am using fragments in my application, and I have to change the Background for one of the fragments within FragmnetTabs. For this very reason I am setting the background in the layout of that fragmnet, which I can see in Graphical Layout of Eclipse is working fine. But when I run my application it shows me Fragments content with black background, instead of the image I set. I am setting it this way...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg" >
<RelativeLayout/>
But this drwaable does not show up in the application. What could be the problem? Any help is appreciated. Thanks!
Edit:
If I try to set the background in the following way, it works fine.
android:background="#drawable:color/anycolor"
Why is it so that custom image is not getting shown up but android colour does?

You can try to do this in code. Call on your RelativeLayout
yourRelativeLayout.setBackground(getActivity().getResources().getDrawable(R.drawable.YourImage));

Related

Within a new activity, ViewImage does not show an image

I created an Activity, which I load from a fragment within my "main" activity.
Within this new activity I placed an ImageView with an image from my drawable directory (I have about 10 other drawables I present the exact same way in the main activity).
The problem is that although in the IDE I see the image, it is not displayed in run time (via the simulator).
-- to clarify, the image is static hence the difference compared to other questions (I'm not loading it by code).
Any ideas on how to solve it?
My hunch is that it relates to the fact it's a new activity, but I'm new to android development, so I can't base it on any knowledge...
I did not add any code to the java section of the activity (didn't touch any "on.." nor added functionality to this specific file)
P.S. I tried cleaning the project, tried presenting an image that is presented on the main activity, and restarting anything that I can... nothing helped :(
My Activity xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="reducted">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/manageSubscriptionScreenLogo"
android:layout_width="match_parent"
android:layout_height="110dp"
app:srcCompat="#drawable/managesubscription_header" />
</LinearLayout>
</RelativeLayout>
It doesn't matter I take the ImageView and place it outside of the LinearLayout (and remove it), or change the size to wrap_content or match_parent (I tried all combinations), the image is not presented.
How it looks in the IDE:
And how it looks in run time:
Try using android:src="#drawable/managesubscrip_header" instead of app:srcCompat="#drawable/managesubscription_header" in ImageView to avoid automatic scaling of the drawable.
Let me know if it helps

Android fixed background image in scrollview while scrolling

I have a situation where I need a scrollview to have a background image that shouldn't scroll along with it's parent when moving. Before any of you suggest me the links for setting background image and this that, I have already tried and it's not working.
The whole story goes like: I have an activity with fragments which have their own backgrounds with some input fields. When focusing over input fields, keyboard appears and background image squeezes. For that I put an image on background of scrollview that fixed my issue of squeezing background but raised another concern that background Image should stay static while scrolling the parent scrollview.
The second solution any of you may suggest is setting background of my activity rather playing with scrollview. That's right, but for that I had to make a style element with background of theme which appears odd while transitioning different fragments plus it adds overhead when I have a lot of code and fragments to move forward and back.
That's the point where I am stuck. I have gone through links below, if you just need to know that I tried it or not.
link1
link2
link3
... and so on
below is the layout I am using for my fragments (it's all being done programmatically)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/backgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY" />
<LinearLayout
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</ScrollView>
</LinearLayout>
So if you guys have any better solution keeping in mind the situation I have, will be warmly welcomed. Thank you
None of the above works for me with some reason but this things works.
getWindow().setBackgroundDrawableResource(R.mipmap.img_reg_bg);
put it in oncreate method hope it will help.
If I guess right you try to fix your hotfix to get it working propperly. If I'm right you sould go back to scratch and do it without your scrollview.
If you have a look at following Link:
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
you'll see softkeyboard mode can be adjusted. "adjustPan" should solve your problem.
"adjustPan"
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
Add someimgage to your drawable folderes, like drawable-mdpi, drawable-hdpi.
In your LinearLayout "#+id/top_layout" add attribute:
android:background="#drawable/someimage"

How to control height of card fragment in android wear?

I have an customized chart in card fragment which is a card showed on my LG G watch , I want to make it show in just one page without scrolling down.I tried to use LayoutParams and make it wrap content,but it does not work. ps: I'm using the GridViewPager widget.
How to make it looks like the what the 3rd pic shows.
Have you tried
android:layout_width="match_parent"
android:layout_height="match_parent"
Here is a sample app for GridViewPager in GitHub.
Its layout XML implemented as below:
<android.support.wearable.view.GridViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true" />
You can use Fragment instead of CardFragment and set your custom layout in onCreateView().
try to use RelativeLayout as parent for your views
I solved this problem by
fragment.setExpansionEnabled(false);

change the background color of the application from a fragment

I am currently using an architecture where I have multiple fragments that I swap in the same activity.
I need to be able to use a different background color for one of these fragments BUT I don't want to use the lazy solution that adds one layer of overdraw.
For clarity : I have a window background setted by my theme and on top of it I draw cards & lists. In one of my fragments I need a slightly different window background color.
Is there any way to do that ? I already tried to use a ContextThemeWrapper but it does not seem to work (maybe because the background has already been drawed ?)
Thanks in advance for your help.
Just found it :
It is doable by calling
ColorDrawable cd = new ColorDrawable(getActivity().getResources().getColor(
R.color.your_color));
getActivity().getWindow().setBackgroundDrawable(cd);
during the initialization of your fragment, when it is attached to the activity.
Unfortunately changing a fragment's background at runtime isn't supported. To get this functionality you can wrap your fragment inside of a RelativeLayout or something similar and set the background programmatically on that.
<RelativeLayout
android:id="#+id/fragmentContainer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ccc">
<fragment android:name="whatever.fragment.name"
android:id="#+id/frag_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Then in your code get by id fragmentContainer1 and do view.setBackgroundColor(YourColorVariable) on it.
Another way may be to get your fragment, find the root view, then set the background on that. fragment.getRootView().setBackgroundColor(Color.WHITE) This depends on your fragment's layout though, so it may or may not work.

Change the view background when an item from a ListView is activated/selected

I'm going to explain (or at least try that) what I want to achieve because this is driving me crazy.
I have a ListView and the items have the following layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/item_background"
android:duplicateParentState="true" >
... some contents here...
</FrameLayout>
</FrameLayout>
"item_background.xml" contains a selector so the background changes depending on the item's state. It's working fine when I press and release the item but it's not working when I 'select' the item with a DPAD/Trackball. I have tried everything (state_activated, state_focused, state_selected...) but nothing works. I'm trying to search for a solution but looks like I can't find the proper keywords to search for.
Any ideas?
Thank you!
Finally I fixed it. The problem was in the order I put the items inside the selector (I put a state_pressed="false" before the state_selected="true").
Now it works flawlessly.
Thank you everyone for your help.
This does most likely not work because some layout above your root layout gets the focus (hard to tell without the rest of the layout).

Categories

Resources