Smaller down the image source inside imageview - android

So I have an image source inside the ImageView. I tried to smaller down the size of source by writing android:padding="25dp".
But when I captured the image and display the imageView, it become like this. The captured image does not fit into ImageView.
Is there a way I can smaller down the source in ImageView but allow the captured image fit into the ImageView ? Here my code:
<ImageView
android:layout_height="150dp"
android:layout_width="150dp"
android:background="#color/light_gray"
android:padding="25dp"
android:src="#drawable/camera"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/imageView" />
Is there a way to remove the captured image but not remove the source? I used code below but it remove everything including the source.
imageView.setImageDrawable(null);
Edit
Trying to place button in the center of imageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_below="#+id/toolbar"
android:layout_height="fill_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="42dp"
android:layout_marginEnd="42dp" />
<Button
android:layout_width="40dp"
android:background="#drawable/camera"
android:layout_height="40dp"
android:id="#+id/imageButtonOne"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/imageView1"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp" />
</RelativeLayout>

Use a Button to invoke the gallery or camera:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_below="#+id/toolbar"
android:layout_height="fill_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:id="#+id/imageView1"
android:layout_marginRight="42dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="40dp"
android:background="#mipmap/ic_launcher"
android:layout_height="40dp"
android:id="#+id/imageButtonOne"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/imageView1"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/your_delete_button_id"
android:background="#mipmap/ic_launcher"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignStart="#+id/imageView1"
android:layout_marginStart="35dp" />
</RelativeLayout>
Use code for that:
// buttons to access camera or gallery for input images
Button imageButtonOne = (Button) findViewById(R.id.imageButtonOne);
imageButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//invoke camera or gallery
}
});
// image upload camera button are visible at first
imageButtonOne.setVisibility(View.VISIBLE);
After image is selected and ready to be placed in ImageView, use this in your onActivityResult():
imageButtonOne.setVisibility(View.GONE);
Delete button to delete image only:
// image-delete button
ImageButton buttonDelete1 = (ImageButton) findViewById(R.id.your_delete_button_id);
buttonDelete1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (your_imageView != null) {
// delete image from your_imageView
your_imageView.setImageBitmap(null);
// enable your camera_button again
imageButtonOne.setVisibility(View.VISIBLE);
} else {
//say something
}
}
});

Related

Store image as value in Android Studio

This is a code when user choose one image and will run this coding (finish()), if choose wrong image then do not run the finish() .
but i dont know how to make the image as a value , how i select the drawable a or b or c and link to this function
if(eyeDected)
{
if(detectedFrame > 25)
{
eyeDected = false;
detectedFrame = 0;
finish();
} else {
detectedFrame++;
Log.d("UNLOCK:", String.valueOf(detectedFrame));
}
} else {
eyeDected = true;
detectedFrame++;
Log.d("UNLOCK:", String.valueOf(detectedFrame));
}
I want to ask about how to store the drawble/image into value?
i'm developing a lock screen application , when user choose the correct image will execute the unlock function
This is a layout showing 3 images and give user choose one images inside a lock screen.
In the setting , i'll let user to choose one image as the correct image that will execute the code
set_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/new_build_resize_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/setting">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set Image "
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="38dp"
android:textColor="#color/abc_primary_text_material_light" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select one graphical passcode as your lock screen."
android:id="#+id/textView2"
android:textSize="22dp"
android:textColor="#1547bb"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="135dp"
android:layout_height="140dp"
android:src="#drawable/a"
android:id="#+id/imageButton1"
android:layout_above="#+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2" />
<ImageButton
android:layout_width="126dp"
android:layout_height="126dp"
android:src="#drawable/b"
android:id="#+id/imageView2"
android:layout_above="#+id/imageView3"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="126dp"
android:layout_height="126dp"
android:src="#drawable/c"
android:id="#+id/imageView3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="46dp" />
</RelativeLayout>

Any Skin Selector Android library

Are there any android library that do:
Shows drawables in either a grid or scroll view.
Allows user to select one of the images and shows his selection.
Allows me to get the selected drawable and show it to the user.
Please see the screenshots to see what I'm trying to achieve.
I have done something like you are thinking about. I will show you that if is useful to you (I think will be, but you must adapt this):
Context: I have created a simple game based in Space Invaders. My game has a main menu (main activity) where the player can press 'PLAY' or scan elect some 'OPTIONS', though a popup.
App main menu image
1 screen: Main menu.
2 screen: Popup appears when I press 'OPTIONS' button. This popup let me choose graphic options (change game background, ally spaceship model and enemy spaship model).
MainActivity.java
optionBoton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
soundMenu.start();
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View viewPopup = inflater.inflate(R.layout.popup_activity, null);
popup = new PopupWindow(
viewPopup,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
popup.showAtLocation(layoutPrincipal, Gravity.BOTTOM, 0, 0);
ImageButton closePop = (ImageButton) vistaPopup.findViewById(R.id.volver_boton);
closePop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
soundMenu.start();
popup.dismiss();
optionBoton.setVisibility(View.VISIBLE);
}
});
optionBoton.setVisibility(View.INVISIBLE);
}
});
This code will be inside onCreate method.
Now, you need to design your own XML popup (if you decide to create a popup). Showing a popup is easy, clean and dynamic way to change the graphic options.
popup_activity.xml
<?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"
android:background="#839ceaac"
android:orientation="vertical">
<ImageButton
android:id="#+id/volver_boton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_marginTop="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:src="#drawable/wcerrar" />
<TextView
android:id="#+id/options_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:layout_marginTop="5dp"
android:text="Opciones gráficas"
android:textSize="20dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#id/options_title"
android:background="#9dc8a6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/options_title"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/back_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Fondo"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/back_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/back_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadBack"
android:scaleType="centerCrop"
android:src="#drawable/fondo" />
<ImageView
android:id="#+id/back_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/back_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadBack"
android:scaleType="centerCrop"
android:src="#drawable/fondo1" />
<ImageView
android:id="#+id/back_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/back_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadBack"
android:scaleType="centerCrop"
android:src="#drawable/fondo3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/spaceship_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Spaceship"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/spaceship_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/spaceship_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadSpaceship"
android:src="#drawable/diseno11" />
<ImageView
android:id="#+id/spaceship_2"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/spaceship_1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadSpaceship"
android:src="#drawable/diseno21" />
<ImageView
android:id="#+id/spaceship_3"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/spaceship_2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadSpaceship"
android:src="#drawable/diseno31" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/enemy_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:text="Enemies"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/enemy_1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#id/enemy_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:adjustViewBounds="true"
android:onClick="uploadEnemy"
android:rotation="180"
android:src="#drawable/enemigodiseno11" />
<ImageView
android:id="#+id/enemy_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_below="#id/enemy_1"
android:layout_marginBottom="3dp"
android:layout_centerHorizontal="true"
android:src="#drawable/enemigodiseno21"
android:onClick="uploadEnemy"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Anyway, if you choose to don't use a popup, I will show you how you can send some data to another activity in Android development:
When you press on a graphic option, look at the call "android:onclick". This allows to call a java method to capture the option selected.
Look a simple example of this:
Method example
public void uploadBack(View view) {
switch (view.getId()) {
case R.id.back_1:
result[0] = "back";
break;
case R.id.back_2:
result[0] = "back1";
break;
case R.id.back_3:
result[0] = "back3";
break;
}
}
As you can see, I have an array of Strings with 3 positions. When I click an ImageView, I call the method wich changes the result[i] value. When I press 'PLAY' and I call the game activity, I use putExtra method to send these options and change the game settings.
I hope this serve as a help for your problem.
Have a nice day!

How to change position of ImageButton in Android

How can I change the position of ImageButton when I click on it.
There are 6 ImageButton here: ibWolf1, ibWolf2, ibWolf3, ibSheep1, ibSheep2, ibSheep3; 1 ImageView ivWood and 1 Button btnGoAndBack. When I click ibSheep3, ibSheep3 will change position like this:
And when I continue to click on ibWolf3 it will change position like this:
That means the image will appear above the wood image. So how to do this?
Here is my game_play_layout.xml:
<?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">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Go"
android:textSize="30dp"
android:background="#3300FF00"
android:id="#+id/btnGoAndBack"
android:layout_below="#+id/ivWood"
android:layout_toLeftOf="#+id/ibSheep2"
android:layout_toStartOf="#+id/ibSheep2" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/wolf"
android:scaleType="centerCrop"
android:id="#+id/ibWolf1"
android:layout_above="#+id/ibSheep2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/wolf"
android:scaleType="centerCrop"
android:id="#+id/ibWolf2"
android:layout_below="#+id/ibWolf1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/wolf"
android:scaleType="centerCrop"
android:id="#+id/ibWolf3"
android:layout_below="#+id/ibWolf2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/sheep"
android:scaleType="centerCrop"
android:id="#+id/ibSheep1"
android:layout_above="#+id/ibWolf2"
android:layout_toLeftOf="#+id/ibWolf1"
android:layout_toStartOf="#+id/ibWolf1" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/sheep"
android:scaleType="centerCrop"
android:id="#+id/ibSheep2"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/ibWolf2"
android:layout_toStartOf="#+id/ibWolf2" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/sheep"
android:scaleType="centerCrop"
android:id="#+id/ibSheep3"
android:layout_below="#+id/ibSheep2"
android:layout_toLeftOf="#+id/ibWolf3"
android:layout_toStartOf="#+id/ibWolf3" />
<ImageView
android:layout_width="200dp"
android:layout_height="30dp"
android:src="#drawable/wood"
android:scaleType="centerCrop"
android:id="#+id/ivWood"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/ibSheep2"
android:layout_toStartOf="#+id/ibSheep2"/>
</RelativeLayout>
One easy solution is that,put two images of sheep and wolf above go button and at first make the visibility invisible or gone.Now when user clicks on the sheep or the wolf images make the visibility of the imagebutton gone and make the image visible of the image above go button.
Otherwise you have to calculate the position of the go button and create a imagebutton progmatically and place it over there.
Hope it clears your view.

how to join 4 images to one image button (android)

Hello I need to join 4 separate images into one ImageButton
I want to attach the images Which should look like that on one image button Thanks for you help.
How can I do it ?
Img1 on top
Img2 img3 at the middle
Img4 at bottom
Like a pyramid
You can do this with Button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/btn_back"
android:drawableLeft="#drawable/bd_logo"
android:drawableRight="#drawable/btn_close"
android:drawableBottom="#drawable/btn_ok"/>
Left Img:
yourButtonId.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
There is a nice article on merging 2 pictures.
Take a look, and then try to connect 4 of them
http://android-er.blogspot.dk/2013/08/merge-two-image-overlap-with-alpha.html
Actually you don't have to use an imageButton for that. You can create a new LinearLayout, and assign a setOnClickListener method to that LinearLayout. So that it behaves like a button and you can locate as many images the way you wanted. For example;
The XML code for LinearLayout;
<LinearLayout
android:id="#+id/imagebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Declaration in your Activity;
LinearLayout imageButton = (LinearLayout) findViewById(R.id.imagebutton);
Handling click events;
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO
}
});

Image button formatting

Above is my desired designed to build on my activity, in which I want to place the image of button just left to button text and want this on centered.
can anyone tell me how to make this thing???
Here is the code to divide buttons width to 50% 50%
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0" >
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight=".50"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/icon_settings"
android:text="#string/search" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:src="#drawable/menu_separator" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight=".50"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/icon_settings"
android:text="#string/profile" />
</LinearLayout>
To place the image as in your screenshot, use drawableStart instead of drawableLeft.
You can also use this code for imageButtons.`
<ImageButton android:layout_width="wrap_content"
android:src="#drawable/backicon" android:layout_height="wrap_content"
android:id="#+id/imageButton1" android:background="#drawable/backicondn"></ImageButton>
Change image by programe
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton imageButton;
imageButton = (ImageButton) findViewById(R.id.imageButton1);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
imageButton.setImageBitmap(bm);
}

Categories

Resources