1

Is it possible to set the visibility of the MainActivity page in android to invisible or make it transparent may be? I have tried setting the visibility to invisible and also made height width as 0dp but this still shows the mainactivity with a white background and blue header. What I am trying to achieve is, I am opening a floating window which is draggable on click of a button in the mainactivity, but I want to directly open the floating window by triggering the button click from the code itself. This works but the mainactivity still shows up for a second before the floating window is opened. I want to make the mainactivity page invisible or transparent so the mainactivity page is not noticeable. Here is the XML of mainactivity.xml ,

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="Invisible"
android:background="@color/material_on_primary_disabled"
tools:context="com.example.widget_android.MainActivity">

<Button
    android:id="@+id/open_widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="Open Widget" />

on click of open_widget(which is triggered from code rather than the user having to click on it) the floating window is opened up. I want to take the user directly to the floating window rather than the landing page which is the mainactivity page.

Here is what is happening on the backend java code

findViewById(R.id.open_widget).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(new Intent(MainActivity.this,WidgetService.class));
            finish();
        }
    });

    findViewById(R.id.open_widget).performClick();
  • Removed `android-studio` tag as that tag is for questions/problems related to the Android Studio product. Your question is a generic Android question and has nothing to do with the Android Studio product. – David Wasser Jun 06 '21 at 18:53
  • 2
    Also, you should edit your question and post the relevant parts of your layout file. Of course you can use a transparent background if you want. It isn't clear what you are trying to do or what you have already tried. Show us more details. – David Wasser Jun 06 '21 at 18:55
  • Have edited my question, please check it out and hope you can help. Thanks! – ravikiran bheemaiah Jun 07 '21 at 09:40
  • Why are you doing this? Why don't you just start your `Service` in `onCreate()` of your `Activity` and then call `finish()`? – David Wasser Jun 07 '21 at 10:06
  • 1
    Yes you are right, i have removed the button click and placed the code in onCreate() – ravikiran bheemaiah Jun 09 '21 at 15:20

1 Answers1

0

The reason you get a white screen is because that is the default behaviour. You've tried to put an invisible, transparent, or non-existing (size of 0 pixels) View on top of the default View, so you are still seeing the default View. What you need to do is to replace the default View with a transparent View.

See How do I create a transparent Activity on Android? for information on how to get a transparent Activity.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I haven't been able to get the translucent style working on my layout after trying out the solution mentioned. I may be doing something wrong. I will update you back on this. Thanks fo the pointers. This is my xml – ravikiran bheemaiah Jun 08 '21 at 03:16
  • You've set width and height to zero! Use "match_parent" instead. – David Wasser Jun 08 '21 at 06:07
  • Which solution did you use? There are many different answers. – David Wasser Jun 08 '21 at 06:08
  • match_parent don't seem to make any difference either. I have used the same solution you have mentioned, [link](https://stackoverflow.com/questions/2176922/how-do-i-create-a-transparent-activity-on-android),this one – ravikiran bheemaiah Jun 08 '21 at 12:52
  • Did you create a style and use it in the `` declaration in the manifest? – David Wasser Jun 08 '21 at 13:03
  • There are many different solutions mentioned in that question. Which specific solution did you use? – David Wasser Jun 08 '21 at 13:03
  • Yes i have created the styles.xml and added it in mainactivity. – ravikiran bheemaiah Jun 08 '21 at 16:54
  • I tried many of the solutions mentioned there, nothing worked actually. I will try to dig a little more and update later. – ravikiran bheemaiah Jun 08 '21 at 16:55
  • did you add the theme in the `` declaration in the manifest? – David Wasser Jun 08 '21 at 18:23
  • Finally I was able to solve this issue by applying the translucent style to the Appcompat theme of the AndroidManifest.xml itself. Thanks for all your help @David – ravikiran bheemaiah Jun 09 '21 at 13:08
  • Glad you were able to solve this. However, I asked a question in the comments about why you are doing this. Could you respond to that question? It seems like you are doing an awful lot of unnecessary work here. – David Wasser Jun 09 '21 at 13:53
  • Also, if my answer was helpful you can accept it by clicking the green checkmark next to the answer. That give me some reputation points and also gets the question off the list of unanswered questions. And it will help others who have similar problems in the future. – David Wasser Jun 09 '21 at 13:54