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.
Related
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);
So, basically I want a Text on android appear in this "message style" or like a speakbubble.
btw that was the first picture to pop up from the old whatsapp style xD.
Does somebody know hot to do this?
Assuming you want something similar to the right side, here is a solution:
First create a new drawable xml
speech_bubble.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#90ee90" />
<corners android:radius="5dp"/>
</shape>
Then in your activity xml you can use it like so:
activity_main.xml
<TextView
android:padding="5dp"
android:background="#drawable/speech_bubble"
android:text="This is a speech bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You put a background drawable behind the text view, with a bit of padding on all 4 sides (so there's an offset between the text and the start of the image). Preferably a 9-patch so you can stretch the speech bubble.
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
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);
I've already posted a related question but was partially solved, so here I'll show you the whole code.
The problem is that I can't set a background from a RelativeLayout in white, for example, and set simultaneously, by java code, a background resource (.PNG file) and merge them.
The .PNG image is a prototype of part of the game screen and has transparent space. What I want to obtain is to show this background in white, because of there are details in black that can't be seen because my pre-established background is black (initial theme selected I suppose).
The code below corresponds to the XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/id_act_principal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:keepScreenOn="true"
tools:context=".Principal" >
And the .java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
RelativeLayout fondo = (RelativeLayout) findViewById(R.id.id_act_principal);
fondo.setBackgroundResource(R.drawable.prototipoestructurapantalla);
}
The .java file sets the background image correctly, but in the XML file is set to show the white background and it doesn't show it. It keeps being black.
I hope you can help me.
Try this:
Create a drawable named image_with_white_backgroud.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape >
<solid android:color="#android:color/white"/>
</shape>
</item>
<item>
<bitmap
android:src="#drawable/prototipoestructurapantalla"/>
</item>
</layer-list>
In your RelativeLayout replace android:background="#android:color/white" with android:background="#drawable/image_with_white_backgroud"
you can only have one background at a time, try leaving the relativelayout as it is, and insert an ImageView for the png.