1

How to set onClick on imageview to set backgroundresource on another layout.xml Even though I close the app and then open it again the backgroundresource on another layout.xml is the imageview I click back then ?

this is my code that content the image i want to be the backgroundsource

public class Main2Activity extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

public Main2Activity(Context context) {
    inflater = LayoutInflater.from(context);

    items.add(new Item("Cindvia",      R.drawable.cindvia));
    items.add(new Item("Harugon",      R.drawable.harugon));
    items.add(new Item("Melodist",     R.drawable.melodist));
    items.add(new Item("Nabilaholic",  R.drawable.nabilaholic));
    items.add(new Item("Nat",          R.drawable.nat));
    items.add(new Item("Rona",         R.drawable.rona));
    items.add(new Item("Shanju",       R.drawable.shanju));
    items.add(new Item("Shinta",       R.drawable.shinta_n));
    items.add(new Item("Ve",       R.drawable.ve));
    items.add(new Item("Vienny",       R.drawable.vienny));
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return items.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if(v == null) {
        v = inflater.inflate(R.layout.activity_main, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView)v.getTag(R.id.picture);
    name = (TextView)v.getTag(R.id.text);

    Item item = (Item)getItem(i);

    picture.setImageResource(item.drawableId);
    name.setText(item.name);

    return v;
}

private class Item {
    final String name;
    final int drawableId;

    Item(String name, int drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
}

}

and this is my .XML code that i want to change the background

<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"
tools:context=".Main2Activity"
android:background="@drawable/nat" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:shadowColor="#ffb4b4"
    android:shadowDx="2"
    android:shadowDy="2"
    android:shadowRadius="3"
    android:text="@string/eee_dd_mm_yyyy"
    android:textColor="#ffffff"
    android:textSize="17sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:shadowColor="#ffb4b4"
    android:shadowDx="2"
    android:shadowDy="2"
    android:shadowRadius="4"
    android:text="@string/hh_mm_ss"
    android:textColor="#ffffff"
    android:textSize="37sp" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="40sp"
    android:layout_height="40sp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="17dp"
    android:layout_marginRight="17dp"
    android:src="@drawable/info"
    android:contentDescription="@string/todo"/>

its a link to download all my workspace i mean code that i'm using,

if you want to download click file and download

https://drive.google.com/file/d/0B7NPhjxHR4zbT2NaUDBRNnU4cWc/edit?usp=sharing

Azza Nadhif
  • 13
  • 1
  • 6

1 Answers1

1

Maybe you should create a SharedPreference to set the last ImageView clicked to the new background. And when your application opens, you should take the Preference like this:

SharedPreferences myPrefs = this.
                         getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
drawableid = myPrefs.getInt("backgroundResourceId", defaultvalue); 
yourlayout.setBackground(drawableid);  

See this simple example from Google Documentation: Storage Options
See this answer: Android: make background image changable
And read also this: Changing background colour with SharedPreference in Android

More information:
How to use SharedPreferences in Android to store, fetch and edit values
SharedPreferences Documentation Android


Example
To create a SharedPreference, you can follow this tutorial, simple and easy to understand:
Android User Session Management using Shared Preferences
And this one: Android SharedPreferences Example


The way to do it
I'm not sure what you have to do exactly, but it will be something like this:
In your Adapter, set OnClickListener:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    // It will be something like this, not sure:
    picture.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {  
            // This is a Toast to see the position of the selected item.  
            Toast.makeText(getApplicationContext(), "You clicked on the image position: " + i,Toast.LENGTH_LONG).show();  
            // so get the Id with this position  
            int idToBackground = getItemId(position);
            // put in SharedPreferences
            SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);  
            SharedPreferences.Editor editor = sp.edit();  
            editor.putInt("NewBackground", idToBackground);  
            editor.commit();
        } 

    });

    return v;
}  

The other way
In your Activity, after set the adapter to your GridView, you can make an OnItemClickListener:

gridView.setAdapter(adapter);    
gridView.setOnItemClickListener(new OnItemClickListener() {  
    @Override   
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {  
        // do some stuff with the selected image  

        // get the id  
        int  idImage = v.getItemId(position); 

        // then put in SharedPref  
        SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);  
        SharedPreferences.Editor editor = sp.edit();  
        editor.putInt("NewBackground", idImage);  
        editor.commit();  
    }  
});  

To edit your preference:

SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("NewBackground", yourDrawableValue);
editor.commit();

Finally, to get it:

SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
int myNewIntValue = sp.getInt("NewBackground", 0);

EDIT:

To have the context, you have these solutions:
By Activity, using ActivityName.this
By Application, using getApplicationContext()
By View, using YourView.getContext()

If you need something universal, have a public static thecontextvariable in your activity and assign the application context. With this, you can always call YourActivity.thecontextvariable.
Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • i'm really newbie a it where i create that sharedPreference ? – Azza Nadhif Dec 10 '13 at 10:54
  • 1
    Create it in getview() method of your adapter and when you are changing image just save it in preferences.And when app start just check that preference if its not empty take the image and set it again. – keshav Dec 10 '13 at 11:10
  • See the first two questions in **bold**. I think it's exactly what you must to do. @keshav says right, these are the follow steps! – Blo Dec 10 '13 at 11:46
  • but how to set that click listener on my imageview ? – Azza Nadhif Dec 10 '13 at 13:55
  • I updated my answer and I added a link to Google Documentation on Storage Options. It's interesting to read it. – Blo Dec 10 '13 at 14:28
  • this is my problem The method getContext() is undefined for the type new View.OnClickListener(){} yourDrawableValue cannot be resolved to a variable – Azza Nadhif Dec 11 '13 at 05:10
  • Try `getApplicationContext()` instead of `getContext()` in your `Adapter`. See my update. – Blo Dec 11 '13 at 05:29
  • i found this problem The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){}) – Azza Nadhif Dec 11 '13 at 12:23
  • Try this: http://stackoverflow.com/a/7577868/2668136 - http://stackoverflow.com/a/3402658/2668136 | Do some researches when you got an error, this helps a lot to understand what happens in your code ;) – Blo Dec 11 '13 at 12:35