- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 4410
Simple "Hello World" dialog example in Android studio.
In my case I started new application, and left everything by default.
In content_main.xml (in my case it is located in \app\src\main\res\layout\content_main.xml) I added button:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="53dp" android:nestedScrollingEnabled="false" android:onClick="testMessage" />
Here notice "android:onClick="testMessage""
Then in MainActivity.java (in my case it is located in \app\src\main\java\com\example\smi\msearch\MainActivity.java) I created method like:
public void testMessage(View view) {
AlertDialog builder = new AlertDialog.Builder(MainActivity.this).create();
builder.setTitle("Yo!");
builder.setMessage("This is a test!");
builder.show();
}
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 4578
There is possibility in Android to set global exception handler. My code looks something like this:
public class BrowserActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(this));
}
}
}
Where CustomExceptionHandler can look like this:
public class CustomExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
//do something here
}
}
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 4521
For example, read version from JSON file. In build.gradle, in my case it is in: /app/build.gradle write something like:
def computeVersionName() {
def f1 = new File("app/src/main/assets/version.json");
def json = new JsonSlurper().parseText(f1.text)
assert json instanceof Map
return json.version
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId 'myId'
minSdkVersion 16
targetSdkVersion 22
versionCode 2
versionName computeVersionName()
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
Notice:
versionName computeVersionName()
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 5562
It seems that androids WebView doesn't like video tag.
Here is Android code which worked for me:
mWebView.setWebViewClient(new WebViewClient() {
// autoplay when finished loading via javascript injection
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()");
}
});
mWebView.setWebChromeClient(new WebChromeClient());
Where WebChromeClient we need to handle javascript methods, and it seems that autoplay in WebView doesn't work.
Also, mWebView is: private WebView mWebView; (I took example of WebView based application)
HTML code looks like:
<video autoplay loop> <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"> </video>