Thursday 16 February 2017

ANDROID - Custom ListView with Image and Text

Demo:

1)Custom ListView Activity:

package com.venky.customlistviewwithimageandtextview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class CustomListViewwithImageandTextViewActivity extends Activity {
      /** Called when the activity is first created. */
      final int[] my = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
                  R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
                  R.drawable.a8 };

      ListView lvList;

      String[] namephone;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            lvList = (ListView) findViewById(R.id.lvList);

            name = getResources().getStringArray(R.array.name);
            phone = getResources().getStringArray(R.array.phno);

            lvList.setAdapter(new MyAdapter(mynamephone,
                        CustomListViewwithImageandTextViewActivity.this));

      }
}  

                    

2)MyAdapter:

package com.venky.customlistviewwithimageandtextview;

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.LinearLayout;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
      String[] namephone;
      Context context;
      int[] my;

      public MyAdapter(int[] my, String[] name, String[] phone,
                  CustomListViewwithImageandTextViewActivity myContext) {
            // TODO Auto-generated constructor stub

            this.my = my;
            this.name = name;
            this.phone = phone;
            context = myContext;
      }

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

      @Override
      public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
      }

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

      @Override
      public View getView(int position, View v, ViewGroup parent) {
            // TODO Auto-generated method stub
            v = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.style,
                        null);
            TextView tvName = (TextView) v.findViewById(R.id.tvName);
            TextView tvPhone = (TextView) v.findViewById(R.id.tvPhone);
            ImageView ivImage = (ImageView) v.findViewById(R.id.ivImage);
            tvName.setText(name[position]);
            tvPhone.setText(phone[position]);
            ivImage.setImageResource(my[position]);
            return v;
      }

}

3)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>


4)style.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/tvPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="20dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


5)Add this code your strings.xml:

<string-array name="name">
        <item>Nagendram</item>
        <item>Srinivasa Rao</item>
        <item>Saraswathi</item>
        <item>Mounika</item>
        <item>Chandra Shekar</item>
        <item>Subba Rao</item>
        <item>Venkateswarlu</item>
        <item>Hari</item>
    </string-array>

    <string-array name="phno">
        <item>0123456789</item>
        <item>1234567890</item>
        <item>2345678901</item>
        <item>3456789012</item>
        <item>9876543210</item>
        <item>8765432109</item>
        <item>7654321098</item>
        <item>1237894560</item>
    </string-array>

ANDROID - Dialogs Demo


1)main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#9932CC"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnAlertDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="Alert Dialog" />

    <Button
        android:id="@+id/btnProgressDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Progress Dialog" />

    <Button
        android:id="@+id/btnDatePickerDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Date Picker Dialog" />

    <Button
        android:id="@+id/btnTimePickerDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Time Picker Dialog" />

</LinearLayout>

2)Activity:


package com.venkool;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class DialogsDemoActivity extends Activity {
      /** Called when the activity is first created. */

      Button btnAlertDialogbtnProgressDialogbtnDatePickerDialog,
                  btnTimePickerDialog;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnAlertDialog = (Button) findViewById(R.id.btnAlertDialog);

            btnProgressDialog = (Button) findViewById(R.id.btnProgressDialog);

            btnDatePickerDialog = (Button) findViewById(R.id.btnDatePickerDialog);

            btnTimePickerDialog = (Button) findViewById(R.id.btnTimePickerDialog);

            btnAlertDialog.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {

                        AlertDialog.Builder alert = new AlertDialog.Builder(
                                    DialogsDemoActivity.this);

                        alert.setMessage("Do you Want to Continuee ...")
                                    .setPositiveButton("Yes",
                                                new DialogInterface.OnClickListener() {

                                                      @Override
                                                      public void onClick(DialogInterface dialog,
                                                                  int which) {
                                                            // TODO Auto-generated method stub

                                                            Toast.makeText(getApplicationContext(),
                                                                        "You Clicked => Yes", 5000)
                                                                        .show();
                                                      }
                                                })
                                    .setNegativeButton("No",
                                                new DialogInterface.OnClickListener() {

                                                      @Override
                                                      public void onClick(DialogInterface dialog,
                                                                  int which) {
                                                            // TODO Auto-generated method stub

                                                            Toast.makeText(getApplicationContext(),
                                                                        "You Clicked => No", 5000)
                                                                        .show();
                                                      }
                                                }).show();

                  }

            });

            btnProgressDialog.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                        ProgressDialog progress = new ProgressDialog(
                                    DialogsDemoActivity.this);
                        progress.setMessage("Loading...");
                        progress.show();
                  }
            });

            btnDatePickerDialog.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Calendar c = Calendar.getInstance();
                        int cYear = c.get(Calendar.YEAR);
                        int cMonth = c.get(Calendar.MONTH);
                        int cDay = c.get(Calendar.DAY_OF_MONTH);

                        DatePickerDialog date = new DatePickerDialog(
                                    DialogsDemoActivity.thisdateListener, cYear, cMonth,
                                    cDay);
                        date.show();
                  }
            });

            btnTimePickerDialog.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                        Calendar c = Calendar.getInstance();
                        int cHour = c.get(Calendar.HOUR_OF_DAY);
                        int cMinute = c.get(Calendar.MINUTE);

                        TimePickerDialog time = new TimePickerDialog(
                                    DialogsDemoActivity.thistimeListener, cHour, cMinute,
                                    false);
                        time.show();
                  }
            });
      }

      OnDateSetListener dateListener = new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {

                  int month = monthOfYear + 1;

                  Toast.makeText(
                              DialogsDemoActivity.this,
                              "Selected Date is" + dayOfMonth + "-" + month + "-"
                                          + year, 5000).show();
            }
      };

      OnTimeSetListener timeListener = new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                  Toast.makeText(DialogsDemoActivity.this,
                              "Selected Time is :" + hourOfDay + ":" + minute, 5000)
                              .show();
            }
      };

}
Demo :