Monday, April 30, 2012

Android custom listview with edit text,button and label

i want to make an custom list view in which there should be button edittext and textview and below listview there should be one button with text as Add New and when user click the Add New button the same widget that are in first row of listview should display in second row of list view and again when user click to Add New button same things should happen .
please help me with full xml and java code



<?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">
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>

<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="Time" android:layout_weight="1" android:id="@+id/btntime" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:text="" android:layout_weight="1" android:id="@+id/ettext"
android:layout_width="70dp" android:layout_height="wrap_content"></EditText>
<Button android:text="Record" android:layout_weight="1" android:id="@+id/btnRecord"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="alert" android:layout_weight="1" android:id="@+id/btnAlert"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

<Button android:layout_width="wrap_content"
android:layout_marginLeft="230dp" android:layout_height="wrap_content"
android:id="@+id/btnAddNew" android:text="Add new"></Button>
</LinearLayout>


java code



public class some extends ListActivity implements OnClickListener {
Button add_time, enter_text, record, alert_alarm,add_new;
TimePicker time_picker;
View vw;
AlertDialog.Builder alertdialog;
int mHour,mMinute;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.some);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.widgets, null, null, null);
this.setListAdapter(adapter);

add_time = (Button) findViewById(R.id.btntime);
add_time.setOnClickListener(this);
add_new=(Button) findViewById(R.id.btnAddNew);

add_new.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Toast.makeText(getApplicationContext(), "hi", 1000).show();


}
});
}

public void onClick(View v) {
showDialog(0);
}

@Override
protected Dialog onCreateDialog(int id)
{
alertdialog=new AlertDialog.Builder(this);
switch (id)
{
case 0:

LayoutInflater layout_inflater=getLayoutInflater();
vw=layout_inflater.inflate(R.layout.widgets, null);
alertdialog.setView(vw);
alertdialog.setIcon(R.drawable.icon);
alertdialog.setTitle("Select time");
time_picker=(TimePicker) vw.findViewById(R.id.timepicker);
time_picker.setIs24HourView(false);
alertdialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {

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

}
});
alertdialog.setPositiveButton("set", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which)
{
add_time=(Button) findViewById(R.id.btntime);
mHour=time_picker.getCurrentHour();
mMinute=time_picker.getCurrentMinute();
if (mHour>12)
{
add_time.setText((mHour-12)+":"+mMinute+" "+"PM");
}
if (mHour==12)
{
add_time.setText("12"+":"+mMinute+" "+"PM");
}
if (mHour<12)
{
add_time.setText(mHour+":"+mMinute+" "+"AM");
}

}
});
alertdialog.show();
}
return super.onCreateDialog(id);
}
}




No comments:

Post a Comment