My app is work for API 19. So I can't use android:elevation. So I app:elevation in my layout.
Android XML: android:elevation vs. app:elevation
For exemple :
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="5dp">
How to change app:elevation programmatically ? I can change android:elevation but I can't find how to change app:elevation !
Try with using the following code
ViewCompat.setElevation(View, float)
Following the below link. Here showed how to make elevation on pre-lollipop device
Android AppCompat 21 Elevation
The accepted answer won't actually work. The answer should have been updated.
If you look at why mAppBarLayout.setTargetElevation(0) is deprecated. it states that:
#deprecated target elevation is now deprecated.
AppBarLayout's elevation is now controlled via a {#link android.animation.StateListAnimator}.
This method now always returns 0.
So to programmatically set the appbar layout you can only do it by creating a StateListAnimator such as appbar_state_unelevated_animator at the animator res folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<objectAnimator
android:propertyName="elevation"
android:valueTo="4dp"
android:valueType="floatType"
android:duration="100"/>
</item>
then loading it in code like so:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.stateListAnimator = AnimatorInflater.loadStateListAnimator(applicationContext, R.animator.appbar_state_unelevated_animator)
}
If I'm not mistaken you could also create a StateListAnimator by code but don't take my word for it.
Update:
In my experience when you set the
app:elevation="//Any DP value//"
The Shadow of the app bar will be lost. Simple remove that line to get the appbar shadow again.
I would like to provide updated and full answer:
switch (state) {
case On:
appBarLayout.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.animator.appbar_elevation_on));
break;
case Off:
appBarLayout.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.animator.appbar_elevation_off));
break;
}
appbar_elevation_on.xml under RES\animator
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="PrivateResource">
<item>
<objectAnimator
android:propertyName="elevation"
android:valueTo="#dimen/design_appbar_elevation"
android:valueType="floatType" />
</item>
</selector>
appbar_elevation_off.xml under RES\animator
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="PrivateResource">
<item>
<objectAnimator
android:propertyName="elevation"
android:valueTo="0dp"
android:valueType="floatType" />
</item>
</selector>
Since you try to change the elevation of the Action bar you can easily use this line, which will work on API 19
getSupportActionBar().setElevation(0);
or you follow this link and check out how to change the elevation of views on Android below API 21 generally
Android AppCompat 21 Elevation
Accepted answer didn't work for me.
To remove shadow in Toolbar use
app:elevation="0" in AppBarLayout xml or
use mAppBarLayout.setTargetElevation(0) in java.
It worked in my case.
The following solution is for NativeScript
let nativeAnView = tnsView.android;
var shape = new android.graphics.drawable.GradientDrawable();
shape.setShape(android.graphics.drawable.GradientDrawable.OVAL);
shape.setColor(android.graphics.Color.parseColor('#30bcff'));
nativeAnView.setBackgroundDrawable(shape);
nativeAnView.setElevation(20);
on Android
let nativeView = tnsView.ios;
nativeView.layer.shadowColor = new Color('#888888').ios.CGColor;
nativeView.layer.shadowOffset = CGSizeMake(0, 2.0);
nativeView.layer.shadowOpacity = 0.5;
nativeView.layer.shadowRadius = 5.0;
on iOS
Related
I want to make this textview visible above the FloatinActionButton, but the solutions I've tried didn't work, any other suggestions?
Thank you.
[Android Studio screenshot][1]
[1]: https://i.stack.imgur.com/oYBWF.png
First you will need to use Extended FAB instead of FAB. Ok lets start :
step 1
add this to your build.gradle (moudle) file
implementation 'com.google.android.material:material:1.1.0'
at the dependencies
step 2
The Extended FAB shape will be rectangle so you should create a drawable resource file and lets call it shape.xml and add this code inside it :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:height="50dp"
android:width="50dp"/>
<solid android:color="#color/colorPrimary" />
</shape>
step 3
Now , lets create the Extended FAB and it's will be like this :
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/shape"
android:text="OK"/>
the result :
It's quite simple. Just put this inside your textview
android:elevation="1dp"
This will give it an elevation.
I am having trouble with loading vector files in my splash background xml file. The vector has been loaded successfully because I can open the vector file in android studio and I get a small graphic of the vector on the side of the xml file. However, the design view of the xml file shows an error for the vector file, and I get a ResourceNotFoundException when trying to run my app.
Code below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_loading_screen_logo"/>
</item>
</layer-list>
splash background.xml
My grade version is classpath 'com.android.tools.build:gradle:4.0.1'
and I have included the following line in my grade app file vectorDrawables.useSupportLibrary = true
It seems that any vector file that I use has this error, so there is some setting of some sort that is missing.
Please assist in anyway.
Thanks,
The problem is wrapping the xml drawable in a <bitmap/>
Try this:
Replace
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_loading_screen_logo"/>
</item>
With:
<item android:gravity="center"
android:drawable="#drawable/ic_loading_screen_logo"/>
replace <bitmap to <item
make sure that your vector is in drawable folder not in drawable-24.
check vectory path may be its too long.
I want to to have the height of an ImageView equal to it's width. I already read something about using "app:..." in activity.xml.
But, it says that it couldn't find the namespace. After that, I just found a code which didn't worked for me.
Does anybody has ideas or suggestions to fix this issue? If it has to be done programmatically and it would be good if has an example for this using Kotlin.
If you want to have the height of an ImageView equal to it's width,
Programmatically [Dynamic way]:
int widthOfImage = image_view.getLayoutParams().width;
image_view.getLayoutParams().height = widthOfImage ;
refer to this answer
you can do it in Hard-coded way:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_background"/>
or you can define background like this image_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#003778" />
<corners android:radius="6dp" />
<size android:width="100dp"
android:height="100dp"/>
</shape>
and add it as background in imageView. [remember it is also hard-coded]
You have to use this custome view inside you xml file.
Example
<YourPackageName.SquareImageView
android:id="#+id/squareImageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher_background"/>
Now you have to bind this view in you code. Like Fragment or Activity whatever you have used.
Exmple (This is code in java you can change it in kotlin) :-
SquareImageView imageView = findViewById(R.id. squareImageview);
I am getting the error: error: No resource identifier found for attribute 'showAsAction' in package:
I cannot find out why I am getting this error but I have been at it for hours without any sort of break through. I have searched google with many different resolutions but none that appear to work for me.
I would be grateful if someone could help direct me towards a fix to the error.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="org.nibbler.zoe.liteplayer.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
Most likely showAsAction is not your attribute, hence not available in app: namespace. Did you mean android:showAsAction?
If u are having min ver below 14 and u want to add showAsAction for below 14 api devices, then u can use this.
1.)U need support library to be added in build path.
2.)Need to add app theme as Theme.appcompat.
3)Rest are as u are using means add ur own namespace with .../res-auto value and use that name with showAsAction
You can add support lib res like this http://developer.android.com/tools/support-library/setup.html
You need to extends ActionBarActivity instead of Activity.
For complete Action bar guide follow the link http://developer.android.com/training/basics/actionbar/setting-up.html
I want to use an activity as a dialog in my android application. Till now, I've used AlertDialogs but I want to make my app more fancy. I've done a lot of research on this but couldn't find a satisfactory answer.
I created a new activity named DialogActivity.java and dialogactivty for the java and xml files respectively.
So, How do I call this activity as a dialog in some other activity?
AND
How do I give this activity a round cornered rectangle dialog shape?
Thanks in advance!
You can set a dialog theme to activity as
<activity android:theme="#android:style/Theme.Dialog" />
and use
<activity android:theme="#android:style/Theme.Holo.Dialog" /> // api level 11 and above
in manifest for the activity required.
You need to start the activity using startActivity(intent)
From the official documentation,
If you want a custom dialog, you can instead display an Activity as a
dialog instead of using the Dialog APIs. Simply create an activity and
set its theme to Theme.Holo.Dialog in the manifest element:
<activity android:theme="#android:style/Theme.Holo.Dialog" >
That's it. The activity now displays in a dialog window instead of
fullscreen.
Use Theme.Dialog for API 10 or lower.
try this :
android:theme="#android:style/Theme.Dialog">
entry in manifest for the activity you required.
#ChinmayDabke To display with rounded corners, you need to create a new shape using xml and set it as the background drawable.. be sure to also set transparency for your activity. For more detailed information on this, there are many up-to-date topics available - I just wanted to give a quick answer to this old post, since you had asked!
My mistake - you don't need to set as Transparent - I meant to say set no Titlebar!
Example:
Create a Shape for the rounded background, inside your res/drawable folder:
shape_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/colorOfActivityHere"/>
<corners
android:radius="12dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
Then set the shape as the Background of your activity's parent layout:
layout_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#drawable/shape_background"
android:orientation="vertical">
...
</LinearLayout>
And in your Activity.java, set no titlebar:
Activity.java
requestWindowFeature(Window.FEATURE_NO_TITLE);