Friday 19 June 2015

Custom Listview in android -2

No comments
In this chapter we learn how to implement custom Listview in android.Listview is one of the most popular controls, It can be used in many different forms and can be customized to suitable of app requirement.In this tutorial, I will demonstrate you how you can bind a simple array of Strings with ListView. 
May You Want To See Another Examples Of How To Create Simple Listview Part-1 and Part-2
Customlistview with same image Part-1
In this example we set a different image for all list items.

Let's go for coding:

CREATE NEW ANDROID PROJECT


We need for this example:

1) activity_main.xml (...res/layout/ )
2) mylist.xml (...res/layout/ )
3) MainActivity.java

4) CustomListAdapter.java

Step 1: Write code into activity_main.xml
This is a main activity where we put list view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Step 2: Write code into mylist..xml
Created custom view for listview to show with image and text in list view.

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#0000aa" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

Step 3: Write code into MainActivity.java

package dev.androidapplink.customlistviewapp_2;

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

public class MainActivity extends Activity {
// create variable
ListView list;
//define string for List item(s)
String[] itemname = { "pic1", "pic2", "pic3", "pic4", "pic5", "pic6",
"pic7", "pic8" };
//set image according to list item(s) from drawable
Integer[] imgid = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7,
R.drawable.pic8, };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create adapter and bind item name with image id
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
//implement on item click listener to show toast on click but you can put here your operation as your requirement of app
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
// make your operation here i make toast.

Toast.makeText(getApplicationContext(), Slecteditem,
Toast.LENGTH_SHORT).show();

}
});
}

}

Step 4: Write code into CustomListAdapter.java
CustomListAdapter.java is a custom list adapter class which provides data to list view.
package dev.androidapplink.customlistviewapp_2;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;

public CustomListAdapter(Activity context, String[] itemname,
Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub

this.context = context;
this.itemname = itemname;
this.imgid = imgid;
}

public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylist, null, true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description " + itemname[position]);
return rowView;
};

}
Step 5: Now Run Your Project:




Thursday 18 June 2015

Custom Listview in android-1

1 comment
In this chapter we learn how to implement custom Listview in android.Listview is one of the most popular controls, It can be used in many different forms and can be customized to suitable of app requirement.In this tutorial, I will demonstrate you how you can bind a simple array of Strings with ListView. 
May You Want To See Another Examples Of How To Create Simple Listview Part-1 and Part-2.
Customlistview with Different image Part-2.
In this example we set a same image for all list items.

Let's go for coding:


CREATE NEW ANDROID PROJECT


We need for this example:

1) activity_main.xml (...res/layout/ )
2) mylist.xml (...res/layout/ )
3) MainActivity.java

Step 1: Write code into activity_main.xml


This is a main activity where we put list view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Step 2: Write code into mylist.xml

Created custom view for list view to show with image and text in list view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView

        android:id="@+id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher" />

    <TextView

        android:id="@+id/Itemname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="4dp"
        android:textSize="18sp" />


</LinearLayout>

Step 3: Write code into MainActivity.java


package dev.androidapplink.customlistviewapp;


import android.os.Bundle;

import android.app.ListActivity;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

       
         //define string for List item(s)
String[] itemname = { "Android", ".NET", "JAVA", "FireFox",
"SQlite", "Orecale", "xmp", "Database" };

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
               
                //Bind custom list view by using ArrayAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.mylist,
R.id.Itemname, itemname));
}

}
Step 4: Now Run Your Project:


Download project:

Checkbox android example

No comments
In this example we learn about Checkbox. checkbox having a two type of states checked or unchecked.here we learn a simple example one is display result in Textview and second is display toaster.

Let's go for coding.


CREATE NEW ANDROID PROJECT


Step 1: Write code into activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView

        android:id="@+id/tvDetails"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fff" />

    <CheckBox

        android:id="@+id/cbAALink"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="https://Androidapplink.blogspot.in" />

    <CheckBox

        android:id="@+id/cbLike"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Like" />


</LinearLayout>

Step 2: Write code into MainActivity.java


package dev.androidapplink.checkboxapp;


import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

// create variables
TextView tvdetail;
CheckBox cbaalink, cblikes, cbimgicn;
OnClickListener checkBoxListener;

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// load control
cbaalink = (CheckBox) findViewById(R.id.cbAALink);
cblikes = (CheckBox) findViewById(R.id.cbLike);

checkBoxListener = new OnClickListener() {


@Override

public void onClick(View v) {
tvdetail = (TextView) findViewById(R.id.tvDetails);

if (cbaalink.isChecked()) {

 
// set message
tvdetail.setText("Following:");
 
// get string of check box + set message (tvdetail.setText("Following:");)
tvdetail.setText(tvdetail.getText().toString() + " "
+ cbaalink.getText().toString());
}
if (cblikes.isChecked()) {

// set message

tvdetail.setText("Yes I Like");
 
// display toast
 Toast.makeText(getApplicationContext(), 
                              "Thank You", Toast.LENGTH_LONG).show();
}

if (!cbaalink.isChecked() && !cblikes.isChecked()) {

tvdetail.setText("Happy To Help");// display when both check box is unchecked

}

}
};

cbaalink.setOnClickListener(checkBoxListener);

cblikes.setOnClickListener(checkBoxListener);

}


}

Step 3: Now Run Your Project:







Sunday 14 June 2015

Toggle button in android -1

No comments
In this example,we learn use of toggle button in android. the toggle button is a special class to render a button which has only two states,like " On / Off "," 0 / 1 ","Yes / On","True / false" etc.It’s best switching buttons to turn on or turn off a function.here i show you a very simple example to display current status on click toggle button.

CREATE NEW ANDROID PROJECT

Step 1: Write code into activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ToggleButton" >
    </ToggleButton>


</LinearLayout>

Step 2: Write code into MainActivity.java


package dev.androidapplink.togglebutton_2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

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

ToggleButton tgbutton;

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

tgbutton = (ToggleButton) findViewById(R.id.toggleButton1);
tgbutton.setOnClickListener(new OnClickListener() {

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

Toast.makeText(getApplicationContext(), "Toggle ON",
Toast.LENGTH_LONG).show();

} else {

Toast.makeText(getApplicationContext(), "Toggle OFF",
Toast.LENGTH_LONG).show();
}
}
});

// To set Toggle button checked/unchecked or default(true or false)
// tgbutton.setChecked(true);

}

}

Step 3: Now Run Your Project:




Toggle button in android -2

No comments
In this example,we learn use of toggle button in android. the toggle button is a special class to render a button which has only two states,like " On / Off "," 0 / 1 ","Yes / On","True / false" etc.It’s best switching buttons to turn on or turn off a function.let me explorer with you.here i covered two example which is displaying two different state of toggle button

import android.widget.ToggleButton;  is a special class,is allow to implement toggle button.

CREATE NEW PROJECT

Step 1: Write code into activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onToggleClicked"
        android:text="ToggleButton" />

    <TextView
        android:id="@+id/tview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:ems="10" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="ToggleButton"
        android:textOff="Turn ON"
        android:textOn="Turn OFF" />

    <TextView
        android:id="@+id/tview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:ems="10" />


</LinearLayout>

Step 2: Write code into MainActivity.java

package dev.androidapplink.togglebuttonapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

// create variable
ToggleButton button1;
ToggleButton button2;
TextView tview1;
TextView tview2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button1 = (ToggleButton) findViewById(R.id.toggleButton1);
button2 = (ToggleButton) findViewById(R.id.toggleButton2);
tview1 = (TextView) findViewById(R.id.tview1);
tview2 = (TextView) findViewById(R.id.tview2);

// Set message
tview1.setText("Button1 is OFF");
tview2.setText("Button2 is OFF");

// Implement state change listener
button2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Log.i("info", "Button2 is on!");
tview2.setText("Button2 is ON");
} else {
Log.i("info", "Button2 is off!");
tview2.setText("Button2 is OFF");
}
}
});
}

// set message according to toggle button state
public void onToggleClicked(View view) {
boolean on = ((ToggleButton) view).isChecked();
if (on) {
Log.i("info", "Button1 is on!");
tview1.setText("Button1 is ON");
} else {
Log.i("info", "Button1 is off!");
tview1.setText("Button1 is OFF");
}
}

}

Step 3: Now Run Your Project:





Popup menu | Drop down menu

33 comments
 In this tutorial we learn how to write a program for create a Drop down menu in android.It also known as Popup Menu. the Drop down menu is a one types of menu like Context menu,Option Menu etc. they can be used for settings like Delete,Search,Copy,Pest,Rename,Move,More etc.In this example we see how to create simple Drop down menu in android that contains various menu items.

Let's do fun with code:


CREATE NEW PROJECT
We need:

1) main.xml located here ...res/menu
1) activity_main.xml
2) MainActivity.java

 Step 1: Write code into main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/one"
        android:title="Rename"/>
    <item
        android:id="@+id/two"
        android:title="Delete"/>
    <item
        android:id="@+id/three"
        android:title="Copy"/>

</menu>

Step 2: write code into activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btclickme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me" />


</LinearLayout>

Step 3: Write code into MainActivity.java

package dev.androidapplink.custommenuapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity {
// create variable
Button btnclickme;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
btnclickme = (Button) findViewById(R.id.btclickme);

btnclickme.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// Create the instance of Menu
PopupMenu popup = new PopupMenu(MainActivity.this, btnclickme);
// Inflating menu using xml file
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());

// registering OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}

}

Step 4: Now Run Your Project:




Follow me Share