Add background image to RelativeLayout in android - android

I tried to add a background image to an Android app using android:background="#drawable/imageURL". I added the image to the drawable-hdpi folder, but I'm getting an error. I'm trying to add the image to a RelativeLayout. I also tried to use LinearLayout but it didn't work either.
So how to add background image to my app within RelativeLayout in Android?
What is the error?
<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:background="#drawable/abc.png"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>

replace
android:background="#drawable/abc.png"
with
android:background="#drawable/abc"
From the documentation:
May be a reference to another resource, in the form
"#[+][package:]type:name"
as you can see the extension is not part of the value of the attribute

just write #drawable/abc instead of #drawable/abc.png

Related

Image with transparent background start to have one in android studio

I'm trying to add an image with transparent background in android studio to a new empty project, but when I add it, it keeps giving me the transparent background as a background
Here's the image I'm trying to add
Here's what keeps happening
I tried many other images but the same thing keeps happening and I looked online but all I found was how to make the image itself transparent or semi-transparent but not the background
Here's the code in the xml file if it helps
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.ahmad.connectfour.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
app:srcCompat="#drawable/eye"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
the java file is not edited at all
use this eye icon:
visit the link for more icons: https://material.io/resources/icons/?icon=check_circle_outline&style=baseline

Android View View XML Programmatically

I'm trying to debug a view that I'm creating programmatically. I'd like to be able to see what the exact XML is of the view after it's created.
Really I'd like to be be able to do something like Log.d(TAG,view.toString())
And get an output like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/activity_main"
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="sampleapp.example.com.tr221test.MainActivity">
<Button android:id="#+id/surveyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Survey!"/>
</RelativeLayout>
Is there anyway I can programmatically see what the xml is of a view?
There is nothing built in for that. You would have to write your own View-hierarchy-to-XML code or see if somebody has a library for that.
Or, use Android Studio's layout inspector.
Or, use uiautomatorviewer.
Or, use Stetho.

Where the right place to set layout in android?

I have 2 files in the res.
Activity_main.xml is
<FrameLayout 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"
tools:context="com.example.test2.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml is
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.test2.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
I plan to change the layout where I problem in this current layout when I drag and drop all item just overlap on each other how to make a nice form type layout? What adjustment and which file must I adjust?
It sounds like you are using the Eclipse GUI tools to create your layout. I strongly suggest that you read about Android Layouts. If you want, you can edit the XML layout text directly or at the very least this will help you understand the properties you can set for your controls to get the exact look that you want.

Background image not loading for relative layout in android

In my android xml code, I want to draw a background image for a relative layout:
I have a file called homeBackground.png. But its not actually showing up when I run on my phone. If I look in the GUI editor for the layout, I can see the background.
Does anyone know what's wrong here?
Thanks.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/homeBackground"
tools:context=".ActivityCourses" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
EDIT:
Now I get this
[2013-10-13 02:21:10 - NAME] res\drawable-hdpi\homeBackground.png: Invalid file name: must contain only [a-z0-9_.]
Capital letters not allowed in resources files.
Instead of
homeBackground.png
rename the file name to:
home_background.png

how to use spinners generated from android-holo-colors

Looking for ways to customize my spinner, I heard about http://android-holo-colors.com so I went there and generated a spinner. When I download the zip, it only contains a res file that has no layout folder. How would I use the generated spinner in a simple bare bone android app? As in I imagine I have to link the generated files to a spinner that I create in a layout file. How would I do that? Say my spinner in the layout is
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/my_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
How do I wire it up with the generated res stuff?
The tool generates a custom pair of theme and styles XML. You should use the theme for your activity/application, or even use only the styles you need.
EDIT: added an example as requested
Given you chose "MyApp" during generation, the zip will contain (besides the drawables and a theme definition) a set of styles (res/values/myapp_styles.xml and res/values-v11/myapp_styles.xml).
In order to use such resources within your layout you have to:
merge the generated res/ folder into your project res/ folder
use the given styles in your layouts accordingly.
eg:
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/my_spinner"
style="#style/SpinnerAppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
where #style/SpinnerAppTheme refers to the spinner-specific style defined in res/values/myapp_styles.xml (and res/values-v11/myapp_styles.xml for API>=11).

Categories

Resources