highlight the linearlayout background in android - android

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 background. This works well now.
Now, I would like that when I open a new activity and come back tho this one, the last selected layout should still be the highlighted one (with the second background).
How can this be done ???
Now i have used below code for highlighting the image when pressed:
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);
EDIT:
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);
}
};
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 ..
EDIT:
I have declared int prevPosition = -1; globally...
Afterthat i have save the value here:
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();
In Onclick method:
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();
But my background color is not staying here after press the back button...

This is "easy". In order to be able to know which image should be highlighted, you need to store this information somewere. A good practice is a database, but if you have only this info to store, you can create something like a shared preferance or some other types of global variables. Then, when you will re-open the activity with your listview (or come back to it), you should tell to your listview which item should be highlighted and highlight this item.

Related

Saving state of a specific like Button in recycler view

In recycler view I have pictures with "Like" buttons on them. When I click a "Like" button for a specific picture, its color changes to red. But when I reopen the app, All "Like" buttons are red, even the ones which I didn't clicked.
I'm using sharedPreferences to save the state of the button.
In onBindViewHolder:
myPref = context.getSharedPreferences("MINE", Context.MODE_PRIVATE);
myEditor = myPref.edit();
myEditor.putBoolean("isChecked", false);
holder.likeBtn.setChecked(myPref.getBoolean("isChecked", false));
holder.likeBtn.setOnCheckStateChangeListener((view, checked) -> {
if (checked) {
myEditor.putBoolean("isChecked", true);
myEditor.apply();
} else {
myEditor.putBoolean("isChecked", false);
myEditor.apply();
}
}
So, here how can save clicked state of a specific like button. How can I get the position of a button that I want to click.

Programatically make LinearLayout dismissible in android studio using shared preference

There is this thing I want to achieve, I have searched all questions on SO and no answer related to this, so I guess no one has asked which makes me curious.
On my XML, Android studio app, I added a Linear Layout, under the Linear layout, I added a 《TextView with text "Dismiss"》.
I want to set an onclick listener to this textview, once user clicks this TextView with text "Dismiss" I don't want that user to see that Linear Layout again on the page until they reinstall the app.
Logically, the Linear Layout will be like a notice to highlight something on my app when users arrive at that page, but instead of that notice to stay there forever I want user to be able to dismiss it which shows they have gotten the notice.
My workings:
Since I want each app user to decide not to see that text again after clicking dismiss, I need to use shared preference on that TextView with text "Dismiss", so whenever app gets the value of this shared preference then it will hide that layout.
I can set the LinearLayout to be invisible if that value from shared preference is found when stored on the app
The question now, how will I set this shared preference and which code will I use to make the Layout "GONE" or "INVISIBLE" if the TextView with text "Dismiss" is clicked?
Your intelligent solution to this will be much appreciated.
check this out , its in Java, and this will hide the layout after clicking the dismisstextview and when u re open it alert will still be hidden.
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = getSharedPreferences(getApplicationContext().getPackageName(),0);
editor = sharedPref.edit();
LinearLayout alertLayout = (LinearLayout)findViewById(R.id.alertLayout);
TextView textViewDismiss = (TextView)findViewById(R.id.textViewDismiss);
if(sharedPref.getBoolean("isLayoutAlertShown",false)){
alertLayout.setVisibility(View.GONE);
}else{
alertLayout.setVisibility(View.VISIBLE);
}
textViewDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertLayout.setVisibility(View.GONE);
editor.putBoolean("isLayoutAlertShown",true);
editor.apply();
}
});
}
}
I would recommend you to use SharedPreferences since this is related to setting matter. You can refer to this link: https://developer.android.com/training/data-storage/shared-preferences#java
You have to call this method at the onCreate. So it will check if the there is showLayout stated true or false, then it will update the design.
//Inside onCreate
private void initSharedPreference(final Activity activity, final LinearLayout linearLayout) {
final SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
if (sharedPref.getBoolean("showLayout", false)) {
linearLayout.setVisibility(View.GONE);
} else {
linearLayout.setVisibility(View.VISIBLE);
}
}
At the setOnClickListener you need to implement this method.
//At the button
private void setSharedPrefShowLayout(final Activity activity, final Boolean isVisible) {
final SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("showLayout", isVisible);
editor.apply();
}

Change background of ImageButton in popup window

I'm building an app and want to change the teamcolors in a popup window. I used ImageButtons to show the users the teamcolors. One button in the main activity and four buttons in the popup window. When I click one in the popup window, I can switch the backgrounds but when I close the popup window and open it again, the buttons in the popup window has been reseted.
How can I close my popup window without the reset?
public void colorchange(final View view){
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popwindow, null);
relativeLayout = (RelativeLayout) findViewById(R.id.popup);
popupWindow = new PopupWindow(container, relativeLayout.getWidth(), relativeLayout.getHeight(), true);
popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY, (int)relativeLayout.getX(),(int)relativeLayout.getY());
ImageButton narancs = (ImageButton) container.findViewById(R.id.imgbutton1);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return false;
}
});
narancs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable asds = v.getBackground();
Log.d("hatter", String.valueOf(asds));
v.setBackgroundResource(R.drawable.bluebutton);
view.setBackgroundResource(R.drawable.orangebutton);
Log.d("hatter", String.valueOf(view.getBackground()));
//popupWindow.dismiss();
}
});
}
This code runs on mainactivity button click event.
If I understand your answer correctly, you want to "save" the button background color on click. If that is so, you could use Shared Preferences (Android Developer - Shared Preferences) to store the selected color on click. in the activity, you could set the color of the button from the shared preferences.
On Click
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.apply();
Afterwards, you can load this value to set the color of the Background
SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
//You can use a switch or if else
if(settings.getString(key, defValue).toUppercase.equals("BLUE")){
//set Color Blue as Background
v.setBackgroundResource(R.drawable.bluebutton);
}else if(...)
I hope this helps :)

how to set a button to mimic another button's backgroundresource

I want to know how I could change a buttons backgroundresource that mimics another button's background resource so that whenever I change that button's backgroundresource it another button mimics the looks of the first button...
for example:
int icon = R.drawable.ic_icon; //more specifically I stored R.drawable.ic_icon in SQL and retrieve and save in int icon when retrieve from that table, so when the table is change the first button dynamically change on create;
btn_01.setBackgroundResource(icon); //when this button is pressed it inflates a layout containing btn_02
btn_02.setBackgrounResource(??????); //this button is on a different layout and is used by different activity and should take the backgroundresource of the button that have been pressed to call that layout.
I could use if else statement but I have different button to be copied by the second button and each button has different backgroundresource possibility.
I couldn't understand your question fully but anyways here is what I got
You can put the resource id in the Intent before starting the activity when Btn_01 is clicked.
Btn_01.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("resource_key", R.drawable.ic_heart);
startActivity(intent);
}
});
then in your Activity2 you can just get your resource data and set to whatever button you want
int DEFAULT_RESOURCE = R.drawable.ic_close;
int resourceId = getIntent().getIntExtra("resource_key",DEFAULT_RESOURCE);
Btn_02.setBackgroundResource(resourceId);

Change Text in a same Layout

I'm Making simple app for project
That App contains lot of text so i want,
"when a button is pressed, text should Change in same layout"
like PowerPoint slide.
I want change text only not scroll.
Now i made my app, have lots of Windows or Layouts.
It is not looking good, too much layout in simple app so please help me .
Thanks in advance
Doing this is very easy, I will quickly walk you through the Algorithm:
Set a class level variable called as FLAG initialize it to 1.
Let us assume that FLAG = 1 will represent the first slide. FLAG = 2 the second slide and so on.
Now in your button click you can use a switch case or an if else condition, based on the value of the flag display the relevant text in textview.
Once done, increment the flag, for the next set of sentence(s).
Class level:
int FLAG = 1;
onCreate:
Initialize your textView:
TextView mtv = (TextView)findViewById(R.id.yourid);
Set a button click listener:
private View.OnClickListener slides = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(FLAG ==1)
mtv.setText("First slide");
else if(FLAG ==2)
mtv.setText("Second Slide");
//and so on...
FLAG = FLAG+1;//increment flag
}
};

Categories

Resources