im looking to have two buttons on the same layout, you click the first button, chose an image and that button changes to that image chosen. You click the second button and that image chosen will replace the button. Easiest to use an imageButton instead of ImageView.
I would like the code if possible, thank you.
(Still dont understand? The end should be 2 images next to each other chosen by the user.)
MainActivity:
package com.example.triptych;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageButton imageButton = (ImageButton) findViewById(R.id.buttonLoadPicture);
// Set the Image in ImageView after decoding the String
imageButton.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/buttonLoadPicture"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
<ImageButton
android:id="#+id/button2"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="180dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
</RelativeLayout>
Another question, do I create another activity for the second button and paste the same code or do I do it on the same activity?
<?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" >
<ImageButton
android:id="#+id/buttonLoadPicture"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
<ImageButton
android:id="#+id/button2"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="180dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
You can try something like this:
In your MainActivity class you need to create 2 different methods to handle gallery pick images , and call it in on click of two different image
buttons, see below code:
your MainActivity.java will look like this:
public class MainActivity extends ActionBarActivity {
private static int RESULT_LOAD_IMG = 1, RESULT_LOAD_IMG_TWO = 2;
String imgDecodableString, imgDecodableStringTwo;
ImageButton btn_load, btn_loadTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_load = (ImageButton) findViewById(R.id.buttonLoadPicture);
btn_loadTwo = (ImageButton) findViewById(R.id.buttonLoad);
btn_loadTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadImagefromGalleryTwo(btn_loadTwo);
}
});
btn_load.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadImagefromGallery(btn_load);
}
});
}
public void loadImagefromGallery(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
public void loadImagefromGalleryTwo(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG_TWO);
}
#SuppressLint("NewApi")
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(imgDecodableString));
btn_load.setBackground(d);
} else if (requestCode == RESULT_LOAD_IMG_TWO && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableStringTwo = cursor.getString(columnIndex);
cursor.close();
Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(imgDecodableStringTwo));
btn_loadTwo.setBackground(d);
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}
Output:
Hope it helps!
getContentResolver().query should not be called from UI thread
Related
How can I make that all the pictures from my gallery appear in one view in Android Studio? I need all of my pictures to use after face recognition on them.
Here is the begining of my code:
package com.face.user.faceapplication;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
//import static android.widget.ImageView.*;
public class FirstClass extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
// private ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
Button button = (Button) findViewById(R.id.button);
// ImageView image = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
private static int LOAD_IMAGE_RESULTS = 1;
#Override
//semmi
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
and 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: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=".MyActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Click here" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
but if I run this I can just pick one photo from my gallery and I need all of them in a view.
you need to use putExtra on intent like this:
// ImageView image = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
N.B. Works in API 18 and higher.
I am doing an app in which user selects an image from gallery and goes to second activity with the selected image from gallery and displays it in second activity.But it takes some time(approx 3 sec) to go to second activity after user clicks on an image in gallery.I want to display progress bar circle for that much of time after the user selects an image from gallery and want to make progress bar circle invisible when execution moves to second activity.I am not getting any idea how to do my task?Should I use any AsyncTask?Please help me.I am stuck here.
I am providing my sample code.
My first Activity is
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class LauncherActivity extends Activity
{
private static int RESULT_LOAD_IMAGE = 2;
ImageButton gallery;
Bitmap bitmap_for_gallery;
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);
gallery.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
ProgressDialog progress=new ProgressDialog(getApplicationContext());
progress.setIndeterminate(true);
progress.setTitle("Please wait");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
progress.dismiss();
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
intent.putExtra("path", picturePath);
startActivity(intent);
}
}
}
my first activity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/homepage"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:scaleType="fitXY"/>
<ImageButton
android:id="#+id/select_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="126dp"
android:background="#android:color/transparent"
android:src="#drawable/select_photo" />
</RelativeLayout>
my second activity is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
ImageView background;
Bitmap transfered;
FrameLayout.LayoutParams layoutParams;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background=(ImageView)findViewById(R.id.imageView1);
layoutParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
Bundle extras = getIntent().getExtras();
String picturePath=extras.getString("path");
transfered=BitmapFactory.decodeFile(picturePath);
background.setImageBitmap(transfered);
background.setAdjustViewBounds(true);
background.setLayoutParams(layoutParams);
}
}
My second activity 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.progressbarcircle.MainActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center" />
</FrameLayout>
Thanks in advance.please help me.
If your first Activity is taking too much time create a ProgressDialog object and show() it after
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
and dismiss() it before startActivity(intent);
You can do the same for the second Activity if it takes too long.
currently I'm developing an application which requires a browse button in my activity. When I press the browse button I should be able to browse the image files in my sdcard as well as in the phone memory. Upon tapping the image file it should be selected and the path of the image file (that is, where that selected image file is located) should be displayed in the activity in a textview. Also the selected image should be displayed in a imageview. How to do this? Can someone please help me out..
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></ImageView>
<Button
android:id="#+id/buttonLoadPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Load Picture"
android:layout_gravity="center"></Button>
</LinearLayout>
MainActivity.java
package com.example.yourpackage;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
Add Permission in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MediaStore.Images.Media.DATA is deprecated. So #Looking_Forward bro's code will not work on android 10+. But just with a simple modification, I worked #Looking_Forward bro's code. Process below:
After Uri selectedImage = data.getData(); line remove all code and just use Glide.with(this).load(selectedImage).into(imageView) (this is Glide library). Also it's much easier then use contentProvider (which is #Looking_Forward bro showed).
I'm still new here in android. Please help me. I manage to put the imageview to the left and right side of my Layout. My problem is when I select an image for imageview2 it passes the image to imageview1 and still the imageview2 can be seen from the right side.
I need to do is when I select an image for imageview2 it should be fixed at the right side. I think I have problems with my code in java?
Here is my code for xml.
<RelativeLayout 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: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" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/insert_bg"
>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip"
>
<ImageView
android:id="#+id/img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitStart"
android:src="#drawable/insert_ci" />
<ImageView
android:id="#+id/img_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:fitsSystemWindows="false"
android:scaleType="fitEnd"
android:src="#drawable/insert_ci"
android:textAlignment="viewEnd" />
</LinearLayout>
And this code for java.
package com.prototype;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.ImageButton;
import android.widget.ImageView;
//import android.widget.LinearLayout;
public class MainActivity3 extends Activity {
private static final int SELECT_PICTURE = 2;
private String selectedImagePath;
private String selectedImagePath2;
private ImageView img;
private ImageView img2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.open_project);
img = (ImageView)findViewById(R.id.img_left);
img2= (ImageView)findViewById(R.id.img_center);
addImageViewClickListener();
addImageView2ClickListener();
}
public void addImageViewClickListener()
{
ImageView btnNavigator1 = (ImageView)findViewById(R.id.img_left);
btnNavigator1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select character image for right side."), SELECT_PICTURE);
}
//#Override
//public void onClick(View v) {
// TODO Auto-generated method stub
//}
});
}
public void addImageView2ClickListener()
{
ImageView btnNavigator2 = (ImageView)findViewById(R.id.img_center);
btnNavigator2.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select character image for left side."), SELECT_PICTURE);
}
//#Override
//public void onClick(View v) {
// TODO Auto-generated method stub
//}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
//File folder = new File(Environment.getExternalStorageDirectory() + "/Database/");
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public void onActivityResult2(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
//File folder = new File(Environment.getExternalStorageDirectory() + "/Database/");
Uri selectedImageUri = data.getData();
selectedImagePath2 = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath2);
img2.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The problem that I was talking about is like this.http://imageshack.us/photo/my-images/31/6sa6.png/
printscreen image
Use this for two imageViews. So your images will not overlap
android:adjustViewBounds="true"
android:scaleType="fitXY
Try this one,
<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"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/img_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img_center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"
android:textAlignment="viewEnd" />
</LinearLayout>
The image is getting stretched because of the weight mentioned.
The android:layout_weight for the first imageView is 1 and that is for second imageView is 2.
This leads to taking 2/3rd of the screen by the the second imageView and the rest by the first.
Adjust the weights and you can overcome the issue.
Change your Imageview make 0dp to your android:layout_width#
<ImageView
android:id="#+id/img_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitStart"
android:src="#drawable/icon" />
<ImageView
android:id="#+id/img_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:fitsSystemWindows="false"
android:scaleType="fitEnd"
android:src="#drawable/icon"
android:textAlignment="viewEnd" />
Update
Create one more folder named layout-land and keep same xml inside with appropriate changes, hope it works.
I have a problem in Android development with camera. i try to take a pic and display it in to imageview but the result does not show but it works fine for Galaxy Note but i doesnt work for Galaxy S3
My Code is Bellow
Camera Activity
package com.nfs;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Camera extends Activity {
private static final int CAMERA_REQUEST = 500;
private ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
this.imageView = (ImageView) findViewById(R.id.imgLoad);
Button bt = (Button) findViewById(R.id.btCam);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
layout
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btCam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Click Here" />
<ImageView
android:id="#+id/imgLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.70"
android:src="#drawable/icon" />
</LinearLayout>
Hi try my Code which is working fine.I suppose this will solve your Problem.The link is as follows:-Imp Link.
Try This:
#Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
try {
try {
Uri selectedImage = imageReturnedIntent.getData();
bmpSelectedImage = getThumbnail(selectedImage);
set_img_camera.setImageBitmap(bmpSelectedImage);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
Log.d("image error", e.toString());
}
}
}
}