set background resource on another Layout.xml and Activity

3.3k views Asked by At

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

1

There are 1 answers

9
Blo On

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.