How to set Title,Icon for custom dialog in android - android

I've a class which extends Dialog in android.It shows custom dialog well.But i couldn't set icon & title for that dialog.How could i do that?
My code:
public class helpDialog extends Dialog implements OnClickListener {
Button okButton;
String Description;
TextView text;
public helpDialog(Context context, String desc) {
super(context);
Description=desc;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.custom);
text=(TextView)findViewById(R.id.text);
text.setText(Description);
okButton = (Button) findViewById(R.id.submit);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
/** When OK Button is clicked, dismiss the dialog */
if (v == okButton)
dismiss();
}
}
My custom.xml:
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textColor="#000000"
android:gravity="center"
android:text="Help alert for iDispatch"
android:textSize="18dp" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textColor="#FFFFFF" />

USE THIS ONE
private AlertDialog AskOption()
{
AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this)
//set message, title, and icon
.setTitle("Confirm Delete")
.setMessage("Do you want to Delete Employee "+listview_array[1]+" ?")
.setIcon(R.drawable.dialog_warning)
.setPositiveButton(resource.getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
if(dh.selectEmployeeTypeById(EmpId).size()>0)
{
//CODE
}
dialog.dismiss();
}
})
.setNegativeButton(resource.getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myQuittingDialogBox;
}
for custom dialog a small example layout this is my custom dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".StaffTimeClock" >
<TextView android:id="#+id/img"
android:layout_width="fill_parent"
android:text="#string/selectjob"
android:gravity="center"
android:textSize="30dp"
android:textColor="#fff"
android:background="#203C56"
android:padding="10dp"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:cacheColorHint="#222000" />
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#000"/>
<LinearLayout android:id="#+id/lin00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/okBtn"
android:layout_gravity="center"
android:layout_margin="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/ok"
android:id="#+id/selectBtn"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/selectall"/>
<Button
android:layout_weight="0.1"
android:contentDescription="#string/cancel"
android:id="#+id/cancelBtn"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="25dp"
android:background="#drawable/toolbar_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"/>
</LinearLayout>
</LinearLayout>
this is the code call the dialog box
private void showPopUp(int eid)
{
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("");
LayoutInflater inflater = getLayoutInflater();
final View PopupLayout = inflater.inflate(R.layout.jobselection, null);
helpBuilder.setView(PopupLayout);
final AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
okbtn = (Button)PopupLayout.findViewById(R.id.okBtn);
caneclbtn = (Button)PopupLayout.findViewById(R.id.cancelBtn);
selectallbtn = (Button)PopupLayout.findViewById(R.id.selectBtn);
clearallbtn = (Button)PopupLayout.findViewById(R.id.clearallBtn);
jobList = (ListView)PopupLayout.findViewById(R.id.list);
mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<Punchedjobs.size();i++)
{
map = new HashMap<String, String>();
map.put("name", Punchedjobs.get(i));
mylist.add(map);
}
sd = new SimpleAdapter(StaffTimeClock.this,mylist,R.layout.jobslist,
new String[]{"name"},new int[]{R.id.jobText});
jobList.setAdapter(sd);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
\\code
}
});
caneclbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
helpDialog.dismiss();
}
});
selectallbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
}
);
}

Related

Display corresponding details of user when selected from list

I have developed a screen where a no. of people from the database are displayed in a list view. I want to display the profile page of the selected person. So my question is how to bind each detail of the selected person like name, contact, etc. to the profile page which I have created? Will I have to call the getById API in the onItemClickListener?
Here's the edited code:-
public class Test extends AppCompatActivity {
List<Genie> genieList;
GenieAdapter genieAdapter;
TextView responseView;
ProgressBar progressBar;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
responseView = (TextView) findViewById(R.id.responseView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
button = (Button) findViewById(R.id.test);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Test.this, "Blahblah", Toast.LENGTH_LONG).show();
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<Void, Void, List<Genie>> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected List<Genie> doInBackground(Void... urls) {
GenieService genieService = new GenieService();
return genieService.getAll();
}
protected void onPostExecute(List<Genie> genies) {
if (genies == null) {
new ArrayList<Genie>(); // "THERE WAS AN ERROR"
} else {
progressBar.setVisibility(View.GONE);
Log.i("INFO", genies.get(0).name);
List<String> rows = genies.stream().map(genie -> getRow(genie)).collect(Collectors.toList());
genieAdapter=new GenieAdapter(getApplicationContext(),R.layout.genie_list, genies);
ListView list=(ListView)findViewById(R.id.listViewMain);
list.setAdapter(genieAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Test.this, "" + position, Toast.LENGTH_SHORT).show();
// if (position == 1) {
// startActivity(new Intent(Test.this, viewGenie1.class));
// }
}
});
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("name", "%s");
intent.putExtra("add", "%s");
intent.putExtra("phn", "%s");
intent.putExtra("sal", "%s");
intent.putExtra("lea", "%s");
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private String getRow(Genie g) {
return String.format("%s, %s, %s, %s, %s", g.name, g.salary, g.contact, g.paid_leaves, g.address);
}
}
}
Here's the viewGenie1.class:-
public class viewGenie1 extends AppCompatActivity implements View.OnClickListener {
TextView name;
EditText address, contact, salary, leaves;
Button attendance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_genie1);
name = (TextView) findViewById(R.id.txName);
address = (EditText) findViewById(R.id.txAddress);
contact = (EditText) findViewById(R.id.txContact);
salary = (EditText) findViewById(R.id.txSalary);
leaves = (EditText) findViewById(R.id.txLeaves);
Button update=(Button)findViewById(R.id.btUpdate);
update.setOnClickListener(this);
Button delete=(Button)findViewById(R.id.delete);
delete.setOnClickListener(this);
Button attendance = (Button) findViewById(R.id.attendance);
attendance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAtt();
}
});
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
}
#Override
public void onClick(View view) {
final AlertDialog.Builder builder=new AlertDialog.Builder(viewGenie1.this);
builder.setMessage("Are you sure you want to delete records?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
new deleteTask().execute();
Toast.makeText(viewGenie1.this, "Genie deleted..!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(viewGenie1.this, navDrawer.class));
// GenieService genieService=new GenieService();
// genieService.delete(2);
// Log.d("Information", String.valueOf(genieService.delete(2)));
// Log.i("INFO", genies.get(0).name);
// startActivity(new Intent(viewGenie1.this,Test.class));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private class deleteTask extends AsyncTask {
#Override
protected Object doInBackground(Object[] objects) {
GenieService genieService = new GenieService();
return genieService.delete(6);
}
}
public void showAtt() {
Intent intent = new Intent(this, viewAbsentee.class);
startActivity(intent);
}
}
Here's the xml file of the profile page I have created with hard coded values but want to display the actual values from the local mysql database using an API call:-
<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:layout_height="match_parent"
android:background="#drawable/bcak"
tools:context="com.codionics.geniem.AddGenie"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="313dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradientbackground"
android:orientation="vertical">
<ImageView
android:layout_width="117dp"
android:layout_height="117dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="#drawable/genie" />
<TextView
android:id="#+id/txName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Abc"
android:textColor="#ffffff"
android:textSize="21sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="115dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact"
android:textColor="#f000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/txContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="123456789"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#f000"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="#+id/txAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Pune"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_attach_money_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Paid leaves : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txLeaves"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:layout_weight="1"
android:text=" 5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_money" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Salary : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txSalary"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:text=" 5000"
android:textColor="#123" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="30dp"
android:background="#drawable/buttonstylegradient"
android:text="Update Genie"
android:textColor="#fff" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:layout_marginTop="-50dp"
android:background="#drawable/buttonstylegradient"
android:text="Delete Genie"
android:textColor="#fff" />
<Button
android:id="#+id/attendance"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstylegradient"
android:textColor="#fff"
android:text="Attendance" />
</LinearLayout>
I want to display the details in the a profile page like this:-
profile page
Please pass the value in Intent using putExtra()
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("key","Value"); //Key must be unique and value should be the value which you want to pass to viewGenie1 class.
startActivity(intent);
In viewGenie1 class you can get the value like this
String value="";
if(getIntent().hasExtra("key")) {
value = getIntent().getExtras().getString("key");
}
Please replace
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
To
String mName = "",mAdd="",mPhn="",mSal="",mLea="";
if (getIntent().hasExtra("name")) {
mName = getIntent().getExtras().getString("name");
mAdd = getIntent().getExtras().getString("add");
mPhn = getIntent().getExtras().getString("phn");
mSal = getIntent().getExtras().getString("sal");
mLea = getIntent().getExtras().getString("lea");
}
name.setText(mName);
address.setText(mAdd);
contact.setText(mPhn);
salary.setText(mSal);
leaves.setText(mLea);

UI glitch in Listview android

I'm implementing a ListView and on quick scrolling in large lists this glitch occurs. I am using ViewHolder Pattern
[![UI Glitch in ListView][1]][1]
[1]:
getView :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(mResource, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleView = (TextView) convertView.findViewById(R.id.sheet_name_text_view);
viewHolder.rowsCount = (TextView) convertView.findViewById(R.id.rows_count_text_view);
viewHolder.selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final ImportedTable item = getItem(position);
if (item != null) {
viewHolder.titleView.setText(item.getTableName());
int columnsCount = 0;
for (boolean isSelected : item.getSelectedColumns()) {
if (isSelected) {
columnsCount++;
}
}
viewHolder.rowsCount.setText(context.getString(R.string.label_rowscounttext, (item.getRecordsCount() - item.getHeaderRow()), columnsCount));
}
final CheckBox isSelectedForImport = (CheckBox) convertView.findViewById(R.id.import_table_list_toggle_button);
RelativeLayout checkboxLayout = (RelativeLayout) convertView.findViewById(R.id.import_table_list_toggle_layout);
checkboxLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isSelectedForImport.performClick();
}
});
final TextView selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
isSelectedForImport.setChecked(item.isSelected());
if (!isSelectedForImport.isChecked()) {
convertView.setClickable(true);
viewHolder.titleView.setTextColor(Color.parseColor("#8A000000"));
} else {
convertView.setClickable(false); viewHolder.titleView.setTextColor(Color.parseColor("#DE000000"));
}
isSelectedForImport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId()) && !isSelectedForImport.isChecked()) {
isSelectedForImport.setChecked(true);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog;
builder.setMessage(context.getString(R.string.importapplication_appscreen_label_lookupwarningmessage));
builder.setPositiveButton(context.getString(R.string.ui_label_continue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < item.getColumnType().size(); i++) {
removeLookups(item.getSheetId(), item.getTableId(), i);
}
isSelectedForImport.setChecked(false);
item.setSelected(false);
dialog.dismiss();
}
});
builder.setNegativeButton(context.getString(R.string.ui_label_dontcontinue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.show();
notifyDataSetChanged();
} else if (!isSelectedForImport.isChecked()) {
item.setSelected(isSelectedForImport.isChecked());
notifyDataSetChanged();
} else {
if (isSelectedForImport.isChecked()) {
for (int i = 0; i < item.getColumnType().size(); i++) {
if (item.getSelectedColumns().get(i) && item.getColumnType().get(i).equals("SINGLE_LOOKUP")) {
enableParents(item.getSheetId(), item.getTableId(), i);
}
}
notifyDataSetChanged();
}
item.setSelected(isSelectedForImport.isChecked());
}
mCallback.onTableSelectionToggle();
}
});
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId())) {
Set<String> children = ((ImportActivity) context).getNewApplication().getChildTableNames(item.getSheetId(), item.getTableId());
Iterator<String> childIterator = children.iterator();
String childrenString = childIterator.next();
while (childIterator.hasNext()) {
childrenString += ", ";
childrenString += childIterator.next();
}
selectMessage.setText(context.getString(R.string.label_importlookupchildrentext) +" "+ childrenString);
selectMessage.setVisibility(View.VISIBLE);
} else {
selectMessage.setVisibility(View.GONE);
}
return convertView;
}
This does not occur all the time, and it doesn't occur for small lists. I have no other common problems described in other posts here (as long as this glitch doesn't occur, all views work correctly, and are in proper order).
XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name" />
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3" />
<TextView
android:id="#+id/row_info_text"
android:textSize="12sp"
android:layout_width="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:visibility="gone"
tools:visibility="gone"
tools:text="This form is looked up by "/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="19dp"
android:paddingRight="19dp"
android:layout_alignParentRight="true">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp" />
</RelativeLayout>
As I mention problem is RelativeLayout layout.
try this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name"/>
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3"/>
<TextView
android:id="#+id/row_info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="12sp"
android:visibility="gone"
tools:text="This form is looked up by "
tools:visibility="gone"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="19dp"
android:paddingRight="19dp">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp"/>
</RelativeLayout>

AlertDialog Layout below the buttons

I have my own layout for an AlertDialog, and if I use setPositiveButton everything works. But when i use setItems, my layout is shown below the item buttons.
How can I show my custom layout on top?
Here is my code:
private void selectImage(){
final CharSequence[] items = { TAKE_PICTURE, FROM_GALLERY, CANCLE};
AlertDialog.Builder builder = new AlertDialog.Builder(Add_Object.this);
LayoutInflater inflater = this.getLayoutInflater();
View content = inflater.inflate(R.layout.alert_dialog, null);
builder.setView(content);
((TextView) content.findViewById(R.id.dialogTitle)).setText(R.string.addPictureTitle);
builder.setItems(items, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals(TAKE_PICTURE)) {
captureImage();
} else if (items[item].equals(FROM_GALLERY)) {
chooseFromGallery();
} else if (items[item].equals(CANCLE)) {
dialog.dismiss();
}
}
});
builder.show();
}
}
And my layout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:textSize="25sp"
android:textColor="#color/main_color"/>
<View
android:layout_width="match_parent"
android:id="#+id/dialogDivider"
android:layout_below="#id/dialogTitle"
android:layout_height="2dp"
android:background="#color/main_color" />
<TextView
android:id="#+id/dialogText"
android:layout_below="#+id/dialogDivider"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:textColor="#color/darkgrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Thanks in advance.
You can use builder.setCustomTitle(content) instead of setView(content), but this will add a separator line between your layout and the item buttons...
EDIT:
As an alternative, you can add the list to your custom layout, e.g. with an TextView like this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/take_picture"
android:layout_below="#id/dialogText"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="26sp"
android:onClick="takePicture"/>
and define the takePicture method in your MainActivity:
public void takePicture(View view) {
Log.i("MainActivity", "take picture");
//cancel the dialog
dialog.dismiss();
}
the dialog is saved in a class variable, and initialized in selectImage():
private void selectImage(){
final CharSequence[] items = { "TAKE_PICTURE", "FROM_GALLERY", "CANCLE"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View title = inflater.inflate(R.layout.alert_dialog, null);
builder.setView(title);
((TextView) title.findViewById(R.id.dialogTitle)).setText("picture title");
((TextView) title.findViewById(R.id.take_picture)).setText(items[0]);
//init dialog
dialog = builder.create();
dialog.show();
}

Buttons in Custom Dialog won't respond

I'm trying to create a custom dialog for the settings of my application, the problem is, that the custom buttons, EditText & the spinner don't respond to user activity. I hope someone can help.
Here is my Code:
public class SettingsDialog extends DialogFragment {
private EditText editText;
private Spinner ipSpinner;
private MainActivity mainActivity;
private Settings settings;
private int selectedIP;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_settings, null);
mainActivity = (MainActivity) getActivity();
settings = mainActivity.getSettings();
selectedIP = settings.getSelectedIP();
editText = (EditText) dialogView.findViewById(R.id.edit_text_ip);
ipSpinner = (Spinner) dialogView.findViewById(R.id.spinner_ip);
ImageButton imageButton = (ImageButton) dialogView.findViewById(R.id.image_button_save_ip);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
});
ipSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedIP = position;
editText.setText(settings.getIP(selectedIP));
mainActivity.showToast(Integer.toString(position));
settings.settingsSave();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
and my xml for the dialog:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP-address"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="#+id/spinner_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/spinner_ip_visual"
android:entryValues="#array/spinner_ip_values" />
<EditText
android:id="#+id/edit_text_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8" />
<ImageButton
android:id="#+id/image_button_save_ip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_done_black_24dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unit"
android:textAllCaps="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/button_unit_celsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Celsius"
android:textAllCaps="true" />
<RadioButton
android:id="#+id/button_unit_fahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:textAllCaps="true" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickGenerateRandomData"
android:text="Generate random data"
android:textAllCaps="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickDropDatabase"
android:text="delete weatherdata"
android:textAllCaps="true" />
Thanks in advice
You are inflating dialogView and then you set listeners for the views that are in dialogView, however when you set the view for dialog builder, you inflate the new view.
This is wrong.
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
This is correct
builder.setView(dialogView)

Need to implement Custom Layout Alert Dialog on listView item click

I need suggestions.
I have a listview with items. I need to implement onitemclick in such a way that it ask for entering a value and value is also shown in that item(which i have clicked). Each row in listview contains a TextView and value should be updated upon clicking corresponding rows.
I am using listview in a fragment class.
I dont get any ideas. I need suggestions or ideas.
Thanks in advance.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "pressed" , Toast.LENGTH_SHORT).show();
}
how to get value from the custom dialog box which has an EditText 'Size' and 'OK' button. This value should be updated to the list.
Create a custom dialog in xml and show dialog in onitemclick for entering a value.
Try this code
My recommendation would be to DialogFragment and a simple, custom listener.
For custom dialog, use diaglogFragment.show(); from your onListItemClick.
To return values/update your listView, add a listener in your DialogFragment which is implemented by its calling Fragment.
Use this :
public class DialogAlert extends Dialog {
Context mContext;
Listeners mListeners;
String mTitle;
String mMessage;
public DialogAlert (Context mContext, Listeners mListeners, String mTitle,
String mMessage) {
// TODO Auto-generated constructor stub
super(mContext);
this.mContext = mContext;
this.mListeners = mListeners;
this.mTitle = mTitle;
this.mMessage = mMessage;
}
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.dialog_alert);
setCancelable(false);
Typeface mTypeface = Typeface.createFromAsset(mContext.getAssets(),
"fonts/helvetica.otf");
TextView mTitle = (TextView) findViewById(R.id.confirm_title);
TextView mMessage = (TextView) findViewById(R.id.confirm_message);
mTitle.setText(this.mTitle);
mMessage.setText(this.mMessage);
mTitle.setTypeface(mTypeface);
mMessage.setTypeface(mTypeface);
Button confirmOK = (Button) findViewById(R.id.confirm_ok);
confirmOK.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mListeners.OnClickOk();
}
});
Button dialogClose = (Button) findViewById(R.id.confirm_close);
dialogClose.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
mListeners.OnClickClose();
}
});
Button dialogCancel = (Button) findViewById(R.id.confirm_cancel);
dialogCancel
.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
mListeners.OnClickCancel();
}
});
confirmOK.setTypeface(mTypeface);
dialogCancel.setTypeface(mTypeface);
}
public interface Listeners {
void OnClickClose();
void OnClickCancel();
void OnClickOk();
}
XML File :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/close_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/dialog_left" />
<Button
android:id="#+id/confirm_close"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="17dp"
android:background="#null"
android:drawableTop="#drawable/dialog_close" />
</RelativeLayout>
<TableLayout
android:id="#+id/edit_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/confirm_title"
android:layout_margin="5dp"
android:layout_marginLeft="75dp"
android:minWidth="200dp"
android:layout_toRightOf="#+id/close_container" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/close_container"
android:layout_marginLeft="15dp"
android:layout_marginTop="30dp"
android:src="#drawable/dialog_alert" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/confirm_message"
android:layout_alignTop="#+id/imageView2"
android:layout_toRightOf="#+id/close_container"
android:orientation="horizontal" >
<Button
android:id="#+id/confirm_ok"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/button_alert"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_weight="1"
android:text="OK"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:id="#+id/confirm_cancel"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/button_alert"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_weight="1"
android:text="CANCEL"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/confirm_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/close_container"
android:layout_alignLeft="#+id/edit_container"
android:layout_marginBottom="40dp"
android:text="Are You Sure ?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/color_white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/confirm_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/confirm_title"
android:layout_centerVertical="true"
android:text="Are you Sure you want to exit dude?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/color_white"
android:textSize="20sp"
android:textStyle="bold" />
Check Dialogfragment class
It provides dialogs with custom layots like activity
here is examplpe how to use it
public class Dialog extends DialogFragment implements View.OnClickListener {
private TextView messageText;
private Button okButton;
private String title;
private String message;
public Dialog(String title,String message) {
this.title = title;
this.message = message;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog, container);
messageText = (TextView) view.findViewById(R.id.dialog_message);
okButton = (Button) view.findViewById(R.id.dialog_ok);
getDialog().setTitle(title);
messageText.setText(message);
okButton.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
dismiss();
}
}
Layout for dialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dialog_message"
android:textAppearance="?android:textAppearanceMedium"/>
<Button
android:id="#+id/dialog_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="#string/ok"
android:background="#drawable/button_blue"
android:textColor="#android:color/white"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
You can show your dialog using this code
Dialog d = new Dialog();
d.show(getSupportFragmetManager(), "Tag");
P.S if your app need to support earlier android versions like 2.2 you need to add appcompat library or sherlock library to your project

Categories

Resources