I have set these properties of an ImageView
<ImageView
android:id="#+id/image_view"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHorizontal_bias="0.0"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
app:layout_constraintBottom_toBottomOf="parent"
android:scaleType="centerCrop"
/>
Now from Java code, I want to remove scaleType property form this image view. I dont want any scale type to be on this ImageView.
Is it possible to achieve this?
If You Not Set Any Scale Type Then For Image View Default Scale Type is FIT_CENTER
//So to remove other scale types and set to default use
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
This is how you set or change the Image View scale type at runtime from within Java:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setScaleType(ScaleType.CENTER);
You can refer to this documentation page for the possible ScaleType options.
Related
I have to put the text over the image, I want to get the following final result:
I list all images from my database and add them dynamically to my Flexbox.
But I need to do this by code, dynamically.
You can use this:
<RelativeLayout>
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Default text"
android:textColor="#000000" />
And dynamically change Text in this image using:
TextView myAwesomeTextView = (TextView)findViewById(R.id.myImageViewText);
myAwesomeTextView.setText("New Text inside Image");
You can use Canvas to draw the Image and draw the text at the bottom o canvas. Check Developer Google
I am absolutly new in Android development and I am findig the following problem working on my first app.
My problem is related to changing the space occupied by an image into my layout.
So I have a situation like this: I don't put the image directly into my XML layout file but I do it programatically into my activity code calling this method:
ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);
difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(), ImgUtility.createRankingImg(context, 3)));
So the ImgUtility.createRankingImg(context, 3) return a Bitmap object that is setted into the ImgeView of my layout having difficultyContainer as ID.
It works fine and the image is showed into my layout, this one:
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scaleType="fitXY" />
<TextView
android:id="#android:id/text1"
style="#style/pastaTitleTextStyle" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="#style/HeaderTextStyle"
android:text="Difficoltà :" />
<ImageView
android:id="#+id/difficultyContainer"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
The image is correctly showed into this ImageView:
<ImageView
android:id="#+id/difficultyContainer"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:scaleType="fitXY" />
The only problem is that doing in this way the image horizontally occupy all the space of the view, this is what I obtained (the image is the last one that contains the chef hats):
It should depends by the fact that I am using android:layout_width="fill_parent" to say that the ImageView having id=difficultyContainer have to horizontally occupy all the spac of its container (the main LinearLayout).
So I want know how can I set a percentual width\height for this ImageView.
What is the best way to specify a percentage so my image will be shown smaller in my app? What is the best solution to do it?
Are you trying to ask how to set the size of the imageView programmatically?
If so use:
ImageView imageView = findViewById(R.id.difficultyContainer);
imageView.getLayoutParams().width = 120;
The same for height, just replace width with height.
This value of '120' being in dp, which means the size of the image will adjust depending on the screen size of the output device, meaning you don't need to utilise percentages like you would in html/css
Just add padding to your ImageView
<ImageView
android:id="#+id/difficultyContainer"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:padding="8dp"
android:scaleType="fitXY" />
I am trying to add an image to a button(image button or normal button) but not in a specific res/drawable folder.
I want this image as a ImageView object or drawable object etc, but I couldn't.
You can set an icon for Button by adding following attribute in XML for Button like this:
android:drawableLeft="#drawable/button_icon"
If you want to know how to do it programmatically, follow this article:
How to programmatically set drawableLeft on Android button?
Or just add 'background':
<Button
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/qrcode"/>
But i think it's better if you use ImageButton, see this example:
<ImageButton
android:id="#+id/searchImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/your_image_drawable" />
Use ImageButton and add src in your background:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/yourImageNameHere"
I am creating an app that needs to dynamically generate an XML file. The layout starts essentially empty (other than a linear layout), then I loop through a JSON array and create the XML code shown below for EACH element in the array (the only thing different for each element in the array is the ids for the views).
I am not really understanding how I can use Java to create these layouts, edit their attributes, and add views to them.
I know I can create a GridLayout object and give it the number of rows and columns with
GridLayout doc = new GridLayout(3, 3);
But I can't figure out how to edit all of the specific attributes, and then add views inside of the layout.
What is the best way to create a layout, edit it's attributes, and add views within that layout through Jave code?
Thank you.
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="3"
android:layout_marginBottom="10dp"
android:background="#3c37ff00"
android:id="#+id/doctor1"
android:longClickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dr. Sam"
android:id="#+id/doctor1_name"
android:layout_row="0"
android:layout_column="0"
android:textStyle="bold"
android:textSize="26dp"
android:typeface="sans" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/doctor1_profile"
android:layout_row="0"
android:layout_column="1"
android:src="#drawable/no_pic"
android:scaleType="fitXY"
android:layout_rowSpan="3"
android:layout_columnSpan="1"
android:layout_gravity="right"
android:background="#00ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/doctor1_action"
android:layout_row="0"
android:layout_column="2"
android:src="#drawable/abc_ic_menu_paste_mtrl_am_alpha"
android:scaleType="centerInside"
android:layout_rowSpan="3"
android:layout_columnSpan="1"
android:background="#47ffffff"
android:clickable="true"
android:onClick="setContentView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Very Close"
android:id="#+id/doctor1_distance"
android:layout_row="1"
android:layout_column="0"
android:textStyle="bold" />
</GridLayout>
You could add your views in any viewGroup by:
YourViewGroup.addView(yourChildView);
But I think ListView will serve you better because:
You don't have to worry about each view's handling;
You will have automatic scroll;
You can simply manage your data via Adapter.
EDIT:
As of most recent APIs, you should use a RecyclerView for various reasons. It works similar to the previous adapters and you can accomplish any list/grid behavior you want.
You should start with a single xml document and a parent layout. Then you can give that an id and call it dynamically. You can add to this layout whatever you want. Heres some code I had from doing it. My parent linear layout is my_ll:
LinearLayout ll = (LinearLayout) myInflatedView.findViewById(R.id.my_ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
// Use this for each image in the list
final ImageView image = new ImageView(getActivity());
image.setAdjustViewBounds(true); // Scales it to the screen
image.setId(i); // Sets each with unique id
ll.addView(image, params); // Adds the ImageView to screen BEFORE adding image (important)
Picasso.with(getActivity()) // THEN you add the image from photosList
.load(photosList.get(i))
.into(image);
You can add onClickListeners to each view also dynamically. The addView is the part that will add it dynamically.
So I'm loading images from a web service, but the size of the images are sometimes smaller or bigger than other images and the visualization looks silly when I put them in a ListView in android. I'd like to fix the size of my ImageView so that it only shows a portion of the image if it's larger than a preset amount. I've tried everything I can think of setting the setMaxWidth/setMaxHeight, setting the scale type to centerCrop, using ClipableDrawable wrapping my BitmapDrawable, setting using Drawable.setBounds(). I've tried setting in the XML and programmatically, but neither worked. I'm very surprised setting max width/height didn't do anything. Below is the XML I'm using ImageView definition in my layout file:
<ImageView android:id="#+id/recipeImage"
android:maxHeight="64px"
android:maxWidth="64px"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"/>
But,nothing has worked.
Just delete the maxHeight and maxWidth attributes and enter your units in layout_width and layout_height appropriately. But do not use the px unit - use dp instead, because these are device-independent.
This will fit the image in the specified height and width, irrespective of image size.
<ImageView
android:id="#+id/image5"
android:layout_width="300dp"
android:layout_height="200dp"
android:src="#drawable/image_1"
android:scaleType="fitXY"/>
Get ImageVIews as facebook posts.
Glide.with(context).load(link)
.placeholder(R.drawable.aboutlogo)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);`
<ImageView
android:id="#+id/img1"
android:src="#drawable/ic_menu_camera"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"/>