BadgedImageview Documentation

repository·master·Indexed 19 days ago

https://github.com/yesidlazaro/badgedimageview

An Android library for overlaying badges containing text or colors onto ImageViews. It provides specialized view classes like BadgedFourThreeImageView and BadgedSquareImageView, as well as ForegroundImageView for rendering drawables on top of image content. Supports configuration via XML attributes and programmatic control through Java.

Tokens
2.1K
Snippets
6
Records
7
Agent score
65%

What's inside BadgedImageview

  1. Install BadgedImageview via JitPack

    master

    To use BadgedImageview in your Android project, add the JitPack repository to your Gradle configuration and then include the dependency.

    // In your project-level build.gradle
    repositories {
            // ...
            maven { url "https://jitpack.io" }
     }
    
    // In your app-level build.gradle
    dependencies {
    	        compile 'com.github.yesidlazaro:BadgedImageview:1.0.2'
    	}
  2. Control BadgedImageView programmatically in Java

    master

    You can manipulate the badge visibility and properties using Java code. Available methods include:

    • showBadge(boolean visible): Shows or hides the badge.
    • isBadgeVisible(): Returns whether the badge is currently visible.
    • setBadgeText(String text): Updates the text displayed on the badge.
    • setBadgeColor(int color): Updates the color of the badge.

    Example usage:

    BadgedSquareImageView badgedImageView = findViewById(R.id.badge_person_gif);
    
    // Show the badge
    badgedImageView.showBadge(true);
    
    // Update badge content
    badgedImageView.setBadgeText("JPG");
    badgedImageView.setBadgeColor(getResources().getColor(R.color.gray_50));
    
    // Toggle visibility
    if (badgedImageView.isBadgeVisible()) {
        badgedImageView.showBadge(false);
    } else {
        badgedImageView.showBadge(true);
    }
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        BadgedFourThreeImageView badgedImageViewDog;
        BadgedSquareImageView badgedImageViewPersonVideo;
        BadgedSquareImageView badgedImageViewPersonGif;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            badgedImageViewDog = (BadgedFourThreeImageView) findViewById(R.id.badge_dog);
            badgedImageViewPersonVideo = (BadgedSquareImageView) findViewById(R.id.badge_person_video);
            badgedImageViewPersonGif = (BadgedSquareImageView) findViewById(R.id.badge_person_gif);
            badgedImageViewDog.showBadge(true);
            badgedImageViewPersonVideo.showBadge(true);
            badgedImageViewPersonGif.showBadge(true);
            badgedImageViewPersonGif.setBadgeText("JPG");
            badgedImageViewPersonGif.setBadgeColor(getResources().getColor(R.color.gray_50));
            badgedImageViewPersonGif.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            if (badgedImageViewPersonGif.isBadgeVisible()) {
                badgedImageViewPersonGif.showBadge(false);
            } else {
                badgedImageViewPersonGif.showBadge(true);
            }
        }
    }
  3. Configure BadgedImageView via XML

    master

    You can define badged image views directly in your layout XML files using BadgedFourThreeImageView or BadgedSquareImageView. The following attributes are available for configuration:

    • app:badgeColor: Sets the color of the badge.
    • app:badgeGravity: Sets the position of the badge (e.g., top|left, end|bottom).
    • app:badgePadding: Sets the padding for the badge.
    • app:badgeText: Sets the text content of the badge.

    Note: The package name for these views is com.creativityapps.badgedimageviews.

    <com.creativityapps.badgedimageviews.BadgedSquareImageView
        android:id="@+id/badge_person_video"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/me"
        app:badgeColor="@color/colorAccent"
        app:badgeGravity="top|right"
        app:badgePadding="@dimen/padding_normal"
        app:badgeText="@string/lab_video" />
  4. ForegroundImageView API Reference

    master

    The following methods are available for managing the foreground content of a ForegroundImageView:

    • void setForeground(Drawable drawable): Sets the drawable to be rendered on top of the ImageView. If the drawable is not null, it is automatically sized to the view's bounds and its callback is set to the view.
    • Drawable getForeground(): Returns the current foreground Drawable, or null if none is set.
    • void drawableHotspotChanged(float x, float y): (Internal override) Updates the hotspot of the foreground drawable when the view's hotspot changes (API 21+).
  5. Create a BadgeDrawable

    master

    Use the BadgeDrawable constructor to create a badge overlay for an ImageView. The badge is rendered as a colored shape with the provided text 'punched out' (transparent) from the background.

    Parameters:

    • context: The Android Context.
    • badgeText: The string to display inside the badge.
    • badgeColor: The integer color for the badge background.

    Note: The badge uses a fixed text size of 12sp, padding of 4dp, and a corner radius of 2dp.

    // Example of initializing a BadgeDrawable
    BadgeDrawable badge = new BadgeDrawable(context, "1", Color.RED);
  6. Use ForegroundImageView to add foreground content

    master

    ForegroundImageView is an extension of ImageView that allows you to render a Drawable on top of the image content. This is useful for displaying badges, overlays, or other foreground elements.

    Key behaviors:

    • The foreground drawable is drawn on top of the image content.
    • The foreground is automatically resized to match the view's dimensions.
    • It supports stateful drawables (the foreground will sync with the view's state).
    • It supports hotspot changes for API level 21 (Lollipop) and above.
    // Setting a foreground drawable programmatically
    ForegroundImageView imageView = findViewById(R.id.my_foreground_image_view);
    imageView.setForeground(ContextCompat.getDrawable(context, R.drawable.my_badge));
  7. Set the foreground drawable in XML

    master

    You can define the foreground drawable directly in your layout XML files using the android:foreground attribute. The ForegroundImageView component is designed to pick up this attribute during initialization.

    Note: This relies on the R.styleable.ForegroundView_android_foreground attribute being defined in the project's resources.

    <com.creativityapps.badgedimageviews.ForegroundImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/main_image"
        android:foreground="@drawable/badge_overlay" />