• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Highlight the selected item background after click the backbutton

krishnaveni

Well-Known Member
Dec 16, 2011
158
0
chennai
I have to develop one android application.

Here is the scenario:There are many images in a linearlayout. When selected, the layout should be displayed with another(gradient_bg_hover.xml) background. This works well now.

Now, I would like that when I open a new activity and come back to this one, the last selected layout should still be the highlighted one (with the (gradient_bg_hover.xml) background).

How can this be done ???

Now i have used below code for highlighting the image when pressed:

[HIGH]
LinearLayout ar = new LinearLayout(this);
ar.setOrientation(LinearLayout.VERTICAL);
ar.setPadding(3, 3, 3, 3);
ar.setLayoutParams(artiLayoutParams);
ar.setGravity(Gravity.CENTER);
ar.setBackgroundColor(Color.parseColor("#666666"));
ar.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_selector));
ar.setId(position);
position++;
ar.setOnClickListener(mArticleClick);

LinearLayout ly = new LinearLayout(this);
ly.setOrientation(LinearLayout.VERTICAL);
ly.setBackgroundColor(Color.parseColor("#666666"));
ly.setLayoutParams(articleLayoutParams);
ly.setPadding(1, 1, 1, 1);


ImageView im = new ImageView(this);
im.setPadding(2, 0, 2, 0);
im.setLayoutParams(imageLayoutParams);
imageViewsHashMap.put(Appscontent.Sub_arraylistimagesub.get(j));
im.setBackgroundColor(Color.parseColor("#302c2d"));

[/HIGH]


list_selector.xml:

[HIGH]
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item

android:state_pressed="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />

</selector>

[/HIGH]

In OnClick function i have wrote the below code:

[HIGH]
private OnClickListener mArticleClick = new OnClickListener()
{

@Override
public void onClick ( View v )
{
int object = v.getId();
v.setSelected(true);

String articletitle = Appscontent.Sub_arraylist.get(object).toString();
Intent in = new Intent(MainActivity.this, SubCate.class);
in.putExtra("Title", articletitle);
startActivity(in);
}
};

[/HIGH]

Here i have to clicked one item means it is go to next activity.afterthat i have clicked back button means the selected item is stay on highlighted with another background.afterthat i have selected another item means its go to next activity.now i have to click back button means these item only highlight with background....but the pervious item also highlighted....

I wish to need the o/p like :

The last selected item only highlighted after click the back button...please help me...how can i do ..

So i have used sharedpreferences :

I have declared int prevPosition = -1; globally...

[HIGH]
ar = new LinearLayout(this);
ar.setOrientation(LinearLayout.VERTICAL);
ar.setPadding(3, 3, 3, 3);
ar.setLayoutParams(artiLayoutParams);
ar.setGravity(Gravity.CENTER);
ar.setBackgroundColor(Color.parseColor("#666666"));
ar.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_selector));
ar.setId(position);
position++;
ar.setOnClickListener(mArticleClick);
SharedPreferences preferences = getSharedPreferences(SPF_NAME, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("POSITION", prevPosition);
editor.commit();

[/HIGH]

In Onclick method:

[HIGH]
private OnClickListener mArticleClick = new OnClickListener()
{

@Override
public void onClick ( View v )
{

int object = v.getId();

SharedPreferences preferences = getSharedPreferences(SPF_NAME, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("POSITION", -1);
editor.commit();

[/HIGH]

But my background color is not staying after press the back button...

pls give me any ideas...
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones