Imageview element disappers on selecting the link - java

I have a XML file with a imageview as follows. It also has a TextView anmed "linksend".
<ImageView
android:id="#+id/imgview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
I have a Java file as as follows
ImageView imageView=(ImageView)rootView.findViewById(R.id.imgview1);
URL url;
url = new URL(someurlforimage);
Bitmap bmp;
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
TextView tv=(TextView)rootView.findViewById(R.id.linksend);
tv.append(Html.fromHtml("" +"Press link"+""));
tv.setMovementMethod(LinkMovementMethod.getInstance());
The image disappears from the imageview when the html link is pressed. Though the html page opens in a separate browser, the app's image becomes empty.

Related

How to remove a ImageView inside of LinearLayout

In my app, I select photos from gallery and insert them inside of LinearLayout programmatically, so I did this :
<LinearLayout
android:orientation="horizontal"
android:id="#+id/linearImages"
android:layout_width="wrap_content"
android:layout_height="150dp">
<Button
android:layout_gravity="center"
android:id="#+id/add_btn"
android:gravity="center"
android:drawableLeft="#drawable/add_icon"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And i'm set a tag to which photo that I'm putting :
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageBitmap(bitmap);
img.setTag(tagCount);
linearImages.addView(img);
bitmapArray.add(bitmap);
tagCount++;
}
And I want to remove a Image by tapping, as I said before, the Images are added programmatically, so I need something to remove images one by one without have a static Image position.
I would suggest to use RecyclerView instaed of LinearLayout. The reason being you can easily get the position of the item clicked and can remove accordingly. If you want to go with your own solution, then I would suggest to add setOclickListener on each ImageView being added. In the listener after the click event get the imagView.getTag(), which is the position of the image view within LinearLayout. You can then remove the image view from the LinearLayout by using:
ll.removeViewAt(position);// to remove view from particular position
Or if you want to remove ImageView being clicked directly then:
ll.removeView(view)// to remove particular view
Update your code of AddNewImages It will work.
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageBitmap(bitmap);
img.setTag(tagCount);
linearImages.addView(img);
bitmapArray.add(bitmap);
tagCount++;
img.setOnClickListener(clickListner);
View.OnClickListener clickListner=new View.OnClickListener() {
#Override
public void onClick(View v) {
linearImages.removeView(v);
}
};
}

LayoutParams using AddRules

I know there are related questions but please try my code 1st
I think my code is correct but I don't know why I can't get want I want
I'm trying to Replace the image into a RelativeLayout with the same LayoutParams dimension and placement(programmatically) then inside that RelativeLayout put a WebView in the center of it.
It was almost done but I'm having trouble for the placement of Webview.
see the images below
activity_main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
// Image
ImageView image = (ImageView)findViewById(R.id.image);
// Container of the image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl_imageView);
loader(rl , image);
}
public void loader(RelativeLayout rl,View view){
// Creating RelativeLayout
RelativeLayout rlnew = new RelativeLayout(this);
// Getting the Layout Parameters of the image such as (position and dimension)
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
// Coloring the background of the new Relative Layout into blue
rlnew.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
// passing the parameters to the new relative layout from the image
rlnew.setLayoutParams(lp1);
// image turns into invisible
view.setVisibility(View.INVISIBLE);
// Creating a webView
WebView webView = new WebView(this);
// load the loading.gif
webView.loadDataWithBaseURL("file:///android_asset/", "<center><img src='loading.gif' style=''/></center>", "text/html", "utf-8", null);
// setting up dimension(height and weight) for the webview
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 60);
// adding position(Center) for the webview
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// setting up Layout parameters for the webiview
webView.setLayoutParams(lp);
// adding the webview to the new Relative Layout
rlnew.addView(webView);
// adding the new relative layout into the container
rl.addView(rlnew);
}
activity_main xml
<RelativeLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#ec0c0c"
android:id="#+id/rl_imageView"
android:layout_marginTop="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<!--Desired Output-->
<!--<RelativeLayout-->
<!--android:layout_width="70dp"-->
<!--android:layout_height="70dp"-->
<!--android:background="#010e6e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true">-->
<!--<WebView-->
<!--android:layout_width="60dp"-->
<!--android:layout_height="30dp"-->
<!--android:id="#+id/imageView"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true" />-->
<!--</RelativeLayout>-->
<!--Default Image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
</RelativeLayout>
Output Images
I'm making a method that will ask for two parameters a RelativeLayout(that contains the second parameter of the method) and View so it can be anything like(Image, TextView, RelativeLayout , etc.,).
This code can replace a specific View like a relativeLayout or textView or Image
and replace it with a relativeLayout with the same size and position of the given View(mine was the image)(It was already Done if you move that image anywhere in the given container the code will replace the image into a relativeLayout no matter where it is as long inside of the given container).
The only problem is the WebView position. . why is webView there?. .I want it in the center of the new RelativeLayout
Can someone tell where did I go wrong
If you have another way just submit answer
Your code is correct, but your xml is not. Try to change your xml below and test again:
<ImageView
android:layout_width="your dimen in dp < rl_imageView. width"
android:layout_height="your dimen in dp < rl_imageView. height"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
P/S: I suggest create this Imageview programmatically also.

ImageView from url not show in real device

imageview on emulator is show but in real device is not, i following tutorial from androidhive
myjava.java
url_get_photo = "http://www.myurl.com/"
String key = getIntent().getStringExtra("key");
int loader = R.drawable.photo_blank;
ImageView image = (ImageView) findViewById(R.id.img_photo);
String image_url = (url_get_photo + key + ".jpg");
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, loader, image);
myactivity.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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/img_photo"
android:layout_width="150dp"
android:layout_height="150dp" />
.......
i don't know why. What is going wrong? Thanks in advance.
Check the URL of your image and Internet Permission.
check your code
int loader = R.drawable.loader;
// Imageview to show
ImageView image = (ImageView) findViewById(R.id.image);
// Image url
String image_url = "http://api.androidhive.info/images/sample.jpg";
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url, loader, image);
I recommend you use a library like Picasso, Glide, Fresco, ImageRequest(Volley) that has many features can facilitate you this task.
Picasso.with(context).load("http://www.tecnologia.net/wp-content/uploads/2015/05/Los-mejores-trucos-para-Android.png").into(yourImageView);
Glide.with(context).load("http://www.tecnologia.net/wp-content/uploads/2015/05/Los-mejores-trucos-para-Android.png").into(yourImageView);
do not forget to import the library in your proyect.
Look this link http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

How to change ImageView source in android

This is my xml, this is located inside a fragment that appears in my activity.
<FrameLayout
android:id="#+id/frame1"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_margin="2dp"
android:layout_weight="0.33">
<ImageView
android:id="#+id/whoamiwith"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/default_image" />
</FrameLayout>
And this is my java code :
#Override
public void onClick(View click) {
if (click == profileBtn) {
whoamiwith.setBackgroundResource(R.drawable.image_i_wanna_put);
}
}
I am trying to change the image source of the image view. There are no syntax errors but when I click the button the emulator is giving me a force close and on the logcat it says:
java.lang.NullPointerException
It's pointing to the line:
whoamiwith.setBackgroundResource(R.drawable.loginbtn);
whoamiwith.setImageResource(R.drawable.loginbtn);
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
Drawable new_image= getResources().getDrawable(R.drawable.loginbtn);
whoamiwith.setBackgroundDrawable(new_image);
Just try
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
whoamiwith.setImageResource(R.id.new_image);
Initialize image view :
whoamiwith = findViewByid(R.id.whoamiwith);
Then on Click method, write this lines to change the image resource :
if(android.os.Build.VERSION.SDK_INT > 15)
{
// for API above 15
whoamiwith.setBackground(getResources().getDrawable(R.drawable.loginbtn));
}
else
{
// for API below 15
whoamiwith.setBackgroundDrawable(getResources().getDrawable(R.drawable.loginbtn));
}
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith);
whoamiwith.setImageResource(R.drawable.image_i_wanna_put);
Exception is whoamiwith is null.
did u initialise whoamiwith like,
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
Refer Using setImageDrawable dynamically to set image in an ImageView
In Kotlin, we can change ImageView programmatically like this.
val imageView: ImageView = findViewById(R.id.imageView)
imageView.setImageResource(R.drawable.imagename)
And in Java,
ImageView imageView= (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.imagename);

How to set ActionBar logo to Text (TextView)?

I've been trying to make the ActionBar logo, or the Up button, a text instead of an image. actionbar.setLogo(); accepts only a drawable resource. Inflating a layout with a TextView didn't do the trick.
Basically, what I'm trying to accomplish is this:
The first one is from the new DeskClock app from Android 4.2 and the second is from the Contacts app. I checked the code in GitHub but I couldn't find anything.
Off the cuff:
Option #1: Draw your text (TextView or directly) on a Bitmap-backed Canvas, and then supply a BitmapDrawable to setLogo().
Option #2: Hide the icon and logo and use setCustomView() to have your caret image and your text.
Here is an implementation of #CommonsWare's option #1, works perfectly.
TextView upTextView = (TextView) getLayoutInflater().inflate(
R.layout.up_text, null);
upTextView.setText("CITIES");
upTextView.measure(0, 0);
upTextView.layout(0, 0, upTextView.getMeasuredWidth(),
upTextView.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(upTextView.getMeasuredWidth(),
upTextView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
upTextView.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
R.layout.up_text:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#f00"
android:textSize="20sp"
android:textStyle="bold" />

Categories