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 :)
Related
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.
I would like to know how I can change the background-color of a TextView, through a switch button. It is necessary to clarify that my switch buttons are in MainActivy and my TextView are in other Activities, to which I access like this:
tvBoton = (TextView) findViewViewById(R.id.Schedule);
tvBoton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, central.class);
startActivity(i);
}
});
I don't know if it influences anything, but my switch buttons have SharePreferences:
pol = (Switch) findViewById(R.id.switch18);
sharpol = getSharedPreferences("pol", MODE_PRIVATE);
final SharedPreferences.Editor editorpol = sharpol.edit();
pol.setChecked(sharpol.getBoolean(ex, false));
pol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean ispolChecked) {
if (ispolChecked) {
editorpol.putBoolean(ex, true);
politica=(TextView) findViewViewById(R.id.polit);
politica.setText("Not working");
} else {
editorpol.putBoolean(ex, false);
}
editorpol.commit();
}
});
As it is in the previous code, I tried inside the ispolChecked, in the true option, to reference the TetView, and then put setText, but this TextView, being inside another layout does not work. I also put setContentView(R.layout.main_activity) several times, but changing it to the respective layouts, but it didn't work either.
Honestly, I don't know what to do anymore. Thanks for your answers.
Use SharedPreference to store the state of the Switch and when you have stored it go to the next Activity and change background color of TextView based on the state of the SharePreference.
I am creating a word game application , I have created a grid view consisting of buttons. When I click a button in the grid view a popup windows opens which contains all the English alphabets.Now when I click any letter in my pop up window , I want that letter to appear in my grid that is the string in my pop window's button must appear in my grid view's button. , how do I do it?
This is my button's code:
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View container = layoutInflater.inflate(R.layout.activity_popup,null);
popupWindow = new PopupWindow(container,800,1100,true);
popupWindow.showAtLocation(constraintLayout, Gravity.NO_GRAVITY,150,400);
b1=(Button) findViewById(R.id.b1);
String s1 = b1.getText().toString();
button1.setText(s1);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionevent){
popupWindow.dismiss();
return true;
}
});
}
});
My popup window's code:
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String s1 = "A";
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
Screenshots of the app
This is my grid layout where the user must enter the letters
When I click any button on the grid this is the pop up window which is displayed.
If I run this , the app is getting stopped.
It might help us if you would share the exact exception that is thrown by your application.
If I unterstand you correctly, then b1 is a button contained in your activity_popup- layout. So you might want to try b1 = (Button) container.findViewById(R.id.b1) instead. If thats not working, post your exception!
I have created a custom arrayadapter for my listview. It has a player name and the score, I also have a add button on the cell, when that button is clicked I want a popup screen to appear, Here is where the user can add the score of the player. This popup window has 6 buttons "+1", "+2" "+10" ..etc and a done button. When the done button is clicked the score gets updated.
I am handling the add button click event on my customArraryAdapter class, so I have to create the popup here as well. I have searched and tried to do this with no success.
What I tried so far:
I have a View = convertView and a viewHolder = holder The problem is I'm not so sure what to pass as the parameter to create a popup. The code below is myCustomArrayAdapter class. I have also read popups cant handle touch events, but some are saying it can. Since my popup has a lot of buttons maybe this might be a great solution.
This is in #Override
public View getView(final int position, View convertView, ViewGroup parent) Method in CustomArrayAdapter Class
//Handle add button click
holder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
addScores(convertView);
//list gets updated
notifyDataSetChanged();
}
});
My addScores method looks like this
private void addScores(View v){
PopupWindow pw;
LayoutInflater inflater = (LayoutInflater)v.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup));
pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
pw.showAsDropDown(btnSelectWeight);
}
You may pass View that will be displayed in popup the same way you do.
Consider this:
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup));
Your weight_popup layout should contain 6 buttons, which would have onClick there you update scores.
Something like:
Button btn1 = (Button)layout.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update your score here.
}
});
//other buttons..
pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
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.