Showing posts with label Android Tutorial. Show all posts

Saturday, 21 January 2017

How to Display Image From URL in ImageView in Android Studio

To Display Image From Url in ImageView is Complicated for Beginners, so We will show the easiest method to display images.

Step 1: Create a new Project in Android Studio and add ImageView Widget in your activity
Step 2: Add Picasso Library to our android project.
compile "com.squareup.picasso:picasso:2.4.0"
Step 3: Add Internet permission to Manifest File . so open  app=>manifest=>androidManifest.xml
and add the following lines

      
<uses-permission 
android:name="android.permission.INTERNET"
></uses-permission>
    
Step 3: Now open MainActivity.java
And add the following code

package in.flipmobi.myapplication;
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.ImageView;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {
    ImageView img;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);

        img = (ImageView) findViewById(R.id.imageView);        Picasso.with(this).load("http://www.justano.com/upload/photos/2017/01/WdX2aoRZRGBDDOvfKAd8_16_bf7c61a9de2ff18994e0c2e8185a7238_image.jpg").into(img);

    }
}


Step 4: Now the ActivityMain.xml Looks like

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp" />
</RelativeLayout>


We Can Also Resize and rotate image And do more with this library


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

Wednesday, 18 January 2017

How to Use ImageView in Android Studio

In Android, We can use  android.widget.ImageView  class to display image.
1. Add image to Resource 
 Put image file to res/drawable folder
Now remember your image file name, if your filename is android3.png, then image ID is android3.



2. Add ImageView to Activity.
Create a activity res/layout/activity_main.xml 
And just add an ImageView 


<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <imageview android:id="@+id/imageView" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_margintop="47dp" android:layout_width="match_parent"></imageview> </relativelayout>
3. Now Third Step is to edit Java file So

 open MainActivity.java

Create Reference and object of ImageView.

so We just created ImageView img;

and last step is to set your resource id using this method

 img.setImageResource("R.dawable.android3");

Now to Full code of MainActivity.java is
package in.flipmobi.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img= (ImageView) findViewById(R.id.imageView); img.setImageResource(R.drawable.a); }
}

Finish

Comment below if you have problem