Displaying an Image Android - java

I am developing an Fitness App. My app will have different exercises like Push Ups and Sit Ups. Every exercise need to have an image to show to the user.
I have been thinking a while on what is a good way to solve this problem. But I don't think my solution below is good. Have you worked with images on Android for displaying images? Give me your solution on how you did it.
An exercise have a name but also images. The purpose is to display a specific Exercise with the images and exercise name.
My Exercise class looks like this right now:
I have thought of having the path to the image saved which I can have access when I need to show the image. I am uploading the images on the assets folder.
public class Exercise {
private String exerciseName;
private String exerciseSmallImagePath;
private String exerciseLargeImagePath;
public Exercise(String exerciseName, String exerciseSmallImagePath, String exerciseLargeImagePath){
this.exerciseName = exerciseName;
this.exerciseSmallImagePath = exerciseSmallImagePath;
this.exerciseLargeImagePath = exerciseLargeImagePath;
}
}

saving the path to the image-source is definitly a good approach. Have a look at ImageViews in order to display your image. There are two approaches to implement such an ImageView:
1: define it in your XML and set the image-source afterwards in your oncreate-method:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Some description" />
2: define your ImageView programmatically in your Activity:
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(fetch your ID here);
someLinearLayout.addView(imageView);

I suppose you are familiar with how to create simple application in Android? If not, then you should get started with samples and reading up on Android developers guide
This article can give you good start in how to work with Images.
As for how you should accomplish this, I can only suggest one approach, the rest is upto your imagination.
Start with creating a fragment that has an image and a text below (or above if you like) it. You can then put this fragment where you want with new image and text.
Here is a rough idea
<LinearLayout (vertical orientation>
<Image ... />
<Text ... />
</LinearLayout>
Once the layout is in place you can set the image source at runtime.
A good way to display this information is to have steps which user can navigate using swiping (view pager).
Every page can have the above mentioned fragment that will show one step. This will result in cleaner, slide screen style guide.

Related

ImageButton image resize

I have the following problem:
I try to use Glide to add to my ImageButton a background, but if I try it, the image sometime very-very big, little, not in the right place, and often the real ImageButton sticking out from behind.
Like this picture: https://www.kephost.com/image/El2G
So, how can I resize, or place, to the button from behind doesn't look, and the whole picture can see in the upper screen.
My Codes:
.xml
<ImageButton
android:id="#+id/ib1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
.java
Glide.with(MainMenu.this)
.load(R.drawable.updatelogkisfelbontas)
.fitCenter()
.placeholder(R.drawable.updatelogkisfelbontas)
.into(ib1);
}
try this:
Glide.with(MainMenu.this)
.load(R.drawable.updatelogkisfelbontas)
.override(200, 200);
.centerCrop();
.placeholder(R.drawable.updatelogkisfelbontas)
.into(ib1);
}

Parsing blog post

Let's say I am making a blog application about news. Whole idea is getting blog posts and showing them in application. I am parsing posts from RSS feed.
Every blog post has some amount of text,some pictures and a youtube video. I want to show everything exactly like in website. But all I can do is showing only text in text view:
<TextView
android:id="#+id/postContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="18sp"
android:textColor="#color/postTextColor"/>
In post view activity:
final TextView mPostContent = (TextView) findViewById(R.id.postContent);
mPostContent.setText(Html.fromHtml(content).toString().replace("","").replace('\r', ' '));
This helps me to show text only. But as I mentioned blog post have more than one images. What should I do? Using webview is not a solution.
You can parse all the data using the RSS feeds and create a specific screen to display this parsed data the way you like.
It depends by the object format of your posts variable, maybe can it helps an adapter to a recyclerView and a CardView with the adapter class the with json or xml parser get the from the api response objects

How to set ImageView's tint under (Expandable)ListAdapter?

Preamble
I've been g00gling for half an hour now, and it seems like I'm really missing something important.
Note: I've already tried the solutions for these questions
How to change color of vector drawable path on button click
How to set tint for an image view programmatically in android?
Changing ImageView source
These solutions either recolor ALL ImageView srcs or they don't do anything at all (vector remains black).
My drawable is a vector asset from the material icons directory:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2z" />
</vector>
My ImageView:
<ImageView
android:id="#+id/gradeBullet"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="25.5dp"
android:layout_marginTop="25.5dp"
app:srcCompat="#drawable/importantgrade" />
I would like to do this with *Compat, because I'd also like to support earlier APIs (API>17).
My ImageView: ImageView gradeBullet = convertView.findViewById(R.id.gradeBullet);
Try I.
MyAmazingAdapter.java - getChildView(...)
gradeBullet.setColorFilter(ContextCompat.getColor(context, gradeObj.colorId), PorterDuff.Mode.MULTIPLY);
Result: Does nothing.
Try II.
MyAmazingAdapter.java - getChildView(...)
DrawableCompat.setTint(gradeBullet.getDrawable(), ContextCompat.getColor(context, R.color.myColor));
Result: Recolors everything.
Try III.
MyAmazingAdapter.java - getChildView(...)
VectorDrawableCompat drawable = (VectorDrawableCompat) gradeBullet.getDrawable();
drawable.setTint(ContextCompat.getColor(context, gradeObj.colorId));
gradeBullet.setImageDrawable(drawable);
Result: Gives a weird color. (Definitely not what I want, it's closer to the original color)
Try IV.
All the above with a final to the gradeBullet, got the same results.
Some other info
The gradeObj.colorId is different for every childView.
Using shapes and setColor instead of SVG is not what I want.
Currently only tested with API lvl 26
MyAmazingAdapter.java extends BaseExpandableListAdapter
If you'd like more info, just mention it in a comment.
You can try your first method with PorterDuff.Mode.SRC_IN or PorterDuff.Mode.SRC_ATOP instead of PorterDuff.Mode.MULTIPLY.
PorterDuff.mode
Hope this helps.

Setting margin on an ImageView

I'm trying to set the imageview's layout_marginTop to one value for different density/screen sizes. In my values-mdpi folder I have the following line in dimensions.xml
<dimen name="marginTop">10dp</dimen>
In the MainActivity
ImageView image = (ImageView) findViewById(R.id.s_image);
But there is no setmargin method for imageview. Is there a way to do this?
You don't need to do that in code, you can do it in your XML file where the image view is defined. See this page for more details.
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
You're on the right track. It's probably easiest to refer to your dimension value within the xml (rather than set this up in java code).
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="#dimen/yourMarginTopValue" />
Try try to put android:adjustViewBounds="true" to
It's probably better to do it in XML since you seem to already have the ImageView defined in XML.
However, the layout_* XML attributes refer to the LayoutParams of the parent layout, not the view itself. To change them in code, access them with getLayoutParams(), do your modifications and call requestLayout() to schedule a re-layout pass. For example:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)imageView.getLayoutParams();
lp.topMargin = 123;
imageView.requestLayout();

how to do haptic feedback when an ImageView is not working

I created an Activity displays an ImageView on the screen. I want get haptic feedback when the image is clicked.
In the main layout main.xml I added the next ImageView tag:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/dog"
android:onClick="doBark"
android:hapticFeedbackEnabled="true"/>
Then, in the Activity code I add this method:
public void doBark(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Log.d("BarkingDog", "is hapticFeedbackEnabled: " + v.isHapticFeedbackEnabled());
}
When I click on the image I can see that doBark() is called and the output of the Logcat says "is hapticFeedbackEnabled: true", but I can't feel anything. I've also tried with the other two HapticFeedback constants, and no luck.
I know that HapticFeedback is enabled because each time I press the menu button, the device vibrates.
Any ideas? Suggestions?
PS: I don't want to use the Vibrator object. By using it, I can make the device vibrate, but I don't think it's the right way to do it.
Take a look at this: http://groups.google.com/group/android-developers/browse_thread/thread/de588e3d15cb9055?pli=1
Do note that it is old though, but the last time I had to use haptic feedback, I followed what Dianne had to say here

Categories