Thursday, 22 November 2012

Gallery with pagging dots

main.xml

         <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res       /android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        android:id="@+id/gallery_paging"
        >
           <Gallery android:id="@+id/mygallery01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:spacing="0px"
                android:fadingEdge="none"/>
             <LinearLayout android:id="@+id/image_count"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"          
                android:gravity="center"         
                android:layout_alignParentTop="true" >  
            </LinearLayout>          

</RelativeLayout>
ImageAdapter.java

       package com.gallerWithDotPagging;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageAdapter extends BaseAdapter {
    private Context context;

    public ImageAdapter(Context c) {
    // TODO Auto-generated constructor stub
    this.context = c;
    }
    public int getCount() {
    // TODO Auto-generated method stub
    return flowers.length;
    }
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView image = new ImageView(context);
    image.setImageResource(flowers[position]);
    image.setScaleType(ScaleType.FIT_XY);
    // image.setLayoutParams(new Gallery.LayoutParams(400, 400));
    return image;
    }
    int[] flowers = { R.drawable.image1, R.drawable.image2,
    R.drawable.image3,R.drawable.image4, R.drawable.image5};
    }

 GalleryActivity.java


    package com.gallerWithDotPagging;

import com.gallerWithDotPagging.R;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GalleryActivity extends Activity {
    Gallery gallery;
    ImageAdapter imageAdapter;
    LinearLayout count_layout;
    int count = 0;
    static TextView page_text[];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        count_layout = (LinearLayout) findViewById(R.id.image_count);
        gallery = (Gallery) findViewById(R.id.mygallery01);
        imageAdapter = new ImageAdapter(this);
        gallery.setAdapter(imageAdapter);
        count=gallery.getAdapter().getCount();
        page_text = new TextView[count];
        for (int i = 0; i < count; i++) {
            page_text[i] = new TextView(this);
            page_text[i].setText(".");
            page_text[i].setTextSize(45);
            page_text[i].setTypeface(null, Typeface.BOLD);
            page_text[i].setTextColor(android.graphics.Color.GRAY);
            count_layout.addView(page_text[i]);
        }
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                 for (int i = 0; i < count; i++) {
                   GalleryActivity.page_text[i].setTextColor(android.graphics.Color.GRAY);
                    }
                 GalleryActivity.page_text[position].setTextColor(android.graphics.Color.WHITE);
               
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
               
            }
        });
       
       }
}


Output:-




Wednesday, 21 November 2012

Loading a image from server to your ImageView

  Note:-Add this line in your manifest.xml file           <uses-permission android:name="android.permission.INTERNET"/>
   main.xml
   
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
          />

</RelativeLayout>


 ImageFromServer.java


 
    package com.dci.imagefromserver;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class ImageFromServer extends Activity {
   
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bitmap = DownloadImage(
        "http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
    ImageView img = (ImageView) findViewById(R.id.image);
    img.setImageBitmap(bitmap);
}

private InputStream OpenHttpConnection(String urlString)
throws IOException
{
    InputStream in = null;
    int response = -1;
           
    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
             
    if (!(conn instanceof HttpURLConnection))                   
        throw new IOException("Not an HTTP connection");
    
    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();               
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                               
        }                   
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");          
    }
    return in;   
}
private Bitmap DownloadImage(String URL)
{      
    Bitmap bitmap = null;
    InputStream in = null;      
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;
       }
   
}


Output


Side navigation like android facebook application

NavigationActivity.java 
package com.shekhar;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NavigationActivity extends Activity {
    /** Called when the activity is first created. */
    private boolean Menu_Displayed=false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Display display = getWindowManager().getDefaultDisplay();
        final int width = display.getWidth();
       
        // menu:
        LinearLayout li_menu = new LinearLayout(this);
        li_menu.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        li_menu.setOrientation(1);//1 is vertical
        li_menu.setBackgroundColor(Color.GREEN);
        TextView tv2 = new TextView(this); 
        tv2.setText("Menu Screen"); 
        tv2.setTextSize(30); 
        tv2.setTextColor(Color.BLACK);
        Button btn1 = new Button(this);
        btn1.setText("button 1");
        btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       
        li_menu.addView(btn1);
        li_menu.addView(tv2);
        //body:
        final HorizontalScrollView hsv = new HorizontalScrollView(this){
            @Override
            // do not let hsv consume the click itself. Then the view under the hsv will also consume the click
            //so that the menu will be clicked
            //when menu is not showed up, let hsv be the only view to consume the click.
            //so that the menu will not be clicked
            public boolean onTouchEvent(MotionEvent ev) {
                if(Menu_Displayed){
                    return false;
                }
                else{
                    return true;
                }
            }
        };
        hsv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        hsv.setBackgroundColor(Color.TRANSPARENT);
        hsv.setHorizontalFadingEdgeEnabled(false);
        hsv.setVerticalFadingEdgeEnabled(false);

        final LinearLayout li_body = new LinearLayout(this);
        li_body.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT)); 
        li_body.setOrientation(0);//0 is horizantal
        li_body.setBackgroundColor(Color.TRANSPARENT);

        hsv.addView(li_body);
       
        //body: place holder transparent
        TextView placeholder = new TextView(this);
        placeholder.setTextColor(Color.TRANSPARENT);
        placeholder.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT)); 
        placeholder.setVisibility(View.INVISIBLE);
        li_body.addView(placeholder);
      

        //body: real content
        LinearLayout li_content = new LinearLayout(this);
        li_content.setLayoutParams(new LayoutParams(width, LayoutParams.FILL_PARENT)); 
        li_content.setOrientation(1);//1 is vertical
        li_content.setBackgroundColor(Color.CYAN);

        TextView tv1 = new TextView(this); 
        tv1.setText("Main screen"); 
        tv1.setTextSize(40); 
        tv1.setTextColor(Color.BLACK); 

      
        //use this button to scroll
        Button btn_showMenu = new Button(this);
        btn_showMenu.setText("Menu");
        btn_showMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        btn_showMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hsv.post(new Runnable() {

                    @Override
                    public void run() {
                        if(Menu_Displayed){
                            hsv.smoothScrollTo(width-100, 0);
                        }
                        else{
                            hsv.smoothScrollTo(0, 0);
                        }
                        Menu_Displayed = !Menu_Displayed;
                    }
                });
            }
        });
        li_content.addView(btn_showMenu);
        li_content.addView(tv1);
      
       

        li_body.addView(li_content);

        //add menu and body in to frame
        FrameLayout mainFrame = new FrameLayout(this); 
        mainFrame.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        mainFrame.addView(li_menu); 
        mainFrame.addView(hsv); 

        //scroll to the body real content to block the menu
        hsv.post(new Runnable() {

            @Override
            public void run() {
                hsv.scrollBy(width-100, 0);            
            }
        });

        setContentView(mainFrame);
      
    }
}
Output

                                

Tuesday, 20 November 2012

How to add a image at the end of a multiline TEXTVIEW...

     Activity_main.xml    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5A9F2"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:id="@+id/text"
        android:text="i want to add a image from drawable at the end of a multiline TextView "
        />

</RelativeLayout>


     MainActivity.java
 package com.imageendoftext;
import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
      text=(TextView)findViewById(R.id.text);
      
      ImageSpan is = new ImageSpan(getApplicationContext(), R.drawable.btn);
     
      SpannableString texts = new SpannableString(text.getText().toString().concat(""));
       texts.setSpan(is,texts.length()-1,texts.length(),0);
      
       text.setText(texts);

    }
}

         

Output