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
Maybe you should create a
SharedPreference
to set the lastImageView
clicked to the new background. And when your application opens, you should take the Preference like this: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
, setOnClickListener
:The other way
In your
Activity
, after set the adapter to yourGridView
, you can make anOnItemClickListener
:To edit your preference:
Finally, to get it:
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 callYourActivity.thecontextvariable
.Hope this helps.