Friday 14 November 2014

Android Interface Use Example

How we can use interface in Android application.

create activity_main.xml

<?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="vertical" >
    
    <EditText 
        android:id="@+id/edtname"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:hint="Enter name"/>
    <Button 
        android:id="@+id/btnadd"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Add item"/>
    <ListView 
        android:id="@+id/lst"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@android:color/black"></ListView>


</LinearLayout>

then create lstraw.xml for listview dynamic raw

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Delete" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/button1"
        android:layout_centerVertical="true"
        android:text="TextView" />

</RelativeLayout>

below is MainActivity.java

package com.exampleinterfaceexample.act;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity{
ArrayList<String> myary;
ListView lst;
MyListAdapter mylstadp;
EditText edtname;
Button btnadd;
String strname;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lst = (ListView)findViewById(R.id.lst);
edtname = (EditText)findViewById(R.id.edtname);
btnadd = (Button)findViewById(R.id.btnadd);
myary = new ArrayList<String>();
myary.add("Android");
myary.add("Iphone");
myary.add("Windows");
myary.add("Blackberry");
myary.add("Ipad");
mylstadp = new MyListAdapter(getApplicationContext(), myary);
lst.setAdapter(mylstadp);
mylstadp.setListeners(new MyInterface() {
@Override
public void rowDeleted(Integer pos) {
// TODO Auto-generated method stub
lst.removeViewAt(pos);
mylstadp.notifyDataSetChanged();
}
});
btnadd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
strname = edtname.getText().toString();
if(!TextUtils.isEmpty(strname)){
myary.add(strname);
}
mylstadp.notifyDataSetChanged();
/*mylstadp = new MyListAdapter(getApplicationContext(), myary);
lst.setAdapter(mylstadp);*/
}
});
}

}

then create MyInterface.java class for interface

package com.exampleinterfaceexample.act;

public interface MyInterface {
   public abstract void rowDeleted(Integer pos);
}

then create listview adapter MyListAdapter.java

package com.exampleinterfaceexample.act;


import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MyListAdapter extends BaseAdapter {

Context mContext;
LayoutInflater layoutinflator;
ArrayList<String> araylistatende;
MyInterface myinterface;
public MyListAdapter(Context context, ArrayList<String> list) {
this.araylistatende = list;
this.mContext = context;
this.layoutinflator = LayoutInflater.from(context);
}

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

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

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

@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub

if (view == null) {
view = layoutinflator.inflate(R.layout.lstraw, null);
}
TextView txteventpasttype = (TextView) view.findViewById(R.id.textView1);
Button btn = (Button)view.findViewById(R.id.button1);
if(araylistatende.get(position) != null){
txteventpasttype.setText(araylistatende.get(position).toString());
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
araylistatende.remove(position);
notifyDataSetChanged();
}
});

return view;
}
// This is the interface method
public void setListeners(MyInterface  datasend)
{
myinterface = datasend;  
}
// This is the interface method
}

Now ,run the code.

This code is define the use of interface is android application.

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...