I am trying load a background Image using the glide library so that it fills up the whole screen. I have the following xml for the layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:adjustViewBounds="true"
android:id="#+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="#layout/signup_layout"/>
</RelativeLayout>
My code for in the activity in which I use the Glide library is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginscreen);
// Setup the email field
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
// Setup password field
mPasswordView = (EditText) findViewById(R.id.password);
ImageView imageView = (ImageView) findViewById(R.id.backgroundImage);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Glide.with(this)
.load(R.drawable.green_field)
.override(width,height)
.centerCrop()
.into(imageView);
After running the code the image has white spaces on the left and right of the screen but it fills up the whole height of the image. I have tried to use both .fitCenter() and .centerCrop() together but the Image become pixelated.
It cannot fill the whole screen since you used
android:adjustViewBounds="true"
which orders (docs):
Set this to true if you want the ImageView to adjust its bounds to
preserve the aspect ratio of its drawable.
Also this is buggy on pre API17 as above docs warn.
If you want to fill up whole screen you shall remove android:adjustViewBounds and perhaps consider using
android:scaleType="fitCenter"
Related
I know there are related questions but please try my code 1st
I think my code is correct but I don't know why I can't get want I want
I'm trying to Replace the image into a RelativeLayout with the same LayoutParams dimension and placement(programmatically) then inside that RelativeLayout put a WebView in the center of it.
It was almost done but I'm having trouble for the placement of Webview.
see the images below
activity_main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
// Image
ImageView image = (ImageView)findViewById(R.id.image);
// Container of the image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl_imageView);
loader(rl , image);
}
public void loader(RelativeLayout rl,View view){
// Creating RelativeLayout
RelativeLayout rlnew = new RelativeLayout(this);
// Getting the Layout Parameters of the image such as (position and dimension)
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
// Coloring the background of the new Relative Layout into blue
rlnew.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
// passing the parameters to the new relative layout from the image
rlnew.setLayoutParams(lp1);
// image turns into invisible
view.setVisibility(View.INVISIBLE);
// Creating a webView
WebView webView = new WebView(this);
// load the loading.gif
webView.loadDataWithBaseURL("file:///android_asset/", "<center><img src='loading.gif' style=''/></center>", "text/html", "utf-8", null);
// setting up dimension(height and weight) for the webview
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 60);
// adding position(Center) for the webview
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// setting up Layout parameters for the webiview
webView.setLayoutParams(lp);
// adding the webview to the new Relative Layout
rlnew.addView(webView);
// adding the new relative layout into the container
rl.addView(rlnew);
}
activity_main xml
<RelativeLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#ec0c0c"
android:id="#+id/rl_imageView"
android:layout_marginTop="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<!--Desired Output-->
<!--<RelativeLayout-->
<!--android:layout_width="70dp"-->
<!--android:layout_height="70dp"-->
<!--android:background="#010e6e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true">-->
<!--<WebView-->
<!--android:layout_width="60dp"-->
<!--android:layout_height="30dp"-->
<!--android:id="#+id/imageView"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true" />-->
<!--</RelativeLayout>-->
<!--Default Image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
</RelativeLayout>
Output Images
I'm making a method that will ask for two parameters a RelativeLayout(that contains the second parameter of the method) and View so it can be anything like(Image, TextView, RelativeLayout , etc.,).
This code can replace a specific View like a relativeLayout or textView or Image
and replace it with a relativeLayout with the same size and position of the given View(mine was the image)(It was already Done if you move that image anywhere in the given container the code will replace the image into a relativeLayout no matter where it is as long inside of the given container).
The only problem is the WebView position. . why is webView there?. .I want it in the center of the new RelativeLayout
Can someone tell where did I go wrong
If you have another way just submit answer
Your code is correct, but your xml is not. Try to change your xml below and test again:
<ImageView
android:layout_width="your dimen in dp < rl_imageView. width"
android:layout_height="your dimen in dp < rl_imageView. height"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
P/S: I suggest create this Imageview programmatically also.
I have a simple xml layout that looks 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"
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=".ColorWheel1Activity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="Color Wheel 1"
android:id="#+id/Target"
/>
</RelativeLayout>
and a java script in the form of:
public class ColorWheel1Activity extends Activity {
Paint black = new Paint();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
black.setColor(Color.BLACK);
black.setStrokeWidth(5);
canvas.drawOval(100, 200, 1000, 1100, black);
//don'tknow what to put here
}
I want to draw the oval on the canvas to the inside of the ImageView (android:id Target) in the xml layout but don't know how to do this. I have tried setContentView(R.layout.activity_color_wheel1.xml) which sets the xml layout without drawing the picture. I have also tried setContnetView(canvas) and a few of it's variants which set the entire screen as the canvas, displaying the oval but none of the xml Layout. I have made attempts to locate the Target ImageView with findViewById() but these either crashed the app or did nothing.
I do not want to create an entire xml Layout in java.
I have made attempts to locate the Target ImageView with findViewById() but these either crashed the app or did nothing.
Im not sure how locating the Target Imageview would cause a crash; this definitely seems like the way to go. Try getting a reference to Target, like so
ImageView Target = (ImageView) findViewById(R.id.Target);
Then set the bitmap you created to that ImageView:
Target.setImageBitmap(bitmap);
When i try to import a button from an xml layout file into my View, I get a white strip running vertically along the button when I run the app. The button is found in my layout xml file which I used to integrate with the View. A screenshot can be shown here:(Button is at bottom left of the screen) http://i844.photobucket.com/albums/ab9/lewweihao93/ScreenHunter_01%20Jul.%2026%2018.20_zpsa1idaeao.jpg
The following is the xml code of my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="bottom"
android:text="FIRE"
android:id="#+id/b" />
</LinearLayout>
The following is the relevant part of my main code:
protected void onCreate(Bundle savedInstanceState) {
layout = (LinearLayout) View.inflate(this, R.layout.mbb, null);
super.onCreate(savedInstanceState);
surfaceview=new mbbs(this);
surfaceview.setOnTouchListener(this);
layout.addView(surfaceview);
b=(Button)findViewById(R.id.b);
shot=new SoundPool(5, AudioManager.STREAM_MUSIC,0);
id=shot.load(this, R.raw.shots,1);
bomb=new SoundPool(5, AudioManager.STREAM_MUSIC,0);
id2=bomb.load(this, R.raw.bomb, 1);
Bundle basket2=getIntent().getExtras();
difficulty=basket2.getString("key");
if (difficulty.equals("e"))
{pace=2;}
else if (difficulty.equals("m"))
{pace=5;}
else if (difficulty.equals("h"))
{pace=10;}
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
plusv = BitmapFactory.decodeResource(getResources(),R.drawable.plusv);
minusv = BitmapFactory.decodeResource(getResources(),R.drawable.minusv);
launch = BitmapFactory.decodeResource(getResources(),R.drawable.launch);
target = BitmapFactory.decodeResource(getResources(),R.drawable.target);
cannon = BitmapFactory.decodeResource(getResources(),R.drawable.cannon);
bg = BitmapFactory.decodeResource(getResources(),R.drawable.bg);
setContentView(layout);
tx=1000;tx2=1000;tx3=1000;tx4=1000;tx5=1000;
bx=0;
How do I get rid of the vertical strip along the button?Is this a good way to integrate a layout into a View?
I am using TwoWayView library but i am not able to set the margin between the images. I have used the setItemMargin(1) but its not working.
When i pass big value(75,100) it increases the space between the images but when i try to set images very close to each other lets say 2 or 5 it doesnt work spacing will be same for all values below 10 default margin as shown in the picture below.
Code for adding view dynamically
protected void showTwowayview() {
// Get the welcome card layout and add this layout
// below that.
adapter = new HorizontalImagesBaseAdapter(MainActivity.this,
ImageApplication.fromRecorderPaths);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.welcomeCard);
Log.d("in", "in");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 300);
// Id of the view below which twoway view will be displayed
params.addRule(RelativeLayout.BELOW, R.id.mannual);
// set the layout margin
params.setMargins(LEFT, TOP, RIGHT, BOTTOM);
myLayout.setLayoutParams(params);
TwoWayView lparams = (TwoWayView) getLayoutInflater().inflate(
R.layout.twowayview, myLayout, false);
TwoWayView twowayview = (TwoWayView) lparams.findViewById(R.id.lvItems);
myLayout.addView(twowayview, 0);
//0 is the pisition off the twowayview in the layout
TwoWayView listView = (TwoWayView) myLayout.getChildAt(0);
//listView.setItemMargin(1);
listView.setAdapter(adapter);
}
Twowayview View
<?xml version="1.0" encoding="utf-8"?>
<org.lucasr.twowayview.TwoWayView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/lvItems"
style="#style/TwoWayView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:background="#android:color/holo_green_light"
tools:context=".MainActivity" />
In the screenshot below is what the minimum spacing i am getting. But i want images to be very close to each other.
Please Note that the value in pixel not DPI, so you need to convert the value from DPI to Pixel before pass it to the setItemMargin
I've been researching how to do this for quite some time now and I can't find a single solution, it seems like no one has even tried this before. I'm trying to do the following:
This is how the marker looks like:
And this is how I want it to look like when the Info Window pops up:
Is there a way for the Info Window to overlap the marker like this and hide a part of it?
Note: The second image is just a model of the design (photoshopped), it is not yet implemented. The first image is the actual screenshot.
Here is your code to add custom marker images.
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
marker=inflater.inflate(R.layout.custom_marker_layout,null, false);
numTxt = (TextView) marker.findViewById(R.id.num_txt);
for(int i=0;i<locations.size();i++){
numTxt.setText(count.get(i).toString());
numTxt.setTextColor(Color.GREEN);
if(Integer.parseInt(count.get(i).toString())<=5){
numTxt.setTextColor(Color.RED);
}
String title=locations.get(i).toString()+"count"+count.get(i);
Currnt=mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(BasicMapActivity2.this, marker))).
position(new LatLng(Double.parseDouble(latitudes.get(i).toString()), Double.parseDouble(longitudes.get(i).toString())))
.title(title)
.snippet(address.get(i)));
markers.add(Currnt);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(latitudes.get(i).toString()), Double.parseDouble(longitudes.get(i).toString())), 12.0f));
}
and method to convert view into bitmap in google map.
// Convert a view to bitmap
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((BasicMapActivity2)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(200,65));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
and Custom_marker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" >
<ImageView
android:id="#+id/img"
android:layout_width="55dp"
android:layout_height="65dp"
android:src="#drawable/custom_marker" />
<TextView
android:id="#+id/num_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:textColor="#008000"
android:textSize="18dp"
android:textStyle="bold" />
</FrameLayout>
Here in this implementation i show custom image and in this image one textview with count value and when i click on marker then it's show my custom window.