0% found this document useful (0 votes)
52 views8 pages

Custom

The document contains code for an Android activity that allows a user to enter a name and phone number. It saves the entries to a custom MyData object and adds them to a MyDataListt adapter which populates a list view. There is an XML layout file for the activity, another for the list row items, and Java classes for the data model, list adapter, and main activity code.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views8 pages

Custom

The document contains code for an Android activity that allows a user to enter a name and phone number. It saves the entries to a custom MyData object and adds them to a MyDataListt adapter which populates a list view. There is an XML layout file for the activity, another for the list row items, and Java classes for the data model, list adapter, and main activity code.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Activity.

xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/EtName"
android:textSize="30sp"
android:textColor="#2b8962"
android:textColorHint="#2d937a"
android:hint="Name"
android:textAlignment="center"
android:drawableLeft="@drawable/ic_person_black_24dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/EtPhone"
android:textSize="30sp"
android:textColor="#2b8962"
android:textColorHint="#9f0f3f"
android:hint="Phone"
android:textAlignment="center"
android:drawableLeft="@drawable/ic_phone_android_black_24dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/BtSubmit"
android:text="Submit"
android:layout_gravity="center"
android:textSize="35dp"
android:paddingRight="30sp"
android:paddingLeft="30dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ListView
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</ListView>

</LinearLayout>
Mydatada list

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

<TextView
android:id="@+id/TvName"
android:textSize="30sp"
android:textColor="#2eab9c"
android:layout_marginTop="5dp"
android:textAlignment="center"
android:textStyle="bold"
android:background="@drawable/values"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/TvPhone"
android:textSize="30sp"
android:textColor="#111817"
android:layout_marginTop="5dp"
android:textAlignment="center"
android:textStyle="bold"
android:background="@drawable/values"
android:layout_weight="0.4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
package com.example.androidapps.jesmin11_08_2018;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyDataListt extends ArrayAdapter {

List list = new ArrayList();

public class DataHandler {


TextView DHName, DHPhone;
}

public MyDataListt(@NonNull Context context, int resource) {


super(context, resource);

@Override
public void add(@Nullable Object object) {
this.list.add(object);

super.add(object);
}

@Override
public int getCount() {
return this.list.size();

@Nullable
@Override
public Object getItem(int position) {
return this.list.get(position);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
parent) {
View view;
view = convertView;

DataHandler dataHandler;

if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)
this.getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);

view = layoutInflater.inflate(R.layout.mylistview, parent, false);

dataHandler = new DataHandler();

dataHandler.DHName = view.findViewById(R.id.EtName);

dataHandler.DHPhone = view.findViewById(R.id.EtPhone);

view.setTag(dataHandler);

}
else {
dataHandler = (DataHandler) view.getTag();
}
Mydata mydata;

mydata = (Mydata) getItem(position);

dataHandler.DHName.setText(mydata.getMDName());
dataHandler.DHPhone.setText(mydata.getMDPhone());

return view;

}
Mydata.java

package com.example.androidapps.jesmin11_08_2018;

import android.content.Context;

public class Mydata {

private String MDName,MDPhone;


private Context context;

public String getMDName() {


return MDName;
}

public void setMDName(String MDName) {


this.MDName = MDName;
}

public String getMDPhone() {


return MDPhone;
}

public void setMDPhone(String MDPhone) {


this.MDPhone = MDPhone;
}

public Context getContext() {


return context;
}

public void setContext(Context context) {


this.context = context;
}

public Mydata(String MDName, String MDPhone, Context context) {


setMDName(MDName);
setMDPhone(MDPhone);
setContext(context);
}
}
maiactivity.java

package com.example.androidapps.jesmin11_08_2018;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


EditText EtName,EtPhone;
Button BtSubmit;
ListView List;

MyDataListt myDataListt;
Mydata mydata;

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

EtName =findViewById(R.id.EtName);
EtPhone =findViewById(R.id.EtPhone);

BtSubmit =findViewById(R.id.BtSubmit);

List =findViewById(R.id.List);

myDataListt = new MyDataListt(getApplicationContext(),R.layout.mylistview);

List.setAdapter(myDataListt);

BtSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mydata = new
Mydata(EtName.getText().toString(),EtPhone.getText().toString(),getApplicationContext(
));

myDataListt.add(mydata);
myDataListt.notifyDataSetChanged();

EtName.setText (null);
EtPhone.setText(null);

}
});

}
}

You might also like