I am trying to display an image in android.The process is i am downloading 1 image from net and trying to display it in the screen,the problem i am facing is in the activity screen i cant able to set the content view and the image.Following is the code:
main.xml:
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Activity Screen:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);---->Here i cant set as main
Bitmap bitmap = DownloadImage(
"http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.); //here i cant set the image id
img.setImageBitmap(bitmap);
}
Use Android Smart Image View. That is the easiest way to display an image using a URL. Here is the link.
Clean your project.sometimes it don't take R.java file.
Just clean the project using Project -> Clean. If it still exists, run the project, and they will usually disappear.
Related
I am showing one image view that showing a svg map. my requirement is to get particular svg path click event. i don't know what is the process or any idea about this scenario
this is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<ImageView
android:id="#+id/richPathView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_map" />
</HorizontalScrollView>
</RelativeLayout>
this is my svg map
please help me how should i get click event on click of svg path.
Any help would be highly appreciated.
I found this (RichPath) amazing library which allow me to detect path click
class
richPathView.setOnPathClickListener { richPath ->
if (richPath.name != null) {
when {
richPath.name.toLowerCase() == "m" -> {
// my task
}
richPath.name.toLowerCase() == "e" -> {
// my task
}
}
}
XML
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:overScrollMode="never"
android:layout_centerVertical="true">
<com.richpath.RichPathView
android:id="#+id/richPathView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:vector="#drawable/ic_map" />
</HorizontalScrollView>
don't know this is valid right soluction or not but it's working.
You can't. There is no way to determine which path in a VectorDrawable has been clicked on. At least no easy way.
The only way I know of right now is to load the SVG into a WebView. And then call a method in your Android app from Javascript.
Call Java function from JavaScript over Android WebView
i want to change color of my imageView.
pasted the code below:-
firstly i pasted the footer.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#f1eeee"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/fHome"
android:background="#drawable/colorchanged"
android:src="#drawable/home" /> <!-- your image here -->
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/fAttendence"
android:src="#drawable/att" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/fTarget"
android:src="#drawable/target" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/fReport"
android:src="#drawable/report" />
</LinearLayout>
pasted .png which i used in this file
pasted .png which i used in this file
when i clicked on the imageView i want to set Blue color on it.
can anyone help me for this?
You can use a ColorFilter like below to be triggered on view click:
yourImageView.setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.MULTIPLY);
Note that this will actually change the state of yourImageView instance so, you'll need a variable to keep track of that.
A way to do this is to edit through photoshop those two .png.
The currently "grey" versions could have for example the name
home_button_unselected.png
Edit this image and change from grey to blue or the color you want, save it as
home_button_selected.png
Import them to your project, so now you have those two files. (for each image)
1) Set the default state, i guess that could be "home_button_unselected.png" through xml like this :
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/fHome"
android:src="#drawable/home_button_unselected" />
Also in an image view you shouldn't have android:background and android:src working together. If you want to know the difference between those two you could google it.
2) Then in your .java file let's say MainActivity.java you have to put an on click listener to that button, that means make it do something when the user clicks.
Declare your button(ImageView) as global variable ( before onCreate() )
private ImageView mHomeButton;
Find the View (in your onCreate() )
mHomeButton = (ImageView) findViewById(R.id.fHome);
fHome is the id of the imageView if you look at the xml above -> "android:id="#+id/fHome"
Set the OnClickListener so when user presses the button the image it changes from "home_button_unselected" to "home_button_selected". (inside onCreate() )
.
mHomeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mHomeButton.setImageResource(R.Drawable.home_button_selected); // setting the image to the selected one ( which is blue/selected)
}
});
If you have other buttons you should check first which is selected and if another button is selected, then all other buttons should change to the unselected png
I have a large .png and i want to show it in a Splash screen, but the image is not showing. 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.infaplic.lpi.activities.SplashActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"/>
And the code of the Activity:
public class SplashActivity extends Activity {
ImageView imagen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imagen=(ImageView)findViewById(R.id.imageView2);
imagen.setImageDrawable(getResources().getDrawable(R.drawable.pantalla_arranque));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, NextActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, 1000);
}
}
This way, the image is not showing. I have tried with android:src: in the xml, but it doesn't work.
The image is very large. Do I need to resize it before putting it in the ImageView? If not, why is the image not showing?
Thank you.
EDIT: The PNG is 1080x1920
In my case I was using a image with file name like "go-back-filename.png". And xml file is like
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/go-back-icon"/>
After i changed the file name to "go_back_icon" it worked fine.
Hope this helps someone.
Since you write that your image is very large I would guess it is too large. I have a bug in one of my apps there I generate an image which is bigger then 2048 pixels. Some devices does not support such big images I would guess that you run into a similar problem.
If that is some background image reduce the image size, a background image is not so important that each pixel on the screen has to own a image pixel.
Add Below this. Please read Options for scaling the bounds of an image to the bounds of this view.ImageView.ScaleType in your ImageView
Use This
imagen.setImageResource(R.drawable.pantalla_arranque);
Then
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
android:adjustViewBounds : Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Must be a boolean value, either "true" or "false".
Please Check demo ImageView scaleType Samples . I hope it helps you
Change your ImageView to this :
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:scaleType="fitXY"
android:adjustViewBounds="false"/>
If it's not working try to add this on your manifest (inside your Activity) android:hardwareAccelerated="false"
EDIT
I was looking for the answer, but I thought that better try it by my self... and I've created a sample project where I use an ImageView 1080x1920 and I was testing since I've found that I've I put 3000 miliseconds it shows up... wiht 1000 miliseconds won't show up if you don't rescale it.
By the way getDrawable() is deprecated I recomend to you use :
imagen.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.imagenprueva));
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView2"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/imagen"
/>
</RelativeLayout>
Here's the full source code hope it helps Source code
In your .xml file
add
xmlns:app="http://schemas.android.com/apk/res-auto"
then in the ImageView add this line of code:
app:srcCompat="#drawable/books"
NOT
tools:srcCompat="#drawable/books"
in instagram app, when image is not available it says
"tap to retry"
and when image is available it loads image.
my app needs something like that, if information is available it loads information onto the linear layout, if not available it returns nothing so my layout remains blank.
so how do i add the text like instagram like " no information available".
I tried using
android:hint
but it doesnt seem to be a good option.
Not sure how you have anything set up, but I would think something like this would work. Have your ImageView and TextView fill the same space, then toggle which one is visible and which one isn't based on the presence of the image. Hope this helps
Layout:
<RelativeLayout
... >
<ImageView
...
/>
<TextView
...
/>
</RelativeLayout>
Logic:
if(isImageAvail){
iv.setVisibility(View.VISIBLE);
tv.setVisibility(View.GONE);
}else{
iv.setVisibility(View.GONE);
tv.setVisibility(View.VISIBLE);
}
So you'd have your drawable...
Drawable imageDrawable = codeToGetYourImage;
TextView errorTxt = (TextView)findViewById(R.id.your_text_id);
ImageView imageView = (ImageView)findViewById(R.id.your_image_id);
if(imageDrawable == null){
imageView.setBackgroundDrawable(imageDrawable);
} else {
imageView.setVisibility(View.GONE);
errorTxt.setVisibility(View.VISIBLE);
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/your_text_id"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorry, image cannot be displayed"
android:id="#+id/your_image_id"/>
</LinearLayout>
I'd like to make a Gallery in android. In order to set the adapter in need to get the gallery which I defined in my xml file. Im doing that as:
Gallery g = (Gallery) findViewById(R.id.gallery1);
But with this code I can't compile my project, since I get the Error "Cannot cast from View to Gallery". My xml-file looks like this:
"
<?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"
>
<Gallery android:id="#+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
/>
</LinearLayout>
Anybody knows how to solve my problem? Thanks a lot
Your code looks fine.
Check if you have the correct import for Gallery
import android.widget.Gallery;
I think you just created the Object instance for Gallery. Gallery is a AdapterView. So you must set Adapter for the Content. please check this sample