Is there an any way to change popup menu background color programmatically?
I know it can do by style.xml but in my app, background color should be change according to a variable (not constant color).
Is there any idea?
PopupMenu popup = new PopupMenu(ViewProfileActivity.this, viewProfileSettings);
popup.getMenu().add(userAccountSettings.getDisplay_name() + " more...");
popup.getMenu().add("Ban user");
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popup.show();
I know there is an option like that
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#color/darkergray</item>
</style>
But i need to do that programmatically to make color variable
Related
I am trying to change the color of the background app when I click a menu for example change background black.
The color it is changing for the first time into transparent so like grey and than stays blue.
I want if the color is red to take red and other colors.
This is my code.
mPopupMenu = new PopupMenu(this, settings);
MenuInflater menuInflater = mPopupMenu.getMenuInflater();
menuInflater.inflate(R.menu.main_settings, mPopupMenu.getMenu());
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPopupMenu.show();
mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if(id == R.id.menu_back_white) {
setActivityBackgroundColor(R.color.white);
} else if (id == R.id.menu_back_black) {
setActivityBackgroundColor(R.color.material_grey_900);
}
return false;
}
});
}
});
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
colors
<color name="material_grey_900">#ff212121</color>
<color name="white">#FFFFFF</color>
styles
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">#color/blue_900</item>
<item name="colorPrimary">#color/blue_300</item>
<item name="colorAccent">#color/blue_300</item>
<item name="colorControlNormal">#color/white</item>
</style>
view.setBackgroundColor expects a color value but you give it a resource id.
As stated in Get color-int from color resource you have to convert it with e.g. (deprecated variant)
getResources().getColor(color);
or
ContextCompat.getColor(context, R.color.your_color);
(since support library 23)
So inside of side_nav_bar.xml is this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="#+id/colorNav">
<solid android:color="#color/colorPrimary"/>
</shape>
For use when using navigation drawer layout. I was wondering if there is a way to dynamically change this color inside of a fragment. For example, I have a settings fragment where the user can pick three different themes. I would like this side_nav_bar.xml shape color to match the theme. I can't figure out how to reference it programmatically in the code.
Example image below. I want that pink to change based on theme choice.
You can change the Navigation header color like this.
NavigationView navigationView = findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
header.setBackgroundColor(getResources().getColor(R.color.my_color));
By using below method you can change any shape color dynamically.
public static void changeShapeColorRunTime(View view, int colorCode) {
if (view == null) {
return;
}
Drawable background = view.getBackground();
if (background instanceof ShapeDrawable) {
((ShapeDrawable) background).getPaint().setColor(colorCode);
} else if (background instanceof GradientDrawable) {
((GradientDrawable) background).setColor(colorCode);
}
}
Just Pass your view and color code to the method to change the color of your navigation bar shape color dynamically.
I want to create a simple drop down list/listview like in below image. It should generate programmatically without using any xml layout.
NOTE : I am not using a spinner in here . Also I want to open it when I click on the ImageView next to the Switch.
I have no idea about this .
Have any ideas ?
not perfect, but it works ;)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenu().add("Edit");
popupMenu.getMenu().add("Delete");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getTitle().toString()) {
case "Edit" :
//execute "edit" action
break;
case "Delete" :
//execute "delete" action
break;
}
return false;
}
});
popupMenu.show();
}
});
Just try to check and implement it
PopupMenu overflowPopupMenu = new PopupMenu(getContext(), finalOverflow);
overflowPopupMenu.getMenuInflater().inflate(R.menu.popup_overflow_options, overflowPopupMenu.getMenu());
overflowPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
break;
case R.id.delete:
break;
}
return true;
}
});
overflowPopupMenu.show();
popup_overflow_options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/edit"
android:title="#string/edit"/>
<item
android:id="#+id/delete"
android:title="#string/delete"/>
</menu>
I use PopupMenu's for this. See also this guide. The guide explains how to use the PopupMenu with an xml menu resource.
In your case you would attach a click listener to the ImageView. That listener would then create a PopupMenu using the ImageView as an anchor. Like this: PopupMenu popup = new PopupMenu(imageView.getContext(), imageView);
At this point since you need dynamic menu items you have the following options:
You can call PopopMenu.getMenu() and manually populate it with MenuItems
You can create an xml menu resource and then adjust/hide ones that need to be changed
I have several radio buttons, each of which has a View.OnClickListener. The whole purpose of those listeners are for me to be able to change the color of the text associated with the RadioButton upon clicking this very RadioButton. My problem is, I'm not sure how to do that.
Here is how I set a listener for each RadioButton:
radioButton.setOnClickListener(badAnswerListener());
Here's the badAnswerListener method:
private View.OnClickListener badAnswerListener(){
return new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//How do I change the radioButton's color?
}
};
}
If radioButton is private member of class :
radioButton.setTextColor(R.color.customcolor);
See this too, it's almost the same question :
Changing Text color of RadioButton if clicked in android
if you are only using radio button clicklistners to change the color of text.Have a look at the better approach.To change the text color when radio button is checked(clicked)
1) Use radio_text_selector.xml as below and put it into res/color folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#android:color/red" />
<item android:color="#504f4f" /> <--default case
</selector>
Use the above selector in "android:textColor" attribute like below
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/radio_text_selector" />
private View.OnClickListener badAnswerListener(){
return new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Change the clicked radioButton's text to red
((RadioButton) arg0).setTextColor(Color.RED);
}
};
}
I have few buttons containing only labels. They are made using 2 image resources (pressed is little bit bigger than not pressed)
button_image.png
button_image_pressed.png
and xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_image" android:state_pressed="false"/>
<item android:drawable="#drawable/button_image_pressed" android:state_pressed="true"/>
</selector>
And i want to use custom TextView insted of this.
Is it possible to chage TextView textSize when user touches text and change textSize back when text user releses screen?
I tried different solutions, but they didn't work.
You can try using an onTouchListener().
textView.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch( View v, MotionEvent event ) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
textView.setTextSize( ... );
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
textView.setTextSize( ... );
}
return false;
}
} );