Pages

Tuesday, April 30, 2013

Splash Screen and Webiview

public class Splash extends Activity { 
     @Override 
     public void onCreate(Bundle icicle) { 
          super.onCreate(icicle); 
          setContentView(R.layout.splashscreen); 
 mProgressBar=(ProgressBar)findViewById(R.id.progressBar1);

        final WebView wvc = (WebView)findViewById(R.id.webview); 
        wvc.getSettings().setJavaScriptEnabled(true); 
        wvc.setWebViewClient(new WebViewClient() { 
          @Override 
          public void onPageFinished(WebView view, String url) 
          { 
              /* Call whatever, It's finished loading. */ 
                  wvc.setVisibility(View.VISIBLE); 
   mProgressBar.setVisibility(view.GONE);
                  } 
      }); 
        wvc.loadUrl("http://www.google.de"); 

     } 



// LAYOUT FILE // 
 Initially show an ImageView and then once the WebView has loaded, swapping their visibility like this
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 



       <ImageView 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:scaleType="center" 
                android:src="@drawable/splash" 
                android:visibility="visible" 
        /> 

        <WebView android:id="@+id/webview" 
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent" 
            android:visibility="gone" 
         /> 

<ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center|bottom"/>

</FrameLayout> 

orr


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView weeb = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = weeb.getSettings();
webSettings.setJavaScriptEnabled(true);
weeb.setWebViewClient(new WebViewClient());
weeb.loadUrl(("http://www.facebook.com"));

}

Friday, April 5, 2013

Simple View Pager Example

1.Import library - Right click on project ,android tools, add support library.
2.Create an xml  (activity_main)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="Page 1" android:id="@+id/textViewHeader"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center" android:padding="10dip" android:textStyle="bold"></TextView>
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager" />

</LinearLayout>

3. Create a java file 
main activity 
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter adap = new MyPagerAdapter(this);
vp.setAdapter(adap);
final TextView tvHeader = (TextView) findViewById(R.id.textViewHeader);
tvHeader.setText("Page 1");

vp.setOnPageChangeListener(new OnPageChangeListener(){

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
tvHeader.setText("Page " + (arg0 + 1));
}
});
}
private class MyPagerAdapter extends PagerAdapter {
private ArrayList<LinearLayout> arry;

public MyPagerAdapter(Context context) {
arry = new ArrayList<LinearLayout>();
// arry.add(new ListView1Page(context));
arry.add(new TextViewPage(context));
// views.add(new ListView2Page(context));
arry.add(new ButtonPage(context));
// use this when design done graphically 
}
@Override
public void destroyItem(View view, int arg1, Object object) {
((ViewPager) view).removeView((LinearLayout) object);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return arry.size();
}
@Override
public Object instantiateItem(View view, int position) {
// View myView = views.get(position);
// ((ViewPager) view).addView(myView);
// return myView;
//
View v = new View(MainActivity.this.getApplicationContext());
       final LayoutInflater inflater = (LayoutInflater) MainActivity.this
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       
       switch (position) {
       case 0:
           v = inflater.inflate(R.layout.j,
                   (ViewGroup) null, false);
//            ((Button) v.findViewById(R.id.pp1btnbck)).setOnClickListener(new OnClickListener() {
//
//                @Override
//                public void onClick(View arg0) {
//                    finish();
//                }
//            });

           break;
       case 1:
           v = inflater.inflate(R.layout.k, null, false
// xml file named k                    );
           break;
       default:

          
           break;
       }
       ((ViewPager) view).addView(v, 0);

       return v;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
public Parcelable saveState() {
return null;
}
}

}


4 . If design is from xmls ,create xml files ,here i have used two xmls k and j.
put wateva u want in those files


5. If done dynamically , create a class , i have named this as ButtonPage

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ButtonPage extends LinearLayout {

public ButtonPage(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public ButtonPage(Context context) {
super(context);
init();
}

private void init() {
// setBackgroundColor(Color.RED);
// Button button = new Button(getContext());
// button.setText("4th Page");
//
// setGravity(Gravity.CENTER);
//
// LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
// LayoutParams.WRAP_CONTENT);
// addView(button, params);
}

}