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 !
Related
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...
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 :)
I can't get my relativelayout to be able to be set to transparent. I have tried many different methods, custom view from class, adding android:background="#android:color/transparent" etc. They either show a white like in the picture below, or they show a solid color of whatever the hex is?
Even programmatically using '.setBackgroundColor(Color.TRANSPARENT);' results in the above picture.
Figured it out. The main LinearLayout of my view was the layout making it have a white background. I added my view under my listview in a RelativeLayout and it works great. Thanks all!
Try using this :
android:background="#00000000"
Try using #AARRGGBB instead of #RRGGBB
Do this as following:
android:background="#AARRGGBB"
becomes:
android:background="#77000000"
The first 2 (AA) will set the transparancy. Set 77 to 00 for zero transparancy,
how to use color state list for background?
I know android:background="#drawable/drawable_selector", but android:background="#color/color_selector" will cause exceptions.
but android:background="#FFFFFF" works again, can anyone explains why?
now i want to change a layout's background color(not a drawable) when it's pressed,
how to do it?
dynamically you can change like this. Use this if it is useful for you -
textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));
put the color in res/values/colors.xml,
like #FFFFFF,
and then create a drawable xml in drawable directory,
,that is ok.
I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"