I am currently involved in a project about hotels and restaurants. My database contains hotel IDs(1,2,3,...) and I need to load the images(image1,image2,...) of the respective hotels/restaurants for which I have to give the image src through .java file.The code that I used is :
String icon="image" + h_id;
int resID = getResources().getIdentifier(icon, "drawable","testing.Image_Demo");
image.setImageResource(resID);
But, the problem is that the hotel image is not loading. I had gone through different questions in this site but the problem remains as it is. Does anyone have an idea to solve this? Thanks in advance!
I created the following sample activity, which works fine if I have a "image.png" image file in the drawable folder:
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String icon = "image";
int resId = getResources().getIdentifier(icon, "drawable", getPackageName());
ImageView imgView = new ImageView(this);
imgView.setImageResource(resId);
setContentView(imgView);
}
}
Did you check that your package name is correct ?
Related
how can i open the html string that has a href on webview only instead of going on your browser?
i am using the LinkMovementMethod, this is okay on opening it on other app like video from youtube but i have some links that needs to be open only on webview.
here is a sample html string
String html_text = "<h1>sample text</h1><p><small>February 1 1970</small></p><p class=\"text-center\"><img src=\"https://www.google.com\" /></p><p>sample (here).</p>"
i need the a href to be open only on webview.
here is my code
DetailActivity
public class DetailActivity extends AppCompatActivity implements Html.ImageGetter {
private TextView newsContentTv;
private WebView newsWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_news);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
newsWebView = (WebView) findViewById(R.id.newsWebView);
setSupportActionBar(toolbar);
// add back arrow to toolbar
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
newsContentTv = (TextView) findViewById(R.id.textNewsContext);
Intent i = getIntent();
String news_content = i.getStringExtra("detail_content");
Spanned spanned = Html.fromHtml(news_content, this, null);
Spannable spannable = new SpannableString( Html.fromHtml(news_content) );
newsContentTv.setText(spanned);
newsContentTv.setMovementMethod(LinkMovementMethod.getInstance());
}
any help would be really appreciated.
// Create an unencoded HTML string
// then convert the unencoded HTML string into bytes, encode
// it with Base64, and load the data.
String html_text =
"<h1>sample text</h1><p><small>February 1 1970</small></p><p class=\"text-center\"><img src=\"https://www.google.com\" /></p><p>sample (here).</p>";
String encodedHtml = Base64.encodeToString(html_text.getBytes(),
Base64.NO_PADDING);
newsWebView.loadData(encodedHtml, "text/html", "base64");
I hope this will help you.
WebView webview = (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");
Your question states you need to open the clicked url in your webview instead of navigating to browser. I can think of two ways to do this.
First case, if its not necessary to have a Textview to display the clickable link then you can load your string into the WebView itself (as others mentioned) and set a WebViewClient (this part is important)
newsWebView.loadDataWithBaseURL(null, html_text, "text/html", "UTF-8", null);
newsWebView.setWebViewClient(new WebViewClient());
// add below line to support youtube videos etc.
newsWebView.getSettings().setJavaScriptEnabled(true);
newsWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
so that any click keeps the navigation in the same WebView. Without the client it will navigate to the browser. But you should note this will load and replace the content inside, so your original link will not be there on screen.
Second case, if you need the clicks to be from the TextView simply setting newsContentTv.setMovementMethod(LinkMovementMethod.getInstance()); won't work as by default it will make the normal intent causing it to navigate to browser or related apps, you need to remove that. Then, you have to create a custom class extending LinkMovementMethod and override the touch event to load the url clicked into your webview as shown below:
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static CustomLinkMovementMethod instance;
public static CustomLinkMovementMethod getInstance() {
if (instance == null)
instance = new CustomLinkMovementMethod();
return instance;
}
#Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
int x = (int) event.getX() - widget.getTotalPaddingLeft() + widget.getScrollX();
int y = (int) event.getY() - widget.getTotalPaddingTop() + widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] urlSpans = buffer.getSpans(off, off, URLSpan.class);
if (urlSpans.length != 0) {
// You can check log to see if you are getting the url on click.
Log.d("TAG", String.valueOf(urlSpans[0].getURL()));
/* If you've made this an inner class in your activity
you can directly load the url into your webview.
Else you will have to create listeners to get url in activity
on event trigger */
newsWebView.loadData(urlSpans[0].getURL());
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
}
use this on the TextView like
newsContentTv.setMovementMethod(CustomLinkMovementMethod.getInstance());
See if this works for you, hadn't expected the answer to become this long.
I'm relatively new to android and I'm working on a horizontal scroll view with drag and drop functionality. Right now everything is working perfectly fine I can drag the image and drop it in the drop zone with count of failed and successful drops. For now the image that is being dragged and its shadow looks the same. What I want is that when I drag an image the shadow that appears for that image is an image from drawables that I select.
Here is the code for shadow builder:
void dragAndDropImage(int imageId)
{
// int drawableid = getResources().getIdentifier("drawable/a1", "id", getPackageName());
final ImageView drag = (ImageView)findViewById(imageId);
drag.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);//I want the drawable id here View.DragShadowBuilder(drawableid)
v.startDrag(data, shadow, null, 0);
return false;
}
});
I want to get the id of one particular image from drawables and then I can use it in View.DragShadowBuilder(id of the image from drawables) to change the image of the shadow. Any help in this regard is appreciated.
It will be something like:
R.drawable.resourcename
Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).
If that doesn't work, you can always use a context's getResources method ...
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
Where this.context is intialised as an Activity, Service or any other Context subclass.
Update:
If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?
Update 2:
The Resources class has this method:
public int getIdentifier (String name, String defType, String defPackage)
Which returns the integer of the specified resource name, type & package.
(I have been seeing this post getting more views every time I log in and I solved this a long time ago but I'm going to post the solution in hopes that it helps someone else.)
In my case I had multiple images in Drawable that I wanted to use therefore
for (int j=1; j<10; j++)
{
setImages("drawable/img" +j);
// SetPopup(drag);
}
void setImages(final String draw)
{
int id12 = getResources().getIdentifier(draw, "id", "your_package_name");
drag1.setImageResource(id12);
I just posted the part of code that helped me access different images directly from drawables and use them as drag images on multiple imageViews.
String uri = "#drawable/yourdrawable";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Can you try this?
Try this,
final ImageView drag = (ImageView)findViewById(R.id.imageId);
drag.setImageResource(R.drawable.a1);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
Assume you have several drawables in your Android resource folder and what to iterate over them. You use a certain naming schema and would like to use this to determine the resources ID’s.
The following steps shows how to get a resource ID based on the resource name:
// Name of resource
//Type
// Package of the app
int identifier = getResources().getIdentifier("pic1", "drawable", "android.demo");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(identifier);
I need set image resource by Send parameter (String src)
I have this code
public void getSource(View view , String src){
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(src);
}
How do I solve this problem ؟
// method to call to set image
private void setImage(int src) {
ImageView iv = findViewById(R.id.your_image_view);
iv.setImageResource(src);
}
//Pass your resource and use it like this :-
setImage(R.drawable.plus_active);
If you want to set image from local storage, then you have to get the uri of the image and use setImageUri like this:
public void getSource(View view , String srcImageUri){
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageURI(Uri.parse(src));
}
If you have image in drawable folder , then you have to set image like this:
image.setImageResource(R.drawable.imageName);
And if you want to set image from web url, then you have to use libraries like picaso or fresco.
use getResources().getIdentifier(); (there you can pass the image name String as a parameter to getIdentifier() function) to get the resourceID of the named image and then use setImageResource(resourceID); to set the image to ImageView.
public void getSource(View view , String src){
int resID = getResources().getIdentifier( src, "drawable", Context.getPackageName());
ImageView image = (ImageView)findViewById(R.id.img);
image.setImageResource(resID);
}
you may need to pass the Context as a parameter to your getSource() function.
i am beginner in android programing, and i created a Full Screen Image Slider app using this tutorial:
Link:
http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/
in this tutorial it reads images from Sd Card but i want to read images from drawable folder, i don't want to access images from SD Card instead access images from drawable.
This is the way I've seen, I hope it is correct:
private Bitmap bitmap = null;
private void createBitmap(Context context)
{
Resources resources = context.getResources();
Drawable d = resources.getDrawable(R.drawable.imagename);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
this.bitmap = bitmap;
}
It can be done like this:
Drawable drawable = this.getResources().getDrawable(R.drawable.image);
where this refers to the context of the actual activity.
OR
you can just give the name of the image and then you can get the image using getIdentifier,
getResources().getIdentifier(name,"drawable", getPackageName());
Where name will be the name of your image i.e - "image1"
I used a tutorial that showed me how to load bitmaps in an async task and set them to an imageview and in the tutorial before the bitmap is loaded the imageview is set to black with this piece of code
static class DownloadedDrawable extends ColorDrawable {
private final WeakReference<DownloadImageTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(DownloadImageTask bitmapDownloaderTask) {
super(Color.BLACK);
bitmapDownloaderTaskReference =
new WeakReference<DownloadImageTask>(bitmapDownloaderTask);
}
public DownloadImageTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}
How can I change the imageview so that before it is loaded it is the spinner in the progress dialog.
Thanks in advance.
you can try aquery android library for lazy loading image and listview...below code may help you.....
AQuery aq = new AQuery(mContext);
aq.id(R.id.image1).image("http://data.whicdn.com/images/63995806/original.jpg");
You can download library from from this link