Image button formatting - android

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);
}

Related

Smaller down the image source inside imageview

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
}
}
});

How to change icon size in button android

I have a button which have both text and image. The image size it is taking as actual size of image. How can i change the size of image in button?
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:drawableTop="#drawable/home_icon"
android:text="Settings"
/>
Set
android:scaleType="fitXY"
Also wrap_content is the preferred Parameter for width and height so that the OS will automatically adjust depending upon the screen size.
Please refer to
Android - Image Button Scale
and
Adjusting the size of an ImageButton in Android
You should use a ImageButton and specify the image in android:src, and set android:scaletype to fitXY
Set Drawable in your code
Drawable drawable = getResources().getDrawable(R.drawable.icon);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.10),
(int)(drawable.getIntrinsicHeight()*0.10));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, sWidth,sHeight);
Button btn = findViewbyId(R.id.btnId);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); //set drawableLeft/Right for example
Control the size deterministically with specific measurements. For example:
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:layout_height="50dp"
android:layout_width="50dp"
android:background="#drawable/home_icon"
android:text="Settings"
/>
For even more control over the button's design use a container such as a RelativeLayout to hold the image and the text. Then assign an onClickListener to the container to turn it into a clickable button. Here's an example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/snazzy_button_with_image_above_text"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:background="#color/SkyBlue"
android:paddingBottom="2dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="1dp" >
<ImageView
android:id="#+id/your_image"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/white"
android:contentDescription="image inside button"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/your_text"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/your_image"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:background="#color/translucent_black"
android:gravity="bottom|center_horizontal"
android:paddingTop="0dp"
android:text="Your Button Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/white"
android:textSize="10sp" />
</RelativeLayout>
In the code assign the onClickListener as such:
yourButton = (RelativeLayout) findViewById(R.id.snazzy_button_with_image_above_text);
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View clickedButton) {
//button click action here
}
Set android:background="#drawable/image_name"
and give width and height like
android:layout_height="10dp"
android:layout_height="10dp"
or you can try using ImageButton instead of Button because ImageButton has more image rendering options than Button
You can refer here
Simple way: add next line to dimens.xml
This parameter change size all icons on buttons in AlertDialog
<resources>
<dimen name="abc_alert_dialog_button_dimen">24dp</dimen>
</resources>

Android XML Layout - a picture with a TextView on it

I'm working on an XML layout in my app and what I want to do is to make an ImageView have a piece of text on it (I want to have a TextView on it). Anyway, less words and more examples. What I want is something like this:
I would like to know what is the best way to make it (with an opaque strip and preferably with text that will stretch or shrink depending on the length of name. Though I think I might figure the stretching out by myself).
You can create a RelativeLayout with the ImageView and TextView as its two children
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="150dp"
android:layout_height="75dp"
android:src="#drawable/myImage" />
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/myImageView"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/myImageView"
android:background="#drawable/myBackground"
android:gravity="center" />
</RelativeLayout>
Building on what Chris Stillwell said, set the Alpha property to less than 1:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="150dp"
android:layout_height="75dp"
android:src="#drawable/myImage" />
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/myImageView"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/myImageView"
android:background="#drawable/myBackground"
android:gravity="center" />
</RelativeLayout>
Then in your onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
ImageView imageView = (ImageView)findViewById(R.id.myImageView);
TextView textView = (TextView)findViewById(R.id.myTextView);
textView.setText("Your Text");
textView.setAlpha(.6f);
}

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
}
});

Android: Image over image

I have a video thumbnail and when you click on it, the video starts playing in the YouTube player.
This works, but It's not clear that you have to click on the thumbnail to play, therefore, I want to draw a play button image over the thumbnail in the bottom right corner. How would I go about this?
I currently have the thumbnail in a Drawable object and the play button in my drawable resource directory.
I tried stuff with bitmaps and canvas but it's quite hard and I don't know if this is the way to go.
Use Relative or FrameLayout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/thumbnail"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
or
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/thumbnail"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_gravity="bottom|right"/>
</FrameLayout>
What about using a FrameLayout or a RelativeLayout to stack the views..
Here follows some psaudo code:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.yourpackage.VideoPlayer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></VideoPlayer>
<!-- Adjust the layout position of the button with gravity, margins etc... -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Play"
/>
</FrameLayout>
I used RelativeLayout and it worked for me
<RelativeLayout
android:id="#+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:clickable="true"
android:layout_gravity="top"
android:maxHeight="400dp"/>
<ImageView
android:id="#+id/play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_menu_play_large"
android:layout_centerInParent="true" />
</RelativeLayout>
This is Easiest Way i Found i think to merge morethan one images in one. this is very helpful if we want share merged file.
Resources r = getResources();
Drawable[] layers = new Drawable[3];
layers[0] = r.getDrawable(R.drawable.image3);
layers[1] = r.getDrawable(R.drawable.image1);
layers[2] = r.getDrawable(R.drawable.image2);
LayerDrawable layerDrawable= new LayerDrawable(layers);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// align image1 over image3
layerDrawable.setLayerGravity(1, Gravity.TOP);
layerDrawable.setLayerInsetTop(1, 250);
// align image2 over image3
layerDrawable.setLayerGravity(2, Gravity.BOTTOM);
layerDrawable.setLayerInsetBottom(2, 250);
}
// both image1 & 2 placed over image3 in layerDrawable.
imageView.setImageDrawable(layerDrawable);
Hope this will works

Categories

Resources