Thursday 25 October 2018

Twitter login button Android.

There is no proper tutorial on twitter-kit-android Github.
After some search on google I finally found the way.
If you encountered an error, check at the end of the post for some solutions

Create app in developer console: developer.twitter.com/apps

Make sure you selected Sign in with Twitter.

To make android app to work, you need to add the scheme: 
twittersdk://
as callback url

Import the library :

If you just need login with twitter button, just use core
otherwise include entire libraby

Adding the TwitterLoginButton in layout:


The rendering error:

If you are targeting SDK version 28, you may get error in the design file.
The error might be 

The error is because of corners of twitter button background.
So you have to manually fix the error by creating a manual class that extend TwitterLoginButton.
If you want you can use the files in the below link.


Setup the button:

Add the following code in onCreate(Bundle savedInstanceState) method of your activity.  If you are adding the button in fragment, then add the code in the enclosing activity. I mean in the activity the fragment/fragment adapter is getting used.
This code needs to be before setContentView(int/view) method call.

Initialise the button and set callback in activity/fragment:


Send the activity result to TwitterLoginButton

In onActivityResult method of activity or fragment, 
if you are using the button inside fragment, then send the activity result back to fragment,

Possible errors while running or at runtime:

If you get an error saying some thing like this,
add the following in your module level build.gradle file scope android ( means inside android{})
If you get message saying "Failed to get request token", make sure you added twittersdk:// in callback urls or schemes.
If you get message like "Authorise token error", make sure you copied the consumer kay and secret correctly.

Tuesday 16 October 2018

Custom RatingBar Android : Use your own colours and symbols.

Before going to coding,

lets talk about how the rating bar stars designed.

The rating bar star has 3 layers : Background, Secondary progress, Progress.

Background layer defines the shape of the drawble that needs to be filled.

Secondary progress defines the drawable that needs to be filled if the progress is less enough to fill a star(Remaining half star).

Progress defines the star to be filled.

Now lets go to coding:


Create a drawable resource with any name. I'm using the name rating_proggressbar.xml

This needs to have 3 layers. So make sure the parent of the xml is <layer-list>

These layer ids are @android:id/background @android:id/secondaryProgress @android:id/progress

So the code in rating_proggressbar.xml will look like some thing like this:

In the next step: you need to define the shape of the progress

Here I'm taking a 14dp star from Android Asset Studio
I'm using black and grey here. You can use any colours you want.

The final code in rating_proggressbar.xml will be like this:


Now use this in rating bar


In Lollipop you can use tint for more colours, progressBackgroundTint, secondaryProgressTint , progressTint.

With custom colours, The code will be like this: