Top Menu

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 18 February 2015

Open any Website into Android Application

If you want to open any website in your custom view of Android application,
Here are the few steps to get it.

1. Create a new Project
2. Modify the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MergeRootFrame">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>
   
</FrameLayout>
3. Now open the MainActivity.java class
package com.example.webviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends Activity{
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webview);

        // Enable Javascript
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("http://www.fashionandyou.com/");

        // Stop local links and redirects from opening in browser instead of WebView
        webView.setWebViewClient(new MyWebViewClient());
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            super.onBackPressed();
        }
    }
}
4. Now create a new class MyWebViewClient.java
package com.example.webviewdemo;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebViewClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(Uri.parse(url).getHost().endsWith("fashionandyou.com")){
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}
5. Now add the INTERNET permission in AndroidMenifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
Read more...

Monday 9 February 2015

Show Hindi font (custom font) text in Android

In your custom apps you will most probably want to use your own font. This post will show you how you can do so. This is a simple example... create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case Devanagri.TTF). Then, if you want to apply that font to, say a TextView, do the following
TextView tv = (TextView ) findViewById(R.id.textView);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/hindi.ttf");
tv.setTypeface(face);
tv.setText("Hindi font");
Read more...

Friday 6 February 2015

Remove line in between two listView (or single listView)

There are different ways to achieve this, at least 2 different ways to do this in a ListView:

1. Set divider to null:
1.1. Programmatically
yourListView.setDivider(null);
1.2. XML
android:divider="@null" (this goes inside your ListView element)
2. Set divider to transparent and set its height to 0 to avoid adding space between listview elements:
2.1. Programmatically:
yourListView.setDivider(new ColorDrawable(android.R.color.transparent));
yourListView.setDividerHeight(0);
2.2. XML
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
 
developer.android.com: #ListView 
Read more...