Skip to content

Instantly share code, notes, and snippets.

@iateyourtoothpaste
Forked from gabrielemariotti/Activity.java
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save iateyourtoothpaste/53b485721d4ee45e64ad to your computer and use it in GitHub Desktop.

Select an option

Save iateyourtoothpaste/53b485721d4ee45e64ad to your computer and use it in GitHub Desktop.
A Floating Action Button for Android L written in C# for Xamarin.

Floating Action Button (requires Android-L preview) Forked and re-written to target C# (Xamarin) by Jason Pezzimenti.

Instructions:

  1. Create a folder called animator inside the Resources folder of your project and place the anim.xml file in it.
  2. Place the dimens.xml file inside Resources > values folder.
  3. Paste the floatingActionButton.xml code into your Main layout.
  4. Place the ripple.xml file inside Resources > drawable folder.
  5. Edit the Resources > values > Strings.xml file to include: the floatingActionButton item that can be found in this Strings.xml file.
  6. Edit the MainActivity.cs file to include the code from this MainActivity.cs file.

All done!

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_elevation"
android:valueTo="@dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/button_press_elevation"
android:valueTo="@dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<dimen name="floatingActionButtonSize">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/floatingActionButton"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="@dimen/floatingActionButtonSize"
android:layout_height="@dimen/floatingActionButtonSize"
android:layout_gravity="bottom|right"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/ripple"
android:stateListAnimator="@animator/anim"
android:src="@android:drawable/ic_menu_edit"
android:elevation="4dp" />
</RelativeLayout>
public class MainActivity : Activity {
protected override void onCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// Floating Action Button
int size = Resources.GetDimensionPixelSize(Resource.Dimension.floatingActionButtonSize);
Outline outline = new Outline();
outline.SetOval(0, 0, size, size);
var floatingActionButton = FindViewById<ImageButton>(Resource.Id.floatingActionButton);
floatingActionButton.ClipToOutline = true;
floatingActionButton.Click += delegate
{
Toast.MakeText(this, "You pressed the Floating Action Button.", ToastLength.Short).Show();
};
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="floatingActionButton"></string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment