I want to create a button that displays a given image with a transparent background. I am using the code segment below:
ui->setupUi(this);
this->setFixedSize(width, height);
QPixmap* orgPixmap = new QPixmap(":/images/a.png");
QSize size(this->width(), this->height());
QPixmap resizePixmap(orgPixmap->scaled(size));
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(resizePixmap));
this->setPalette(palette);
ui->toolButton->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
ui->toolButton->setFixedSize(64, 64);
ui->toolButton->setIconSize(QSize(64, 64));
ui->toolButton->setIcon( QIcon(":/images/add.png") );
At the top of the widget, I have added a button with an icon. This is a blue circle with a transparent background. However, when I deploy and run my code on an Android device, the button is displayed with an opaque background with white colour.
Please see the picture:
I have tried to force the transparency for the button using the following code:
QColor transparent_color(0,0,0,0);
QPalette button_palette(ui->pushButton->palette());
button_palette.setColor(QPalette::Button, transparent_color);
ui->toolButton->setPalette(button_palette);
The above code did not seem to have the desired effect as I had expected.
I would like to know what the problem might be and how to fix it.
I see you need a circular button, so how about QWidget::setMask()?
QPainterPath path;
path.addEllipse(ui->pushButton->rect());
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
ui->pushButton->setMask(mask);
Just an idea, I did not test that.
You could set a simple style sheet. Like so: widget->setStyleSheet(rgba(255, 255, 255,0);"); if you want to adjust the opacity, just change the fourth parameter.
I have recenyly resolved similar problem on a Win/Mac project based on Qt Graphics View Framework, pls note I never tested this on Android platform.
Anyhow, first you need to setForegroundBrush() for your Graphics Scene:
ui.yourGraphicsView->scene()->setsetForegroundBrush(Qt::transparent);
Alternatively you may use Qt Designer to get similar result or edit your Project.ui file as follows:
<widget class="QGraphicsView" name="yourGraphicsView">
...
<property name="foregroundBrush">
<brush brushstyle="NoBrush">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</property>
And now setBackgroundBrush() as transparent for your button:
ui->toolButton->setBackgroundBrush(Qt::transparent);
I found these two to be sufficient to get hovering transparent button over other objects in QGraphicScene; calling setMask() is not required as Qt does it automatically for any QGraphicsPixmapItem (or ancestor) when it's set to alpha-channeled PNG. For buttons it also works as you would expect: clicking transparent area of your PNG (displayed white above) would NOT trigger OnClick() event, while clicking trnslucent parts on PNG-based button would.
Hope this helps...
Related
I'm using the amazing library MPAndroidChart. It works like a charm, except when I'm trying to change the background color of the BarData. Default color is white, and I want to change it to Transparent.
I've tried this :
Paint p1 = mChart.getPaint(Chart.PAINT_GRID_BACKGROUND);
p1.setColor(Color.RED);
and this:
<com.github.mikephil.charting.charts.BarChart
android:id="#+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
... but it seems that doesnt works.
Any ideas ?
Thanks =)
Since release v1.6.5, the background of the Chart is transparent by default. Meaning, that everything in the background (chart background, other Views, etc.) that is not overlayed by data drawn into the Chart, will be visible.
If you want to change the background (color, or maybe drawable), you can either do that by changing the chart-background
in .xml (android:background="...")
by calling setBackgroundColor(...) or setBackgroundResource(...)
Another way could be to change the background of the parent layout that contains the Chart.
Code to change Background color:
chart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
chart.setDrawGridBackground(false);// this is a must
if you want to Change Whole Screen Background Color
Barchart chart;
chart.setBackgroundColor(Color.rgb(0, 0, 0));//Set as a black
chart.setDrawGridBackground(false);//set this to true to draw the grid background, false if not
Happy to help Thanks
So, after some research, I've found that it's not possible yet: https://github.com/PhilJay/MPAndroidChart/issues/53
Hope It will be possible soon ! =)
I have this button in my layout:
<Button
android:id="#+id/followers_right"
android:drawableLeft="#drawable/ic_team"
android:text="#string/visibility_right_followers" />
By default, all my icons are grey. But in some cases, I want to apply a specific color. And for the drawable button, how can I do that programmatically ?
myButton.setColorFilter(getResources().getColor(R.color.tint_selected));
This line doesn't work (build error).
Thanks for your help!
I tried to find a solution myself for this interesting question but I am not there yet. One work around is have the same icon in different colors as Drawable resources,
and change the icon of the button grammatically,
yourButtom.setCompoundDrawablesWithIntrinsicBounds(R.drawable.low, 0, 0, 0);
But the only issue is, if you want huge range of color chages
In my application I have a CameraPreview. I would like to add an imageView over it so I need the imageView to have a transparent background. The src of the imageView will be a png so I could see the CameraPreview underneath.
However, it's like I've tried everything to manage the background of the ImageView Transparent:
change the alpha value (in the code AND in the xml file)
change the background to #null then to #00000000 (in the code AND in the xml file for both)
But it still not work !
Did I miss anything ?
Please ask if you need parts of my code.
P
Ok thanks for your answears but I have managed to solve my problem:
I gave a random color to the background of my layout (for example #FF34035544) and I changed the two first letters to 00 which made #0034035544.
It was finally very simple !
I am using the MPAndroidChart library. I am setting the multi line chart with data using MPAndroidChart. It's working great but the background is coming white. This is what i am doing
nhChart = new LineChart(getActivity());
nhChart.setDescription("Number of Hits View");
nhChart = (LineChart) hitsView.findViewById(R.id.line_chart_number_of_hits);
//int color = Color.parseColor("#80101010");
nhChart.setBackgroundColor(Color.parseColor("#80101010"));
//nhChart.setBackgroundResource(R.drawable.background_portrate);
//nhChart.setBackground(getResources().getDrawable(R.drawable.background_portrate));
nhChart.setStartAtZero(true);
nhChart.setDrawBorder(true);
nhChart.setNoDataTextDescription("No Data available for Charts");
nhChart.setDrawYValues(false);
nhChart.setDrawBorder(true);
nhChart.setScaleEnabled(true);
nhChart.setHighlightEnabled(false);
nhChart.setTouchEnabled(true);
//nhChart.setGridColor(Color.WHITE & 0x70FFFFFF);
//nhChart.setDragScaleEnabled(true);
nhChart.setPinchZoom(true);
setData(valueDate.size(),10000);
nhChart.animateX(2500);
Legend l = nhChart.getLegend();
l.setForm(LegendForm.CIRCLE);
l.setFormSize(6f);
l.setTextColor(Color.WHITE);
YLabels y = nhChart.getYLabels();
y.setTextColor(Color.WHITE);
y.setLabelCount(6);
XLabels x1 = nhChart.getXLabels();
x1.setCenterXLabelText(true);
x1.setPosition(XLabelPosition.BOTTOM);
x1.setTextColor(Color.WHITE);
I am plotting line graphs in post execute method of AsyncTask in a fragment viewpager. Other fragment showing the graph shows the same white background. I tried setting a color for the background but nothing works. I also left it blank, but its still showing me the white background. I also updated the latest Jar but its not works. Please help.
Here is the image how it looks
You want this for transparent:
chart.setDrawGridBackground(false);
And this for transparent bar:
chart.setDrawBarShadow(false);
Well by default, the backgroud of the chart is transparent which means that it will have whatever color you set for the view/layout below the chart.
If you want to change the background (color, or maybe drawable), you can do that in the following ways:
in .xml (android:background="...")
--> set the background color of the chart in xml or set the backgroud color of the layout the chart is in.
by calling setBackgroundColor(...) or setBackgroundResource(...)
I guess you want your background to be some kind of black?
What happens if you call:
chart.setBackgroundColor(Color.BLACK)?
Does setting the background-color work then? Also check the example code in the github repo here. There are some cases in the example app that change the background color.
You can do this with:
chart.setBackgroundColor(getResources().getColor(R.color.transparent));
Please define first:
<color name="transparent">#00000000</color>
I am developing an Android app. There's this issue with the EditTexts, because in some devices the "background" is white (which is the way I want it), but on others is transparent, and makes the app less professional. Here's an image with the "white background":
On other devices the edit texts that say "Yo" and "Casa" are transparent, so the words mix with the map and its pretty awful. I have tried changing the background color in the android layout xml. The closest one was I believe #drawable/white, but slightly changes the position and size of the edit text, making them look like they're one and its even worst.
Here is an example:
Is there a nice way to approach this problem? I read somewhere that an option was to add a white background image to the edit texts, but seemed like a lot of trouble.
UPDATE
I tried adding a background image, but its the same as changing the background color to any color using the android:background, the edit texts get like smaller or something. I added a blue "delimiter" to the image and this is the result:
But I want them like in the first picture, not so close one to another.
This would be the code of the edittext layout XML, the other one looks very similar. Only by adding the android:background tag changes from picture 1, to picture 3
SOLUTION
To solve this what I did was set a background image and set its height in dp as follows:
<EditText
android:id="#+id/markerTitle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/buttonModifymarker"
android:ems="10"
android:inputType="textPersonName"
android:text="TÃtulo"
android:background="#drawable/editwhite"
>
Why don't you try setting the background of EditText programmatically?
yourEditText.setBackgroundColor(Color.WHITE);
or...
yourEditText.setBackgroundColor(Color.parseColor("#ffffff"));
simply you can add android:background="#drawable/white" attribute to your element in layout xml. you can use a lot of color resource between the double quotes (like from android color resources).
put the following image which has a solid white background in the drawable folder (it may dont appear because it's white and this page's background is white also) right click under this paragraph and select "Save Image as".
and refer to it in your layout and you will get a result like the following image
if the issue continue after trying this, please show me your layout code or email me at : mohamed_hamed4158#yahoo.com , thanks for voting my answer :)