DISQUS on Android

Here we thought we’d give you a quick tutorial on how to integrate DISQUS  on Android. The implementation in a webpage is fairly straightforward and not too difficult in Android either. Since we just released a version of our Votter App with these comments enabled we thought we’d share some of the details behind the code. We’ll detail out a scenario using an android xml layout, android class and a simple php page. Before starting you should have already created an android app and be able to deploy it.  A hosting account will also be needed for the php component of our walk through. If you have not done this there are many tutorials over on the google android developers webpage.

To start with make sure your app has internet permissions  <uses-permission android:name=”android.permission.INTERNET”></uses-permission> and you have signed up for DISQUS and set up your site. It is good to have some familiarity with the system so try setting up a basic comment page on your webpage first so you know your setup in correct.

Start with making a layout called disqus.xml. Since we will be setting up our php  page in a moment, we will include a WebView object. The most frustrating part of most Android development is testing different resolutions and orientations. Thankfully by using WebView, we let WebView do all the heavy lifting in this area. Place whatever elements you like in the xml file but place a WebView on the bottom.  For most layouts that might have extensive comments, you will likely want to place your whole page in a ScrollView.  Using  fill_parent  is a good idea here if it is just going to be tagged onto the bottom of the layout.

disqus.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" >

<ScrollView
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

// INSERT all your other GUI objects on top of the comments here

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/disqus"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

</LinearLayout>
</ScrollView>
</LinearLayout>

Once you have your layout how you like, it is best to go ahead and set up a php page on your server. This page will essentially be blank but setup the same way any old DISQUS thread would be set up. The difference being we will pass the disqus_identifier in through the URL. With the exception of line 6 this is taken from the DISQUS installation guide. So in case of an update to the API, just follow their guide. Don’t forget to add the shortname for your disqus application.

showcomments.php

<div id="disqus_thread"></div>
<script type="text/<span class=">

/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
 var disqus_shortname = '<shortname>'; // required: replace example with your forum shortname
 var disqus_identifier = '<?php echo $_GET['disqus_id']; ?>';

/* * * DON'T EDIT BELOW THIS LINE * * */
 (function() {
 var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
 dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';

(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
 </script>
 <noscript>

Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a>

</noscript>

<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

Ok now that both components are setup we just need to link them together in our activity. To do this just load your xml layout(disqus.xml) in your Activity (Disqus.java). To load the comments we will simply do it onCreate. Instantiate your WebView and assign what WebSettings your wish to it. In line 27 you can see to load the comments we will just call our php file with a URL parameter that will be the disqus_identifier. You can store this in a variable or have it static depending on how many forums or comment threads you have.

Disqus.java

</pre>
public abstract class Disqus extends Activity
{
private WebView webDisqus;

protected void onCreate(Bundle savedInstanceState)
{

//set layout to disqus xml

setContentView (R.layout.disqus);

webDisqus = (WebView) findViewById(R.id.disqus);
 //set up disqus
 WebSettings webSettings2 = webDisqus.getSettings();

webSettings2.setJavaScriptEnabled(true);

webSettings2.setBuiltInZoomControls(true);

webDisqus.requestFocusFromTouch();

webDisqus.setWebViewClient(new WebViewClient());

webDisqus.setWebChromeClient(new WebChromeClient());

webDisqus.loadUrl("http://<PATH TO WEB SERVER>/showcomments.php?disqus_id="+<disqus thread id>);

}

}

That is it for loading DISQUS on Android. If you have any questions or suggestions leave them in the comments below. If you’d like to see a demo of this first, check out the Votter App on Google Play.  If you want more code samples of different techniques used in Votter or any of the GlobeOtter Apps just send us a message or leave a comment below and we will try to write up a walk through.

Update

To address the login and logout issues we were seeing we added a blog entry here http://globeotter.com/blog/disqus-login-and-logout/.  You can also check out a full implementation of this in our new App the Daily Forum.

  • As an additional matter you may want to manually set your disqus title through a url parameter or else you will receive comments for “untitled” instead of a more useful page title

  • Guest

    test

  • Sumon Chhaterjee

    hii!!!

    it will be of great help if you provide the source code in more details and also let me know where from I will be getting the disqus_id and short name

    • Hi there, the shortname is given to you when you register your site with disqus. It is used to identify your “forum”. So when some one receives a reply it might email the user with the subject [votter] [title] or whatever your [shortname][thread title] is.

      The ID is optional. You can use it to keep track of your own identifier. So for instance in Votter we have Poll Ids and I force Disqus to use this same ID by specifying that variable. You can leave this out of the code and Disqus will just assign you a random id for that discussion.

      Let me know if you have any issues with it and I’ll try to help

  • babu001

    I dont register my app but how to test my app with disqus using local server

    Now only I am developing application.

    • You shouldn’t need to register your app with Google if that what you mean, you will need a DISQUS account though. I don’t think they have a tester account for DISQUS.

    • babu001

      No. I am asking that I am developing application and how to integrate the app with disqus.

      • You should be able to use the process described in the blog. It doesn’t require your app to be finished or registered on Google Play. I have a few in development with this process.

      • babu001

        So We can develop our own comment with integration using disqus id right? Then Here,

        “http:///showcomments.php?disqus_id=”+

        how to give the what it means

  • ccvcvcv

    Great Post!!!

  • Dipen Patel

    nice tutorial. I’m facing problem for redirection. Application is not able to redirect to comment page after login or logout

    • Yes, we have seen a similar problem on some devices. Looking at Disqus it looks like they have a few options for this. We will try out a few options and let you know what we find.

      • Thanks for response. can you please update the solution?

    • Hi,

      We found a solution to the problem. So under some versions of the new Disqus and new android OS systems it hangs on the login and logout.

      We wrote two new classes extending WebViewClient and WebChromeClient. It listens for the log in and the log out functions. I will write up a new post with the code in the next few days. If you need help with it before then just let me know. But the good news is… there is a solution 🙂

      • MDawkins

        Hi,

        Even if you don’t have time for clear explainations, could you post the two classes on pastebin or some similar file hosting service ? It would be very helpful for us since we are struggling to make loggin work in our android app.

        Thanks !

    • ER Abhishek Rai

      Sir plz help me
      But How can generate disqus_id

  • Pingback: Disqus Login and Logout in Android()

  • Thanks for sharing with us your tutorial…..

    Android training in chandigarh

    • ER Abhishek Rai

      Sir plz help me
      But How can generate disqus_id ,

  • Nitish Chawla

    how to generate disqus id ” webDisqus.loadUrl(“http:///showcomments.php?disqus_id=”+);” which is inside this code

    • If it is the thread id you need, you actually decide on the number the first time you load the disqus webpage. After that your program needs some way of tracking it. So for instance you could have it be the page number or some unique identifier for that page in your app.

  • rkumar69

    do you know any other methd except this webView?????

    • sir i m new in this field
      so i have any idea for enable disques login in android ,than share with me plz

  • Using share option is not working. Share url’s http: is missing.

  • androidvideo
    • Asmoutcha

      good

  • ER Abhishek Rai

    But How can generate disqus_id
    Plz help me

    • Your disqus ID will likely be generated by your database. It is completely based on your implementation. In your app you are directing your users to a particular disqus thread. Internally you need to have these pages mapped to some integer.

  • Plz help me any one who has any idea with disques login in android

  • Sir Plz share/help with me disques Tutorials complete code

    i am fresher and currently i am working in small company,And dieques Api impementation is my job and a m facing lot of prab so i want help …

  • Any one can help for Disqus login in android,
    plz send me source codeeeeeeeeeeeeee

  • raji

    Thanks, Above code is working fine. But i have a doubt, the variable disqus_identifier in showcomments.php file is not using anywhere. Can you plz clarify my doubt,Where i need to add this variable?. Then only i can get particular post comments.

  • A, Price

    I am NOT computer ‘savvy’ or Mobile Phone savvy; having said that, my Android/Samsung/At&T keeps kicking my ‘Login’ info out! When I try to ‘loin’, there’s ALREADY an email/password there, that is NOT the right one! But, when I put the ‘right email/password in, it doesn’t “recognize it”!!! I feel like a ‘dog-chasing-it’s tail!!!
    Any help would be much appreciated! Thanks.
    P.S. I have ‘Disqus’ on my ‘desk-top’ and it works fine!

  • Anand Barnwal

    Thanks. Above tutorial is working fine but i’m facing problem to set up PHP file on server.
    It ends up showing “comments powered by Disqus”.

    Can you please provide some more details about line 27 (Like how and where to set up php file on server and where can i get the disqus id).

  • Eden Lynas

    nice guide. I’m experiencing issue for re-direction. Program is not able to divert to opinion web page after sign in or logout

    Spybubble gratuit

  • Nice and useful tutorial for me, i am looking for this since week. Thanks admin. Keep it up.

  • Eric Charles

    Very good and informative article, thanks for this posting..

    Buy Sildenafil Softgel

  • Lovely Singh

    I need to add login feature, whether they can login through the app and add comments. How can I do that?

    http://www.ahdwallpaper.net/

  • Don´t understand the last step? “Ok now that both components are setup we just need to link them together in our activity. To do this just load your xml layout(disqus.xml) in your Activity (Disqus.java).” Help please 🙂

  • What a nice topic and how beautiful you explain. Bravo

  • goodman

    My name is Mrs. ivy. I live in singapore
    and i am a happy woman today? and i
    told my self that any lender that rescue my
    family from our poor situation, i will refer
    any person that is looking for loan to him,
    he gave me happiness to me and my family, i
    was in need of a loan of $250,000.00 to
    start my life all over as i am a single
    mother with 3 kids I met this honest and GOD
    fearing man loan lender that help me with a
    loan of $250,000.00 U.S. Dollar, he is a GOD
    fearing man, if you are in need of loan and
    you will pay back the loan please contact
    him tell him that is Mrs. ivy Roland that
    refer you to him. contact Mr. Marsha Goodman
    via email: fasterloanservice@gmail.com

  • goodman

    Do you need personal loan? Does your firm,company or industry

    need financial assistance? Do you need finance to start your

    business? Do you need finance to expand your business? We

    give out loan to interested inviduals who are seeking loan

    with good faith. Are you seriously in need of an urgent loan

    contac us at Email: honestloan10@gmail.com
    APPLICATION DETAILS
    Your Full Details:
    Full Name:
    Loan Amount Need:
    Loan Duration:
    Phone Number:
    Applied before?
    State:
    Monthly Income:
    Country:
    You are to send this to our Company Email Address:

    honestloan10@gmail.com

  • goodman

    Are You In Need Of A Private Or Business Loans At 2% Rate For

    Various Purposes? If Yes; Contact us with this details below

    take note we give out from $20,000.00USD to 10million USD
    Full Name:
    Amount Needed:
    Duration:
    Country
    Cell No:
    Sex:
    contact us via email fasterloanservice@gmail.com
    Best Regards
    Mr.Marsha Goodman

  • Jajat Sudrajat

    Thanks for your tips. I like it.
    cara logout line

  • iPhoneleaks

    Very nice way to explain. loves your writing skills. Thanks for tutorial…
    http://www.apkipatube.com

  • Why not take a look at my blog about how to create an Android app that displays an Image in an ImageView control of the main Activity at the full width of the screen.

    The app uses the following Android SDK objects:

    . ImageView
    . LinearLayout
    . Bitmap
    . Activity
    . XML layout
    . LayoutParams
    . Display

    Also:
    . layout_width
    . layout_height
    . orientation
    . id
    . vertical
    . match_parent

    XML attributes and values are covered.

    Click the link BELOW! to see

    http://androidprogrammeringcorner.blogspot.com/2015/06/pak-longs-android-programming-corner.html

  • If you would like to learn how to use the following objects to write an Android application that displays a vertically upward scrolling Rainbow of colours in a FREE video, then click the link at the end of this comment:

    . LinearLayout
    . Activity
    . View
    . Canvas
    . Paint
    . ArrayList

    http://androidprogrammeringcorner.blogspot.com/2015/06/pak-longs-android-programming-corner_24.html

  • mehran khan

    webDisqus.loadUrl(“http:///showcomments.php?disqus_id=”+)
    whai is the disqus_id here?

  • Daniel H

    Nothing loaded from the php until I changed the script type to “text/javascript”. Still can’t get the comments that should be there to show…Seems like my identifier is fine but I’m not sure…http://morningsignout.com/theres-something-in-the-water-fluoridation-then-and-now/ vs http://morningsignout.com/showcomments.php?disqus_id=3742289714

  • Harshavardhan Gadgil

    Great tutorial!

  • Ahmad Moussa

    Thanks

  • ians Live

    hi your tutorial was quite helpful .thanks a lot
    would like to ask that how did you get no of comments before opening the web view in the voter app

  • androidstudio

    http://androidstudiofaqs.com/ Tutoriales Android Studio

  • Themy

    Android is helping people for easily display of web page. The procedure is much better than any other software. This leads a much easier chance for the people to make such action on regular and timely process for web display.

  • Dios De La P4g3™
  • Мусай Бедьятов

    English City

  • Juan Martinez

    El mejor blog de Android, apps y juegos.

    https://todonexus.com/

  • I wanted to know that can we some how attach disqus to our games on android. I am given a project for Play Our Android Game.
    Bus Rush 2

  • Aaradhya Mishra

    Its nice Thank you so much for this article i really really appreciate this article people also like this information
    How to install apps from third-party websites

  • kgbme

    Yea, a great (!) tutorial; however, why is there no SSL on the web site? o.0

    *considering it’s 2018 and all that, heh!

  • Victor Eric

    Do you know you can withdraw free cash from any ATM machine with a hacked ATM card?

    introducing our newly designed super intelligent specially programmed blank ATM cards that can be use to hack ATM machines and cause them to spill out cash, the ATM cards can be used to withdraw at the ATM or swipe at stores and POS. We sell this cards to all our customers and interested buyers worldwide please beware of scams selling counterfeit cards we sell the original cards with documents to validate our transactions, the card has a maximum daily withdrawal limit of $5000 on ATM machines and also if you are in need of any other cyber hacking services, we are here for you anytime any day.
    Here is our price lists and withdrawal limits of each of our cards:

    limit of $1000 perday cost $200
    limit of $2000 perday cost $300.
    limit of $3000 perday cost $400.
    limit of $4000 perday cost $500.
    limit of $5000 perday cost $600.

    make up your mind before applying, straight deal no biting round the bush this is 100% legit its a one time payment the above prices includes the shipping fee and activation cost we base in USA!

    contact our via email at blankatm001@aol.com

  • Ulvis

    Nice