Rash
Rash

Reputation: 896

Viewing larger image on onClick of ListView Item

I have an ListView in which I am having wallpapers listed. I want to open large image onClick of List item and want to set it as wallpaper. How to do it? Here is my code:

abc.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/aa" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="22px" >
    </TextView>

</LinearLayout>

abc1.java

package com.example.wallpaper;
import com.mkyong.android.adaptor.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class abc1 extends ListActivity {

    static final String[] WALLPAPER_IMG = 
               new String[] { "Wallpaper1", "Wallpaper2", "Wallpaper3", "Wallpaper4","Wallpaper5","Wallpaper6","Wallpaper7","Wallpaper8"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new MobileArrayAdapter(this, WALLPAPER_IMG));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
            //get selected items
            String selectedValue = (String) getListAdapter().getItem(position);
            Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}

MobileArrayAdapter.java

package com.mkyong.android.adaptor;

import com.example.wallpaper.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.abc, values);
        this.context = context;
        this.values = values;
    }
    int onscreen;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.abc, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);

        // Change icon based on name
        String s = values[position];

        System.out.println(s);

        if (s.equals("Wallpaper1")) {
            imageView.setImageResource(R.drawable.a);
            onscreen = R.drawable.a;
        } else if (s.equals("Wallpaper2")) {
            imageView.setImageResource(R.drawable.b);
            onscreen = R.drawable.b;
        } else if (s.equals("Wallpaper3")) {
            imageView.setImageResource(R.drawable.c);
            onscreen = R.drawable.c;
        } else if (s.equals("Wallpaper4")) {
            imageView.setImageResource(R.drawable.d);
            onscreen = R.drawable.d;
        }else if (s.equals("Wallpaper5")) {
            imageView.setImageResource(R.drawable.e);
            onscreen = R.drawable.e;
        }else if (s.equals("Wallpaper6")) {
            imageView.setImageResource(R.drawable.f);
            onscreen = R.drawable.f;
        }else if (s.equals("Wallpaper7")) {
            imageView.setImageResource(R.drawable.g);
            onscreen = R.drawable.g;
        }else{

            imageView.setImageResource(R.drawable.h);
            onscreen = R.drawable.h;
        }

        return rowView;
    }


}

Upvotes: 0

Views: 1588

Answers (1)

Gan
Gan

Reputation: 1399

You can go about this in a couple of ways.

1) Use the android native gallery to open the image and set the image as wallpaper through the implicit functions available there (by selecting the menu and setting it as the wallpaper). Inorder to do so you have to send an intent which is described here.

2) The other way is to create your own imageView to display the image and then set the image as the wallpaper. You can set the wallpaper as described here.

Upvotes: 1

Related Questions