Tuesday, 6 November 2018

Picasso Android Tutorial – Android Picasso Image Loader Library

Hello programmers, today we will be learning about picasso library for image downloading developed by sqaure.
  • Picasso one of the most popular library for image downloading.
  • It makes our wok very simple so its powerful library for image downloading.
  • It uses caching technique.
  • I have already published a post on Glide(similar to picasso) Image Downloading library for android.


Why Picasso library?

Its obvious that you will think why we should use picasso library.
If we use the traditional method of downloading the image from a url then we need to manage a lots of thing.
 And of course lines of code and development time will also increase.
 But if we will use a 3rd party library like picasso then we will achieve our goal in few lines of code.  So if we will not use a 3rd party library then  we would need
  1. Very large amount of code to be written
  2. We have to write another logic to implement caching.
  3. Caching is very important to make the application faster.
  4. We also have to deal with memory while writing the code.
  5. If any error occurs we need to handle that also.
  6. we will not focusing more on the application feature that is going to be developed. 
So, picasso will handle these all by default and overcome these all problems.

Adding Picasso Library to our Android Project

Adding picasso android library to your project is very easy.
Add the following line in the dependency block of your build.gradle file(app /module level).
I am assuming that you are using Android Studio.
After adding it just sync your project.

Loading Image from URL by Using Picasso Android Library

Loading image from URL by using Picasso Android Library is very simple and easy. The first thing we would need is an ImageView

Code for Loading Image with Picasso

It is very simple. We have to use the Picasso class.

 Picasso.get()  
 .load(IMAGE_URL)  
 .into(imageView);  


Placeholder and Error Handling

  • Because we are loading the image from internet.
  • the process would take some time depending on the internet speed. 
  • So it would be a good idea to display a image from the device while the image from URL is getting loaded.
  • One more situation could be when the image is not downloaded from the URL (when the URL given is wrong). 
  • In this case we should display an error image.
  • Both these things can be done very easily by using picasso.
  • See the following code snippet.

  Picasso.get()
   .load("YOUR IMAGE URL HERE")  
   .placeholder(Your Drawable Resource) // optional the image to display while the url image is downloading  
   .error(Your Drawable Resource) //this is also optional if some error has occurred in downloading the image                                  //this image would be displayed  
   .into(imageView);  

Re-sizing and Rotating

We can also resize and rotate the image very easily.


 Picasso.with(this)  
    .load("YOUR IMAGE URL HERE")      
    .placeholder(DRAWABLE RESOURCE)  // optional      
    .error(DRAWABLE RESOURCE)   // optional      
    .resize(width, height)            // optional      
    .rotate(degree)               // optional      
    .into(imageView);  


Using Picasso Android Library in Our Project

Now lets try the above codes in our Android Studio Project. So I will be creating a new Android Project.

  • Open Android Studio and create a new project.
  • I have created PicassoExample. 
  • First we have to add the Picasso Library. 
  • So open your build.gradle(app level) and add the following line inside dependency block and sync your project.
  • As we will load the image from a URL so we will also need internet permission. 
  • So open AndroidManifest.xml and add internet permission.
 <?xml version="1.0" encoding="utf-8"?>  
 <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:orientation="vertical"  
   android:layout_height="match_parent"  
   tools:context=".MainActivity">  
   <ImageView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerInParent="true"  
     android:src="@mipmap/ic_launcher_round"  
     android:id="@+id/imageView"  
     />  
   <Button  
     android:layout_width="match_parent"  
     android:id="@+id/btn"  
     android:layout_below="@+id/imageView"  
     android:layout_centerHorizontal="true"  
     android:text="show image"  
     android:layout_margin="10dp"  
     android:layout_height="wrap_content" />  
 </RelativeLayout>  


  • Now come inside MainActivity.java and define your ImageView and a button.

MainActivity.java 

 package protector.video.com.picasoexample;  
 import android.os.Bundle;  
 import android.support.v7.app.AppCompatActivity;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.ImageView;  
 import com.squareup.picasso.Picasso;  
 public class MainActivity extends AppCompatActivity {  
   private ImageView imageView;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     Button btn=findViewById(R.id.btn);  
     imageView=findViewById(R.id.imageView);  
     //setting click listener to the button
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         Picasso.get()  
             .load("https://avatars0.githubusercontent.com/u/1?v=4")  
             .placeholder(R.drawable.ic_image_black_24dp) // optional the image to display while the url image is downloading  
             .error(R.drawable.ic_error_black_24dp) //this is also optional if some error has occurred in downloading the image                 //this image would be displayed  
             .into(imageView);  
       }  
     });  
   }  
 }  
  • We need two images one for placeholder and one for error. I will be using the following images. Just save these and paste inside the drawable folder of your project.
  • Now we need a URL to an ImageFile. I have this URL https://avatars0.githubusercontent.com/u/1?v=4


Finally run your application. 
Picasso Android 
Happy coding......
Thank You

Monday, 5 November 2018

Glide - Image Download library for android

What is Glide?

  • Glide is an open source media management framework for Android.
  • Control media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface
  • Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs.
  • Glide includes a flexible API that allows developers to plug in to almost any network stack 
  • Glide’s primary focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.

Glide Unique Features

  1. Animated GIF decoding : you can load GIF animation into ImageView.
  2. Supports Thumbnail
  3. Supports OkHttp and Volley library & retrofilt library
  4. Placeholder can be added before loading the original image.
  5. Any custom image can be shown if error occurs while loading the original image.

Alternatives of Glide 

  •  Picasso  

Why Image loader library? 

  • To show an image into ImageView from a url we need to download that image
  • downloading the complete image from remote location, handle network errors if occurs while downloading. 
  • Implement image caching.
To download an image one should do a lot of things.
This will increase the lines of code and of course development time.
So its better to use the library to reduce our work and focus more on the development idea.

Steps to use Glide?

step 1.

 Add dependency for Glide into app/build.gradle.
dependencies {

implementation 'com.github.bumptech.glide:glide:4.8.0
} 

Step 2.

 Add INTERNET permission in manifest file 

 <uses-permission android:name="android.permission.INTERNET" /> 

Step 3

 Add ImageView in you layout

Declare an ImageView in your layout to display image from remote server in it
 <ImageView  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:id="@+id/imageView">  
 </ImageView>

Step 4 

 Add ImageView in you layout


 Glide.with(this)  
     .load("IMAGE URL HERE")  
     .placeholder(R.drawable.placeholder)  
     .error(R.drawable.imagenotfound)  
     .into(imageView);



MainActivity.java

 public class MainActivity extends AppCompatActivity {  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     final String url="https://avatars0.githubusercontent.com/u/1?v=4";  
     //Glide  
     Button btn=findViewById(R.id.btn);  
     final ImageView imageView=findViewById(R.id.img);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         Glide.with(MainActivity.this).load(url).into(imageView);  
       }  
     });  
   }  
 }  

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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:orientation="vertical"  
   android:layout_height="match_parent"  
   tools:context=".MainActivity">  
   <ImageView  
     android:layout_width="wrap_content"  
     android:id="@+id/img"  
     android:src="@mipmap/ic_launcher"  
     android:layout_height="wrap_content" />  
   <Button  
     android:layout_width="wrap_content"  
     android:text="Download image and show"  
     android:id="@+id/btn"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  

Tuesday, 26 September 2017

how to generate colorful output in c language

To generate colorful output on console in c language we are going to use a predefined function of c language that is textcolor().

Now ,in this post we will learn how to use this textcolor funtion to generate colorful output on console.

declaration of textcolor -

void textcolor(int color);

here we have to send the integral value of the corresponding  color that we want to use .

Color Numerical Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15

here are more colors than this. The colors available depend on the installed graphics drivers and current mode. Colors must be written in all Upper Case
because these colors are predefined macros.



                        note :we must use cpreintf function instead printf.

 #include<conio.h>  
 #include<stdio.h>  
 int main()  
 {  
         textcolor(RED);// the color is set to red  
         cprintf("I am Red");///the output will be in red color  
         textcolor(GREEN);// the color is set to green  
         cprintf("I am green");///the output will be in green color  
         textcolor(BLUE);// the color is set to blue  
         cprintf("I am blue");///the output will be in blue color  
         textcolor(CYAN);// the color is set to cyan  
         cprintf("I am Cyan");///the output will be in Cyan color  
         textcolor(YELLOW);// the color is set to yellow  
         cprintf("I am Yellow");///the output will be in yellow color  
        return 0;  
 }  

Friday, 1 September 2017

CONSTANT EXPRESSION REQUIRED-ERROR IN C PROGRAMMING

CONSTANT EXPRESSION REQUIRED-ERROR IN C PROGRAMMING 




The above program has error 'constant expression required' that means in between the [ ] constant yielding expression is needed or constant of type integer is needed.
While declaring an array the compiler must know how much memory will be reserved for how many elements of the array. So in C language at the time of declaration of an array we must provide a meaningful size of the array and the number of elements of any array can not be non- integral.
The size of the array must be integer because the number of elements can not be in fraction they can only be in integral numbers and cant be float or double.
               On the other context ,if we want to give the size of the array at the time of execution then we have to deal with a concept known as dynamic memory allocation and we need to use some predefined functions as calloc() ,malloc() and realloc().

Thursday, 31 August 2017

TOO MANY TYPES IN DECLARATION - ERRORS IN C PROGRAMMING

TOO MANY TYPES IN DECLARATION - ERROR IN C PROGRAMMING 

Compile Time Error

Too many types in declaration is a common error that occurs when we try to declare a function or variable of multiple types that is not allowed in C programming .As for example
int char a;
then this statement results in this type of error and exactly this situation is happening with the program in the above image. Here compiler gets confused that what is the exact data type of a;

In the above program in image, structure is defined that is a user defined data type thats why the compiler is condensed about the return type of main() function.Compiler is considering the structure as a data type so there are two data types there the first one is int and the second one is structure.And this ambiguity results in this error. And the second error is also explaining the same thing that incompatible types. So,when we put a semicolon before int and after the structure definition the error will be gone.
I hope you enjoyed this post...stay connected I will be posting  such interesting posts.

Other posts:
 how to insert image in C/C++ language
Introduction to Errors in C programming
C program for Bisection method

Wednesday, 30 August 2017

INTRODUCTION TO ERRORS AND THEIR TYPES IN BRIEF

INTRODUCTION TO ERRORS AND THEIR TYPES IN BRIEF

While writing programs we have to take care of many things and whenever we miss any thing required this creates a situation that prevent the program execution.
In simple words any problem generated while developing a program is known as an Error.Technically errors are known as bugs in computer science.
Basically there are three types of errors in c programming:
Runtime Errors
Compile Errors
Logical Errors

C Runtime Errors

C runtime errors are those errors that occur during the execution of a c program and generally occur due to some illegal operation performed in the program. Examples of some illegal operations that may produce runtime errors are:

Dividing a number by zero
Trying to open a file which is not created
Lack of free memory space

It should be noted that occurrence of these errors may stop program execution, thus to encounter this, a program should be written such that it is able to handle such unexpected errors and rather than terminating unexpectedly, it should be able to continue operating. This ability of the program is known as robustness and the code used to make a program robust is known as guard code as it guards program from terminating abruptly due to occurrence of execution errors.

Compile Errors


Compile errors are those errors that occur at the time of compilation of the program. C compile errors may be further classified as:

Syntax Errors

When the rules of the c programming language are not followed, the compiler will show syntax errors. For example, consider the statement,
int a,b:
The above statement will produce syntax error as the statement is terminated with : rather than ;

Semantic Errors

Semantic errors are reported by the compiler when the statements written in the c program are not meaningful to the compiler. For example, consider the statement,
b+c=a;
In the above statement we are trying to assign value of a in the value obtained by summation of b and c which has no meaning in c. The correct statement will be
a=b+c;
Logical Errors

Logical errors are the errors in the output of the program. The presence of logical errors leads to undesired or incorrect output and are caused due to error in the logic applied in the program to produce the desired output. Also, logical errors could not be detected by the compiler, and thus, programmers has to check the entire coding of a C program line by line.

Tuesday, 29 August 2017

C program for Bisection method

C program to implement bisection method in easy way..

bisection method theory

Here we have used a term tolerance value .Tolerance value is the difference between two consecutive

approximate roots of the equation that can be neglected.
As for example 1.00008 and 1.00004 are two approximate consecutive roots .

.One of them can be answer if there will be tolerance value of 0.00004 or greater otherwise not.
Here we have used a function fabs that returns the absolute double value and also takes double value 
as an argument.

 #include<conio.h>  
 #include<stdio.h>  
 #include<math.h>  
 float F(float x)  
 {  
   return x*x*x-x-1;  
 }  
 /*  
 f(0)=-ve  
 f(1)=-ve  here a=1 f(a)  
 f(2)=+ve  and b=2  
 tolerance value:0.0005  
 */  
 int main()  
 {  
   float a,b,c,d;  
   int i=0;  
   printf("Enter the interval(a,b) in which the roots of the equation lies::");  
   scanf("%f %f",&a,&b);  
   printf("\nEnter the tolerance value::");  
   scanf("%f",&d);  
   do{  
       c=(a+b)/2;  
       ++i;  
       if(F(a)*F(c)<0)//f(a)<0  
       {  b=c;  
         printf("\nValue of a and b in %d iteration is %f and %f",i,a,b);  
       }  
       else  
       {  
         a=c;  
         printf("\nValue of a and b in %d iteration is %f and %f",i,a,b);  
       }  
   }while(fabs(a-b)>d||F(c)==0);  
   printf("\nRoot is %f",c);  
   printf("\nPress any key to exit");  
   getche();  
   return 0;  
 }  
                                               


How to implement Facebook audience network banner ad in android app using Android Studio

Its very simple to implement Facebook audience network banner ad in android app using Android Studio. We will complete it in 4 Steps. 1. ...