Background Image in Android - android

I´m trying to put a background image in my layout, I put the path of the file (on drawables) in the "background" propierty, it shows in the xml preview, but when I run the app on my phone it doesn´t.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#drawable/background_image">
</LinearLayout>
My onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Also, I was trying this tutorial --> link but it doesn´t works, I can call the xml file and it shows an error.

There's one case where images wont be shown, if the dimensions of your drawable exceeds 2048 pixels in any axis (on non-HD devices only).

Related

PNG not showing in ImageView

I have a large .png and i want to show it in a Splash screen, but the image is not showing. The xml:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.infaplic.lpi.activities.SplashActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"/>
And the code of the Activity:
public class SplashActivity extends Activity {
ImageView imagen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imagen=(ImageView)findViewById(R.id.imageView2);
imagen.setImageDrawable(getResources().getDrawable(R.drawable.pantalla_arranque));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, NextActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, 1000);
}
}
This way, the image is not showing. I have tried with android:src: in the xml, but it doesn't work.
The image is very large. Do I need to resize it before putting it in the ImageView? If not, why is the image not showing?
Thank you.
EDIT: The PNG is 1080x1920
In my case I was using a image with file name like "go-back-filename.png". And xml file is like
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/go-back-icon"/>
After i changed the file name to "go_back_icon" it worked fine.
Hope this helps someone.
Since you write that your image is very large I would guess it is too large. I have a bug in one of my apps there I generate an image which is bigger then 2048 pixels. Some devices does not support such big images I would guess that you run into a similar problem.
If that is some background image reduce the image size, a background image is not so important that each pixel on the screen has to own a image pixel.
Add Below this. Please read Options for scaling the bounds of an image to the bounds of this view.ImageView.ScaleType in your ImageView
Use This
imagen.setImageResource(R.drawable.pantalla_arranque);
Then
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
android:adjustViewBounds : Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Must be a boolean value, either "true" or "false".
Please Check demo ImageView scaleType Samples . I hope it helps you
Change your ImageView to this :
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:scaleType="fitXY"
android:adjustViewBounds="false"/>
If it's not working try to add this on your manifest (inside your Activity) android:hardwareAccelerated="false"
EDIT
I was looking for the answer, but I thought that better try it by my self... and I've created a sample project where I use an ImageView 1080x1920 and I was testing since I've found that I've I put 3000 miliseconds it shows up... wiht 1000 miliseconds won't show up if you don't rescale it.
By the way getDrawable() is deprecated I recomend to you use :
imagen.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.imagenprueva));
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/imagen"
/>
</RelativeLayout>
Here's the full source code hope it helps Source code
In your .xml file
add
xmlns:app="http://schemas.android.com/apk/res-auto"
then in the ImageView add this line of code:
app:srcCompat="#drawable/books"
NOT
tools:srcCompat="#drawable/books"

Image not resizing in Android Studio's emulator

I'm using a USB connected device to run my app. In this practice app, I used an Android logo image to implement the zooming feature. When I try to change the image dimensions in the activity, it does not show the changes when the app imported into the usb device is loaded. The logo is simply zoomed to fit. But I want it to display the image with the way I see it in the Design view: http://i.imgur.com/hcaOHjW.png
I forked the code from this source to implement the zooming: http://www.c-sharpcorner.com/UploadFile/88b6e5/multi-touch-panning-pinch-zoom-image-view-in-android-using/
It does not have the double tap zoom in and out feature so let me know what I can add to implement that
And here is the code for my activity:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.test.firstapp.Login"
android:id="#+id/activity_activity2"
android:focusable="false"
android:nestedScrollingEnabled="false">
<ImageView
android:layout_width="180dp"
android:layout_height="220dp"
android:id="#+id/botImg"
android:src="#drawable/bot"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"/>
in your java class try creating this view and then try changing the height and width parameters .inflate this view dynamically and assign parameters in the java itself dont use xml for this view .

How to make android app screen go full screen?

I am making a browser app on android platform. I am testing my app on 2.3 version of android. The problem which I am facing is, I am not able to make the app screen full screen. I want the "WebView" to take full screen, but notification bar should be seen. Also, the title bar should be hidden.
I had tried using these :
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
But still I can see the title bar in my app.
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
This hides the notification bar, but I don't want that. I just want to hide the title bar. I had also tried using theme in Manifest file such as:
android:theme="#android:style/Theme.NoTitleBar.Fullscreen
OR
android:theme="#android:style/Theme.Black.NoTitleBar
But with this, my app doesn't start.It stops abruptly.
Also my webview and URL address bar kind of displaying in center. They are leaving some space on all four sides. Like this:
How to make a full screen webview
How to remove it make it full screen.
Pls Help.
To disable Title bar, try adding this in Manifest -
android:theme="#android:style/Theme.NoTitleBar"
or try this:
Add this in styles -
<style name="Theme.Transparent" parent="#android:style/Theme.Translucent.NoTitleBar.Fullscreen">"
<item name="android:windowBackground">#000000</item>
</style>
and then set theme as this in Manifest -
android:theme="#style/Theme.Transparent"
According to me, requestWindowFeature(Window.FEATURE_NO_TITLE); should work fine. Just once try adding it before setContentView(...) like this -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(com.android.internal.R.layout.preference_list_content);
...
}
I am not sure but probably your WebView is leaving space due to default margins. Your layout file must be somewhat like this -
<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"
//REMOVE THIS CODE
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
//TILL HERE
tools:context="com.example.web.MainActivity" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent" //MAKE THIS MATCH_PARENT
android:layout_height="match_parent" //MAKE THIS MATCH_PARENT
android:layout_alignParentBottom="true" />
...
</RelativeLayout>
Remove this -
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
Also make sure you don't have any other margins and padding and also make sure WebView's height and Width is match_parent...

How to wait for an ImageView in xml to finish loading

I want to create a title screen for an Android app. The screen is a simple XML file with an image in it. Basically the screen will show until some other stuff finishes loading, upon which the screen will change to the app content. The problem is, many time the image in the XML doesn't appear at all - all I get is a blank xml page, and afterwards the app loads. I assumeit's because loading the image is done async or something. I tried to use Thread.sleep, which proved to be a bad idea.
Anyone has an idea how to get this to work?
Thanks!
The main xml:
<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=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp"
android:src="#drawable/icon" />
The main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<do some stuff>
start_other_activity_with_different_xml();
Ok, got found the answer faster than I expected. For my purposes, what I'm looking for is a splash screen, and a good explanation of how to create one can be found here: http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/

In Android, what is similar with UI view of IOS?

In IOS, an image which can moved is attached like a magnet to a place to a place. I don't know what is like those method in android.
With an Activity you have a layout file. In that layout file you have multiple items that represent your view. The top most item is what is the same as UIView. You can give this layout an id like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/uiview"
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" >
Edit
Today we also have ConstraintLayout, which is like RelativeLayout but uses constraints (like iOS) to layout its childs. You can look here for more information: https://developer.android.com/training/constraint-layout/index.html

Categories

Resources