Why Design(activity_main.xml) and actual app look different? - java

i am new in Android development in java
my activity_main.xml file is look like this
But when i run this application in my pixel 3 phone than it's look's like
why this design are different ?

As seen in background, you ignore missing constraints. When working with ConstraintLayout you must provide at least one horizontal and one vertical constraint, otherwise views could jump, and I believe IDE gave u that error.

check your style file.
By default is with Dialog.
u can change it with No Dialog
Sorry i mean
the line
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
u can change the theme with no actionbar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
there are many variants

if you want to have a full screen theme like Design(activity_main.xml):
solution 1: go into the manifest and use a full screen theme.
<activity
.
.
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
solution 2:
Just add the following attribute to your current theme:
<item name="android:windowFullscreen">true</item>
for example :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/orange</item>
<item name="colorPrimaryDark">#android:color/holo_orange_dark</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
solution 3 :
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
and for manage your buttons, read Dorian's answer to Design and actual app look same.

Related

Android Studio Java: setSystemUIVisibility cannot resolve symbol view

I'm developing an app using Capacitor js and trying to achieve an edge-to-edge display on Android. Unfortunately it looks like this requires knowledge about Android Studio and Java that I just don't have so I'm finding myself quite confused.
I found this documentation page, which tells me that to remove the black bar at the bottom of the screen I can either:
Add the following to my app styles:
<style name="AppTheme" parent="...">
<item name="android:navigationBarColor">#android:color/transparent</item>
<!-- Optional, but recommended for full edge-to-edge rendering -->
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
Which I've added in res/values/styles.xml, but which doesn't seem to be doing anythin (the black gesture bar is still there):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:navigationBarColor">#android:color/transparent</item>
<!-- Optional, but recommended for full edge-to-edge rendering -->
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
Or, do it programmatically by calling:
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
I tried to add the above code to my MainActivity.java like this:
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends BridgeActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
Unfortunately Android Studio is telling me that it cannot resolve symbol 'view'. I understand why that's happening since I have no reference to view in my class, but I'm unsure on how to get a reference to view at all.
I'm sure this can't be that complex, I'm just having a hard time understanding Android Studio and Java as I have very little experience with them. If anyone could point me in the right direction that would be great.
EDIT: Or is setSystemUI deprecated? Should I use this instead? I'm really lost
You can use the decorView() from your activity. View view = getWindow().getDecorView(). Also, note that this view can be null, so always check for null before using it.
Retrieve the top-level window decor view (containing the standard
window frame/decorations and the client's content inside of that),
which can be added as a window to the window manager.

Change background of whole app

I want to give the users of my app the option to change the background of the whole app . My solution is to create a button and in the onclicklistener, i would change the background of each activity separately with setBackgroundResource.
Is there a better way to do this?Or my way is sufficient?
Put this style in res>values>styles.xml
<style name="bgThemeDark" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#drawable/bg1</item>
</style>
and
<style name="bgThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#drawable/bg2</item>
</style>
Change #drawable/bg2 and #drawable/bg1 with your background resource.
Then make an BaseActivity in your app, extend all activity by BaseActivity.
Then write this in BaseActivity onCreate
boolean darkTheme = true;
public void onCreate(Bundle savedInstanceState) {
setTheme(darkTheme ? R.style.bgThemeDark:R.style.bgThemeLight);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
You can also set this in manifest if you don't want change theme at run time.
<application
android:theme="#style/CustomBackgroundTheme"
or
<activity
android:name=".appClasses.activities.ActivityMain"
android:theme="#style/CustomBackgroundTheme"
>
Caution: You should not set any background of any activity layout parent nodes.

Can I change my android application style at runtime?

I want to change the manifest style when the user chooses a setting in the preferences file or settings. I have a setting in my apk which includes a list of preferences with three entries {Style_1, Style_2, Style_3}. I want to change the style color, like primaryColor, when the user clicks a style. How can I do this?
Yes you can do this easily, I do it all the time.
Just call this method before setContentView like this:
setTheme(R.style.Theme);
setContentView(R.layout.activity_layout);
Now what I do is that i take a static int variable in the app constants and change it according to my theme. Then i do something like this
//This is in my constants file
public static int CURRENT_THEME = R.style.AppTheme;
//This is in my onCreate of every Activity.
setTheme(Constants.CURRENT_THEME);
setContentView(R.layout.activity_layout);
Hope this helps.
Yes you can set Theme like this:
activity.setTheme(R.style.theme_large);
activity.setTheme(R.style.theme_small);
<style name="theme_large">
<item name="main_background">#drawable/background_red</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_light</item>
</style>
<style name="theme_small">
<item name="main_background">#drawable/background_red</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_light</item>
</style>
Create Two Style
setTheme(darkTheme ? R.style.AppThemeDark : R.style.AppThemeLight);
<style name="AppThemeDark">
<item name="main_background">#drawable/background_dark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="button_light">#color/button_dark</item>
</style>
<style name="AppThemeLight">
<item name="main_background">#drawable/background_light</item>
<item name="colorPrimaryDark">#color/colorPrimaryLight</item>
<item name="button_light">#color/button_light</item>
</style>
I think, setting theme after a button click or option choosen could be harsh for an Activity to handle.
What I am suggesting you is to reload your activity on button click but before that simply save the style_name , the user want to apply in internal memory or Shared Preferences. You can apply shared preference simply looking at here.
At the start of onCreate of your activity, apply that fetching part of shared preferences and apply the theme as user has indicated. This would help you to preserve the theme for that user until s/he uninstall that application or clears the app data.
If s/he is using the application for first time, the stored style_name string would be null so load your application with default theme.
You can simply reload your activity on button click by using the below code:
public void onClick (View v){
Intent intent = getIntent();
finish();
startActivity(intent);
}
Hope it helps !!

Welcome Activity with background of image

I use the theme to show my WelcomeActivity
#android:style/Theme.NoTitleBar.Fullscreen
But I found it will have a period of black background when the app start
So I want to change the background of this theme
then I use...
<style name="WelcomeTheme" parent="#android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#drawable/welcome_img</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
Although the background is changed , the Actionbar is also showing!!
the Actionbar doesn't show when I use Theme.NoTitleBar.Fullscreen
but it's showing when I use WelcomeTheme
how should I remove the ActionBar
Thanks for your help :)
-
By the way , in my WelcomeActivity extends Activity but not ActionBarActivity
Did you try one of those?
in Activity;
getActionBar().hide();
in Fragment;
getActivity().getActionBar().hide();
1. Make you welcome activity.
2. Go to styles xml file and create a style like that:
<!-- BWelcome application theme. -->
<style name="WelcomeTheme"
parent="android:Theme.DeviceDefault.NoActionBar.Fullscreen">
<!-- Customize your theme here. -->
</style>
3. Go to manifest file and add this to your welcome activity:
android:theme="#style/WelcomeTheme"
4. Add your welcome logo in an ImageView
5. Advice: Never use Java code if you can do it with XML
Hope it helps!
You can do this way:
WelcomeActivity extends ActionBarActivity:
on onCreate();
getSupportActionBar.hide();
In Layout file:
setBackground to Parent View:
Hope this will help you.

Android action bar overlay & hidden by default in xml

I need to have the action bar hidden on application start. Then, after some user action, it has to show up again overlaying the content. Currently I turn it off in a custom style by setting <item name="android:windowActionBar">false</item>, and then call
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getActionBar().hide();
in onCreate() of the activity.
That seems a little awkward though. Can I do all of it in xml configs?
I don't know about the hiding part, but you cna style the ActionBar as an overlay in your apps theme like this:
<style name="AppTheme" parent="#style/Theme.AppCompat">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>

Categories