How to change ImageView source in android - java

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);

Related

Customize Google SignInButton In Android

I want to customise Google SignIn Button In Android. Currently I have a very basic default layout by using following code.
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I am not satisfied with the current button layout. Is it possible to update the button text, background colour in the default button, Or Should I create full custom button layout?
Thank you.
ps: I am new to Android
Its very simple , you need to do this
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/common_signin_button_text_long" />
You can customize your button , but you will have to make sure you follow the guidelines.
Edit: Check out this library custom signin button
If you want to do it yourself you can create a linear layout and add images.
I think you have to do it pragmatically rather than xml something like this
public static void customizeGooglePlusButton(SignInButton signInButton) {
for (int i = 0; i < signInButton.getChildCount(); i++)
{
View v = signInButton.getChildAt(i);
if (v instanceof TextView)
{
TextView tv = (TextView) v;
tv.setText("My Text");
tv.setAllCaps(true);
tv.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
//here you can customize what you want
return;
}
}
}
another way but not that secure
TextView textView = (TextView) signInButton.getChildAt(0);
//Customize here
Basically, Google SignInButton is a FrameLayout you can customize it
as you want but need some dynamic tweaks.
XML part
adding custom background to the frame layout, make your own.
<com.google.android.gms.common.SignInButton
android:id="#+id/google_signIn"
android:layout_width="match_parent"
android:background="#drawable/google_bottun_bg"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
// google image logo
<ImageView android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="12dp"
app:srcCompat="#drawable/ic_gmail"/>
</com.google.android.gms.common.SignInButton>
inside FrameLayout we have a textView so you can redesign text view as needed
fun reDesignGoogleButton(signInButton: SignInButton, buttonText: String) {
for (i in 0 until signInButton.childCount) {
val v = signInButton.getChildAt(i)
if (v is TextView) {
v.text = buttonText //setup your text here
v.setBackgroundResource(android.R.color.transparent) //setting transparent color that will hide google image and white background
v.setTextColor(resources.getColor(R.color.white)) // text color here
v.typeface = Typeface.DEFAULT_BOLD // even typeface
return
}
}
}
Happy Coding
see the results
No need any library. The nice trick is to create your fully custom layout then place google btn in this layout and set width and height match parent for Google Sign in Button, then set Visibility for Google Btn = 0 and then in
googleSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
googleLayoutCustom.setPressed(true);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
So Google Sign In btn will be on top of all layouts and your custom layout will perform touch ripple on google btn click.
P.S sure you have to implement touch ripple effect with any colors you want in your drawable. Example:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorMain">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>

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

OnClickListener display ImageView in Full screen in other Intent/Fragment

I am using fragment, I would like to display the imageView I've got in thumbnail on my Fragment in another view or Dialog ? or something for displaying :)
I would like when we tap on the ImageView, the new view displays, and when we tap on Button, that returns on the main Fragment.
I have implemented my onClickListener, and that works but I don't know how to pass data or whatever for displaying ImageView in full screen...
Here is my code for onClickListener :
mImageReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (zoomOut) {
zoomOut = false;
} else {
zoomOut = true;
}
}
});
No need to create new window for displaying full screen image. You can use android default gallery image viewer. Just set full image path or image URI to "mImageReport" as a tag, inside its onClick you can retrieve it. Use following line of code to display full screen image as
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
you can set full screen dialog to show image like below
final Dialog nagDialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
ivPreview.setBackgroundDrawable(dd);
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
xml code for layout containing image view and close button only:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/close"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />

Categories