How do I create a transparent Activity on Android?

前端 未结 22 2133
忘掉有多难
忘掉有多难 2020-11-21 04:43

I want to create a transparent Activity on top of another activity.

How can I achieve this?

22条回答
  •  孤独总比滥情好
    2020-11-21 05:32

    In my case, i have to set the theme on the runtime in java based on some conditions. So I created one theme in style (similar to other answers):

    
    
      
    
    

    Then in Java I applied it to my activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        String email = getIntent().getStringExtra(AppConstants.REGISTER_EMAIL_INTENT_KEY);
        if (email != null && !email.isEmpty()) {
            // We have the valid email ID, no need to take it from user,
            // prepare transparent activity just to perform bg tasks required for login
            setTheme(R.style.Theme_Transparent);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
        } else
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_dummy);
    }
    

    Remember one Important point here: You must call the setTheme() function before super.onCreate(savedInstanceState);. I missed this point and stucked for 2 hours, thinking why my theme is not reflected at run time.

提交回复
热议问题