Question: I can't find out how to change the text gravity of the right side labels of my MPAndroidChart.

[That's how it looks actual](https://i.stack.imgur.com/raQbi.png) [![enter image description here](https://i.stack.imgur.com/Rksnn.png)](https://i.stack.imgur.com/Rksnn.png)

[This is what it should look like](https://i.stack.imgur.com/5ngeb.png)[![enter image description here](https://i.stack.imgur.com/e7jQW.png)](https://i.stack.imgur.com/e7jQW.png)

How is that to be done?

Answer: 
You can generalize your `getEnumFromString` function by creating an interface and having your companion object implementing it. An extension on this interface will let you call the function directly on the companion of your enum class.

This will do the trick:

```
interface EnumWithKey<T : Enum<T>, K> {
    val T.key: K
}

/* The reified type parameter lets you call the function without explicitly 
 * passing the Class-object.
 */
inline fun <reified T : Enum<T>, K> EnumWithKey<T, K>.getByKey(key: K): T? {
    return enumValues<T>().find { it.key == key }
}

```

Now you can create your `PaymentMethodType` like this:

```
enum class PaymentMethodType(val type: String) {
    PAYPAL("Paypal"),
    VISA("Visa"),
    MASTERCARD("MasterCard"),
    VISA_DEBIT("VISA Debit"),
    LPQ_CREDIT("Lpq Credit");

    companion object : EnumWithKey<PaymentMethodType, String> {
        // Just define what the key is
        override val PaymentMethodType.key
            get() = type
    }
}

```

And voila, now you can do this:

```
println(PaymentMethodType.getByKey("Paypal")) // Prints PAYPAL

```

The `EnumWithKey` interface can now be reused by just having the companion object of an enum implementing it.
Well? How about this code? 

```
enum class PaymentMethodType(val type: String) {
    PAYPAL("Paypal"),
    VISA("Visa"),
    MASTERCARD("MasterCard"),
    VISA_DEBIT("VISA Debit"),
    LPQ_CREDIT("Lpq Credit");

    companion object {
        private val TAG: String = PaymentMethodType::class.simpleName

        fun fromString(name: String?): PaymentMethodType? {
            val maybeType = PaymentMethodType.values().firstOrNull { it.type == name }
            if (maybeType == null) {
                Log.e(TAG, "No corresponding PaymentMethodType for $name")
            }
            return maybeType
        }
    }
}

```

Just made `getEnumFromString` method simpler like this way.

Moreover, if you want to make your PaymentMethodType more "reusable, flexible and global", add some abstract method onto your `PaymentMethodType` or consider using **Sealed class** in this case. We can guess that many payment methods require their own protocols, and implementing it by `enum` requires an externalised `when` or `if-else` branch to do so. For example, the code should be looks like this:

```
fun paymentProcessor(payment: PaymentMethodType): Boolean {
    return when (payment) {
        PAYPAL -> { processPaypalPayment() }
        VISA   -> { processVisaPayment() }
        // ...
    }
}

```

which is not bad unless numbers of payment methods are limited but not quite desirable. We can remove this insidious `if` or `when` keyword like this way(retaining `enum class` approach):

```
enum class PaymentMethodType(val type: String) {
    PAYPAL("Paypal") {
        override fun processPayment(): Boolean {
            TODO("Not implemented.")
        }
    },
    VISA("Visa") {
        override fun processPayment(): Boolean {
            TODO("Not implemented.")
        }
    },
    // ... more types ...
    ;

    abstract fun processPayment(): Boolean

    // ...
}

```

With either approach, we can eliminate `when` keyword in `paymentProcessor` method I demonstrated like this:

```
fun paymentProcessor(payment: PaymentMethodType): Boolean {
    return payment.processPayment()
}

```

I don't explain `sealed class` approach since the code is not much different compare to `enum class` approach in this case. [The official document](https://kotlinlang.org/docs/reference/sealed-classes.html) may help.

Hope this helps.