Fixes issue in the android twitter timeline article

This commit is contained in:
Julien Lengrand-Lambert
2015-02-28 11:34:54 +01:00
parent 0398ff16a9
commit 4efcebdf1f

View File

@@ -47,11 +47,14 @@ Then, I simply created a new Activity in my Application, that contains a webview
{% highlight java %}
package fr.lengrand.brestram;
import fr.lengrand.brestram.activities.BaseActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class TimeLineActivity extends BaseActivity {
public class MainActivity extends ActionBarActivity {
public static final String TAG = "TimeLineActivity";
private static final String baseURl = "https://twitter.com";
@@ -62,26 +65,32 @@ public class TimeLineActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timeline);
setContentView(R.layout.activity_main);
load_background_color();
WebView webView = (WebView) findViewById(R.id.timeline_webview);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(baseURl, widgetInfo, "text/html", "UTF-8", null);
}
/**
* Needed because android:background does not work
* for WebViews
*/
private void load_background_color() {
WebView webView = (WebView) findViewById(R.id.timeline_webview);
//webView.setBackgroundColor(getResources().getColor(R.color.twitter_dark));
webView.setBackgroundColor(0); // transparent
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
{% endhighlight %}
@@ -112,6 +121,12 @@ And here is the layout file :
I used fill_parent because I wanted to give as much space as possible to the Webview.
The last thing you want to do is make sure you are allowing your app to make web calls. For that, add this line in your Android Manifest.
{% highlight xml %}
<uses-permission android:name="android.permission.INTERNET" />
{% endhighlight %}
The result is far from perfect, but it's not that bad for roughly 30 minutes of work :).
<a href="{{ site.url }}/images/posts/2013/10/twitter_timeline.png"><img class="size-full wp-image-900" alt="twitter timeline embedded into an android application" src="{{ site.url }}/images/posts/2013/10/twitter_timeline.png" width="480" height="800" /></a>
@@ -123,3 +138,5 @@ Now, back to more complex features :).
<strong>Oh, and if you leave near Brest, check out <a title="brestram google play" href="https://play.google.com/store/apps/details?id=fr.lengrand.brestram&amp;hl=en" target="_blank">BresTram</a>!</strong>
<em>P.S: This is not exactly how the actual source code look like. I changed several things to highlight the essentials in this post.</em>
<em>EDIT : Edited after a request on 28th Feb. 2015.</em>