Creating Background Image for Android App - Java - java

I'm just getting started with android development and I need help with background images. I want to be able to have a background image and then overlay other items (buttons, text, etc.) on top of that background with a layout. I used a LinearLayout just for the sake of being simple and because I don't know what's best for me at the moment.
Anyways, I can't get an image to display using the following code:
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;
public class NewGameActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(Drawable.createFromPath("/assets/images/androidBackground.png"));
this.setContentView(ll);
}
}

ll.setBackgroundResource(R.drawable.image_name);
http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29
This is the preferred way of accessing drawable resources. The image_name is a png image in your drawable or drawable-mdpi folder.

yes if you are using XML then find the id of that linearlayout like
ll=(LinearLayout)findViewById(R.id.linear1)
Then set its background as
ll.setBackgroundResource(R.drawable.image_name);
else here as your code given here you can directly go for
ll.setBackgroundResource(R.drawable.image_name);

Related

How to embed WebView in Java in Android Auto

I'm new to Java and am currently developing an Android Auto app.
It's using WebView to display local HTML files.
I've tried many ways, but everything went wrong.
My App can start in Android Auto, but embedding WebView fails every time.
And the Internet is like empty on this subject.
Here is what I'm trying currently:
package com.example.webviewandroidauto;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.model.Template;
public class StartScreen extends Screen {
public StartScreen(CarContext carContext) {
super(carContext);
}
#NonNull
#Override
public Template onGetTemplate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("file:///android_asset/myresource.html");
}
}
Can someone please help me out?
Thank you!
Proof that my app starts in Android Auto: https://i.stack.imgur.com/OXC80.png

Use MBTiles from online storage with OSMDroid

I have imported OSMDroid in my project and its running quite ok with default OpenStreetMap. I want to use an MBTiles format map instead of OSM. There are a lot of tutorial for using MBTiles as offline map but my MBTiles file is stored in an online storage such as
http://www.example.com/mymbtiles.mbtiles.
I am using the following code in my MainActivity.java which is xerox of default tutorial of OSMDroid
package com.example.user.osmdroid;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
public class MainActivity extends Activity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(9);
GeoPoint startPoint = new GeoPoint(23.7588, 90.38967);
mapController.setCenter(startPoint);
}
}
How to use that online stored MBTiles file with my OSMDroid project?
You can't use it directly. MBTiles is basically an sqlite database. OSMDroid can read and use it from the device's storage but not over the network. To solve this, you'll have to transfer the database to the device, then tell OSMDroid to use it the mbtiles file via tile source and tile providers.
Edit: considering the size of the database, the simplest thing for you to do is to host a simple web service that fetches the tiles from the database (server side) then provide them to your users via a custom ITileSource. I actually have code for such as service, but it can't be released (yet). The "standard" osm way is http://server/port:path/Z/X/Y.png but you can make it whatever format you want so long as there's a ITileSource setup on the android device. Also, you probably want to see to this example https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/SampleCustomTileSource.java Note, we're extending OnlineTileSourceBase

Unfortunately my app stopped working? [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
I tried to display "hello world !" by designing an app using a custom font stored in src/Main/CustomF/times.ttf in my laptop.....while execution it disappears very soon telling that "unfortunately your app stopped"....my coding is as below.....please help me..
package com.example.nambimanavalan.customfonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.mine);
Typeface custom=Typeface.createFromAsset(getAssets(),"CustomF/times.ttf");
t.setTypeface(custom);
}
}
you need to put your font in assets/Font folder and than you can use
Typeface custom=Typeface.createFromAsset(getAssets(),"Font/times.ttf");
put your custom font in : assets/CustomF/times.ttf and maybe then it will work
assets folder is directly under your project folder, and whenever you create a fresh project it is empty.

How to use a button to within a app to display the phone's contact list

Hello everyone I am completely new to programing no experience at all so please treat me as a 2 year old when you respond. I have alot of time on my hands as I sit in an office with nothing to do so I figure I should do something productive, and since I have a android tablet I figure I'll try to learn how to make apps for it. This is my first attempt and I'm getting an error in the .java portion of the code, I've been reading and reading on various sites and can't seem to get a clear understanding of what I need to do to correct it. PLEASE HELP
Now down to business, in the app I am trying to learn how to display an image its that simple, I'm taking baby steps. So I started a project, read a few tutorials on how to code ImageView and then I got stuck. I have the error that says "The public type TestImages must be defined in its own file" so I ask how to define/create this file? where to put it?
Here is the .java portion of the code where the error lies.
package com.example.myproject;
import com.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class TestImages extends Activity {
/**Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
ImageView image = (ImageView) findViewById(R.id.test_image);
}
}
Please help, and if you know of any other resource out there that can help a newbie/dummy like myself who don't know anything about coding and need every detail explained please point me in that direction. I've tried the Android Developers site but the information presented on there are not explained in detail it looks as if they expects everyone who tries to do this have a background in coding and I don't. Oh and please don't discourge encourage, because I'm eager to learn.
use on click listener on the button and then check this
https://github.com/android/platform_packages_apps_contacts

how to view youtube video

I am finding great difficulties to view YouTube videos in my app.
here is my code:
package com.example.webvideo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class WebVideo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vv = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
vv.setMediaController(mc);
vv.setVideoURI(Uri.parse("http://youtu.be/2OIOOb-0t44"));
vv.start();
}
}
The emulator is showing an error that the video cannot be played.
What am I doing wrong? Am I giving the URL in a wrong format?
I would expect that the URI you need to give is to the actual media file to be played. I wouldn't count on redirects working either... and anyway that redirect you give seems to point to a YouTube web page, which I sure wouldn't expect the video player to be able to render.
http://youtu.be/2OIOOb-0t44 is certainly an invalid URL. Maybe you meant http://youtube.com/watch?v=2OIOOb-0t44?
The URL you are using is for the webpage where you can view it, not for the video itself. The embedding URL appears to be http://www.youtube.com/embed/2OIOOb-0t44, but I think it's HTML5, not flash... you may have to go old school and track down an AVI or MPEG file.

Categories

Resources