Thursday 25 August 2011

How to Add Smiley/Emojis in Edittext?

Emoji.java :-


package com.android.emoji;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Emoji extends Activity implements OnClickListener {
    EditText edttxtemoji;
    Button btnsubmit, btnselectemoji;
    TextView txtviewdisplay;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        edttxtemoji = (EditText) findViewById(R.id.edttxtemoji);
        btnsubmit = (Button) findViewById(R.id.btnok);
        btnsubmit.setOnClickListener(this);
        btnselectemoji = (Button) findViewById(R.id.btnselect);
        btnselectemoji.setOnClickListener(this);
        txtviewdisplay = (TextView) findViewById(R.id.txtviewdisplay);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btnsubmit) {
            txtviewdisplay.setText(edttxtemoji.getText());
        } else if (v == btnselectemoji) {
            Intent in = new Intent(this, Select_Emoji_Activity.class);
            startActivity(in);
        }
    }

    CharSequence cs;
    Custom_List_Emoji cstlistemoji;
    int index;

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

        cstlistemoji = new Custom_List_Emoji(this);
        @SuppressWarnings("static-access")
        SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
                this.MODE_WORLD_READABLE);
        index = myPrefs.getInt("key1", 0);
        System.out.println("Pref Data index is:- " + index);
        ImageGetter imageGetter = new ImageGetter() {
            public Drawable getDrawable(String source) {
                Drawable d = getResources().getDrawable(
                        cstlistemoji.images[index]);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                return d;
            }
        };
        cs = Html.fromHtml(
                "<img src='"
                        + getResources()
                                .getDrawable(cstlistemoji.images[index])
                        + "'/>", imageGetter, null);
        System.out.println("cs is:- " + cs);
        edttxtemoji.setText(cs);
    }
}


Select_Emoji_Activity.java :-


package com.android.emoji;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Select_Emoji_Activity extends Activity implements OnItemClickListener{
    ListView lstviewemojis;
    Custom_List_Emoji cstlistemoji;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emojis);
        lstviewemojis=(ListView)findViewById(R.id.lstviewemojis);
        lstviewemojis.setOnItemClickListener(this);
        cstlistemoji=new Custom_List_Emoji(this);
        lstviewemojis.setAdapter(cstlistemoji);
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();
        System.out.println("Emoji is:- " +arg2);
        prefsEditor.putInt("key1", arg2);
        prefsEditor.commit();
        finish();
    }
}

Custom_List_Emoji :-


package com.android.emoji;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Custom_List_Emoji extends BaseAdapter {

    public Activity context;
    public LayoutInflater inflater;

    public Custom_List_Emoji(Activity context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return emojis.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        String temp = emojis[position];
        return temp;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    TextView txtview1;
    ImageView image;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.custom_emojis, null);
            txtview1 = (TextView) convertView.findViewById(R.id.txtviewemojis);
            image = (ImageView) convertView.findViewById(R.id.imgviewemoji);
        }

        image.setImageResource(images[position]);
        txtview1.setText(emojis[position]);
        return convertView;
    }

    public final int[] images = new int[] { R.drawable.emo_im_happy,
            R.drawable.emo_im_sad, R.drawable.emo_im_winking,
            R.drawable.emo_im_tongue_sticking_out, R.drawable.emo_im_surprised,
            R.drawable.emo_im_kissing, R.drawable.emo_im_yelling,
            R.drawable.emo_im_cool, R.drawable.emo_im_money_mouth,
            R.drawable.emo_im_foot_in_mouth, R.drawable.emo_im_embarrassed,
            R.drawable.emo_im_angel, R.drawable.emo_im_undecided,
            R.drawable.emo_im_crying, R.drawable.emo_im_lips_are_sealed,
            R.drawable.emo_im_laughing, R.drawable.emo_im_wtf };
    public final String[] _strEmoticonLabels = new String[] { "Happy", "Sad",
            "Winking", "Tongue sticking out", "Surprised", "Kissing",
            "Yelling", "Cool", "Money Mouth", "Foot in mouth", "Embarrased",
            "Angel", "Undecided", "Crying", "Lips are sealed", "Laughing",
            "Confused" };
    public final String[] emojis = new String[] { ":-)", ":-(", ";-)", ":-P",
            "=-O", ":-*", ":O", "B-)", ":-$", ":-!", ":-[", "O:-)", ":-\\",
            ":'(", ":-X", ":-D", "o_O" };

}




Main.xml :-


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content" android:text="Dipak"
        android:layout_height="wrap_content" android:id="@+id/txtviewdisplay" />
    <EditText android:layout_width="fill_parent" android:layout_below="@+id/txtviewdisplay"
        android:layout_height="wrap_content" android:id="@+id/edttxtemoji" />
    <Button android:layout_width="wrap_content" android:layout_below="@+id/edttxtemoji"
        android:layout_height="wrap_content" android:id="@+id/btnok"
        android:text="Submit" />
    <Button android:layout_width="wrap_content" android:layout_below="@+id/edttxtemoji"
        android:layout_height="wrap_content" android:id="@+id/btnselect"
        android:text="select Emojis" android:layout_toRightOf="@+id/btnok"/>
</RelativeLayout>


emojis.xml :-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ListView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/lstviewemojis" />
</RelativeLayout>


custom_emojis.xml :-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/mRlayout1">
        <ImageView android:layout_width="wrap_content" android:id="@+id/imgviewemoji"
            android:layout_height="wrap_content" android:src="@drawable/icon"
            android:layout_centerVertical="true" android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dip" />
        <TextView android:layout_width="wrap_content"
            android:layout_centerVertical="true" android:layout_marginRight="10dip"
            android:layout_height="wrap_content" android:text="Emoji"
            android:layout_alignParentRight="true" android:id="@+id/txtviewemojis" />
    </RelativeLayout>
</RelativeLayout>


Wednesday 10 August 2011

How to Play Video in Media Player without Display inbuilt Media Player Buttons?

Put below code into your Main Activity File.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setFixedSize(176, 144);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mediaPlayer = new MediaPlayer();
     
    String stringPath = "/sdcard/flying_kiss.mp4";
       
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.reset();
    }

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDisplay(surfaceHolder);
       
    try {
        mediaPlayer.setDataSource(stringPath);
        mediaPlayer.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mediaPlayer.start();
}


Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surfaceview"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <Button android:id="@+id/mBtnmore" android:layout_width="wrap_content"
        android:layout_height="35dip" android:text="More"
        android:layout_marginRight="5dip" android:layout_alignParentRight="true"
        android:layout_marginTop="8dip" />
    <Button android:id="@+id/mBtnhints" android:layout_width="wrap_content"
        android:layout_marginBottom="4dip" android:layout_height="35dip"
        android:text="Hints" android:layout_marginRight="5dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

Enjoy :-)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

How to Play mp4 Video in Android Using Remote URL ?

Add Below Code into your MainActivity.java file.

@Override
protected void onCreate(Bundle savedInstanceState)
     // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    try {
        setContentView(R.layout.videodisplay);
        String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view&amp; current=1376992942447_242.mp4";
        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri video = Uri.parse(link);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
    } catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
    }
}

Enjoy :-)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.