I have a problem with getting the background to display in the emulator. It will show up on main.xml though and there is no errors. So the screen just comes up black here is my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background = "#drawable/clearskies">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
and yes I do have my image in the drawable folder and the spelling is correct and the image is a .png
Can anyone see what im doing wrong here?
Thanks
Check if you are setting the layout file.
in onCreate(), use,
setContentView(R.layout.main);
Can you see your image in background of graphical Layout of your xml if yes then it must problem with your java file otherwise it problem with your image or drawable something else...
Related
I have few images that i want to use for the help. Where each image will be used in one activity. i have the images in drawable directory and referencing them using aliases for the different configurations. the images are extension is png.
in the UI designer the image is displayed, but not in run-time. it show white view
my Layout
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<View
android:id="#+id/help_img_screen"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#drawable/todays_reminders_help"
android:contentDescription="#string/content_description" />
<CheckBox
android:id="#+id/help_chx_dont_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/dont_show_help" />
</LinearLayout>
my drawable todays_reminders_help.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="fill"
android:src="#drawable/todays_reminders_activity_help" >
</bitmap>
i am getting white view at run-time not the image i am referencing? this happen on my test device.
However, when i test on the emulator it worked with no issue
You can try couple of things:
1. In your todays_reminders_help.xml, try to set the android:layout_width and android:layout_height to match_parent
2. Just to make sure if things are working as expected, you can test by setting a non-zero height for View with id help_img_screen. i.e replace
<View
android:id="#+id/help_img_screen"
android:layout_height="0dip"
android:layout_weight="1"
..../>
with
<View
android:id="#+id/help_img_screen"
android:layout_height="100dp"
..../>
and see if that helps. If it works, then you need to figure out why layout_weight is not working - you might have to use hierarchy viewer tool to see why it's not working.
Lastly, i would recommend that you approach this problem in a different way -
Make the View an ImageView and in real time i.e in the Java code, you can set the image src or background dynamically based on the activity/context. You can completely avoid using another layout file todays_reminders_help.xml
Hope this helps.
I have an ImageView that when I look at in the XML preview layout on Eclipse looks fine, but when I launch the app on my hardware device a big whitespace comes to surround it. This is what the actual image is, without the whitespace that is being added. Here is my XML file too. How do I get rid of this whitespace?
Since your image shouldn't be too large, try this:
<?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" >
<ImageView
android:id="#+id/linkMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="cat"
android:src="#drawable/ashsc" />
</LinearLayout>
This way, the image won't attempt to scale and won't create bounds outside the ImageView's image.
I'm new to Android and I wonder why I can't drag and drop things like buttons to the display in the main.xml Graphical Layout? It seems like it's locked or something else that I can't figure out on myself. Help is preciated! Thanks!
EDIT: This is my code in xml file
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello"
/>
You are not able to do so because you don't have a Layout in your XML. Given here is an example as to how to build a basic layout. You can also refer to the Relative Layout tutorial or Linear Layout tutorial for more details, as these are widely used layouts.
Given below is the sample code for your XML:
<?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="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
You can replace Linear Layout with Relative Layout at your convenience. But do keep in mind that you can't use the graphical layout without having a layout. To drag and drop a layout, refer the image below:
Hope this helps.
If that's your code, then it seems that you might be trying to drag elements into the TextView since it is filling the entire layout. Try to remove the TextView and see if you are then able to drag and drop elements again. Either that or change it to:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
I am a begginer Android Studio to. I have same problem about drag and drop in design screen (in GUI). I solve the problem by using "res/layout/content_main.xml" (left side of the screen). It works! but ı am not sure that is it the right way :)))
Drag and drop the elements onto the Outline area instead of the display itself.Worked for me.
I am trying to change the background of whole screen to white. I doing it by adding by android:background="#ffffff" attribute to linerlayout. but it is not working, i dont know what i am missing ?
http://developer.android.com/reference/android/widget/LinearLayout.html
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="10dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
**android:background="#ffffff"** >
I also tried to this programtically , something like this
setContentView(R.layout.main);
LinearLayout linLay = (LinearLayout) findViewById(R.layout.main);
linLay.setBackgroundColor(Color.WHITE);
but application throws error
Are you please able to post the complete code necessary to replicate your problem?
This works fine for me:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
android:layout_height="fill_parent"
also setBackGroundColor(int) takes a #color resource , not a color integer. you declare them in values/colors.xml
I'd like to know how to draw two PNG pictures onto the screen.
My XML layout: (named paperxml.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/paperid"
android:src="#drawable/paperrepresentation"
/>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rockid"
android:src="#drawable/rockrepresentation"
android:layout_alignTop="#id/paperid"
/>
</RelativeLayout>
What would be the java code to instantiate the XML layout and display both ImageViews on the screen at the same time?
Simply calling setContentView(R.drawable.paperxml); crashes my application on startup.
Replace the xml with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView android:id="#+id/paperid"
android:src="#drawable/paperrepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/rockid"
android:src="#drawable/rockrepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Explanation:
RelativeLayout doesn't use android:orientation="vertical".
Every view should have android:layout_width and android:layout_height.
Add the xmlns:android thing just in the first element.
Calling setContentView(R.drawable.paperxml); is not crashing your code - it's your XML file. Macarse has the correct answer to your problem, and keep your code the same!
You may also want to look at the View Tutorials for some examples of setting up your XML and using different View objects.
I put the XML in but it only displays one ImageView Here's a screenshot of the emulator I took. i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png Its worth mentioning that the file shown is paperrepresentation
If you look hard enough you will see that at the bottom there is a second very tiny image. You just need to increase the scale.