My custom dialog adds extra space after the Cancel and Save button. I have made a couple of custom dialogs in this app, but this problem appears only in this case. I've colored the view's background to blue, so I think it should be okay.
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/mo"/>
<CheckBox
android:id="#+id/cbMo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/tu"/>
<CheckBox
android:id="#+id/cbTu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/we"/>
<CheckBox
android:id="#+id/cbWe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/th"/>
<CheckBox
android:id="#+id/cbTh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="#string/fr"/>
<CheckBox
android:id="#+id/cbFr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:gravity="center_horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_marginRight="10dp"
android:text="#string/repeat_every"
android:textColor="#android:color/black"
android:gravity="right"
android:layout_gravity="center_vertical"/>
<Spinner
android:id="#+id/spRecurrence"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:prompt="#string/repeat_every"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:layout_marginRight="10dp"
android:text="#string/starts_on"
android:textColor="#android:color/black"
android:gravity="right"/>
<TextView
android:id="#+id/tvDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="#string/pick_a_date"
android:drawableRight="#drawable/ic_calendar"
android:gravity="center_vertical"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.2"
android:visibility="visible"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="right"
android:layout_marginRight="10dp"
android:textColor="#android:color/black"
android:text="#string/recurrences_colon"/>
<EditText
android:id="#+id/etNumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:inputType="number"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.3"
android:visibility="invisible"/>
</LinearLayout>
<TextView
android:id="#+id/tvError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_red_dark"
android:visibility="visible"
android:text="Error text"/>
</LinearLayout>
Displaying the dialog:
Context context = getContext();
final NewRecurrencesDialogView recurrencesDialogView = new NewRecurrencesDialogView(context, subject);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(recurrencesDialogView)
.setTitle(R.string.set_recurrence)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) { }
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
final Dialog dialog = builder.create();
dialog.setCancelable(false);
dialog.show();
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<Teaching> newRecurrences = recurrencesDialogView.saveRecurrences();
if (newRecurrences != null) {
recurrences.addAll(newRecurrences);
Collections.sort(recurrences, new Comparator<Teaching>() {
#Override
public int compare(Teaching lhs, Teaching rhs) {
if (lhs.getBegin() < rhs.getBegin())
return -1;
else
return 1;
}
});
updateListViewAdapter();
dataChangedListener.setChanged(true);
dialog.dismiss();
}
}
});
((AlertDialog)dialog).getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
I've compared the layout file with the other dialog's files, but can't figure out where the problem is.
I think the problem is with the android:layout_weight you set for your view.
Try change your:
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0.2"
android:visibility="visible"/>
To this:
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="visible"/>
I am not really sure what you trying to achieve, but think this will show you the direction (hopefully the right one).
Related
I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program
LeadActivity.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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LeadSalesActivity">
<LinearLayout
android:background="#E9ECEB"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/b_menusamping2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="#drawable/menu_button"
android:backgroundTint="#27D01B"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome,"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/t_username3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Rizaldi"
android:textColor="#android:color/black"
android:textSize="25sp"
android:textStyle="bold"
android:fontFamily="#font/action_man_bold"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_refresh2"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:backgroundTint="#27D01B"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#drawable/icon_update"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="("
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:textStyle="bold"
android:id="#+id/count_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=")"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/btn_add"
android:layout_marginRight="20dp"
android:backgroundTint="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah Lead"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telp"
android:textColor="#android:color/black"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="#+id/listviewlead"
tools:listitem="#layout/c_lead"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/back6"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
c_lead.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_tgl"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Tgl"
android:textSize="15sp"
android:textStyle="italic"
android:textColor="#000000" />
<TextView
android:id="#+id/t_lead4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Lead"
android:textSize="15sp"
android:textColor="#000000" />
<TextView
android:id="#+id/t_numberphone"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="right"
android:text="No.Telp"
android:textSize="15sp"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pencil_icon"/>
</LinearLayout>
<LinearLayout
android:id="#+id/btn_delete"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/delete_icon"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_alamat"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Alamat"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
MyAdapter
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_lead, null);
}
final HashMap<String, Object> hashMap = _data.get(_position);
final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);
String dateString = _data.get(_position).get("create_date").toString().replace("-","");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format1.parse(dateString);
String dateFinal = format2.format(date);
t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
} catch (ParseException e) {
e.printStackTrace();
}
t_lead.setText(_data.get((int)_position).get("type").toString());
t_alamat.setText(_data.get((int)_position).get("street").toString());
if (_data.get((int)_position).get("mobile").toString().equals(true)){
t_numberphone.setText("Tanpa Nomor");
} else {
t_numberphone.setText(_data.get((int)_position).get("phone").toString());
}
btn_edit.setFocusable(false);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
startActivity(i);
}
});
btn_delete.setFocusable(false);
btn_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
}
});
return _view;
}
Is there any mistake inside my program? How to solve this?
in the below code At a time both conditions are execution on click card view listener is executing every time .Edit and share option are not working properly. when I am click on cardview want to execute that one click.If I am click buttonViewOptions want to execute this onClick.
but every time cardview onClick only executing
private boolean isedit=false;
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// showPopupMenu(holder.buttonViewOption);
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_card, popup.getMenu());
// popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
isedit=true;
switch (item.getItemId()) {
case R.id.edit:
editItemClickListner.myEditItemClick(position);
return true;
case R.id.share:
//item.setVisible(false);
return true;
default:
}
return false;
}
});
popup.show();
}
});
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isedit=false;
clickListener.myItemClick(position);
}
});
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="#color/back_blue"
android:id="#+id/contact_card"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="center"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/card_details">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/cards"
android:background="#color/back_blue">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/slivergray"
app:cardCornerRadius="5dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/White">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:id="#+id/names">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/salutations"
android:layout_below="#+id/names">
<TextView
android:id="#+id/text_states"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/salutations"
android:paddingLeft="16dp"
android:text="salutationtype"
android:textColor="#color/gray"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/appImage"
android:padding="2sp"
android:layout_below="#+id/text_states"
android:textStyle="bold"
android:text="#string/first_name"
android:textColor="#color/gray"
android:textSize="18sp"
tools:ignore="RtlHardcoded"/>
<TextView
android:id="#+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/firstname"
android:layout_toRightOf="#+id/appImage"
android:padding="2sp"
android:textStyle="bold"
android:text="#string/last_name"
android:textColor="#color/gray"
android:textSize="18sp"
tools:ignore="RtlHardcoded"/>
</LinearLayout>
<TextView
android:id="#+id/textViewOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:textColor="#color/linecolor"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:text="⋮"
android:textAppearance="?android:textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/account_names"
android:layout_below="#+id/names"
android:weightSum="4">
<TextView
android:id="#+id/text_acc"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_below="#+id/bill_city"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Account Name"
android:textColor="#color/gray"
android:textSize="15sp"
/>
<TextView
android:id="#+id/account_name"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_spec"
android:layout_weight="2"
android:textStyle="bold"
android:textColor="#color/linecolor"
android:textSize="15sp"/>
</LinearLayout>
<!-- <TextView-->
<!-- android:id="#+id/facility_type"-->
<!-- style="#style/Base.TextAppearance.AppCompat.Subhead"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_below="#+id/account_name"-->
<!-- android:paddingLeft="16dp"-->
<!-- android:text="Facility Type"-->
<!-- android:paddingRight="16dp"-->
<!-- android:textColor="#color/gray"-->
<!-- android:textSize="15sp"/>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/streets"
android:layout_below="#+id/account_names"
android:weightSum="4">
<TextView
android:id="#+id/text_spec"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_below="#+id/bill_city"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Specialization"
android:textColor="#color/gray"
android:textSize="15sp"
/>
<TextView
android:id="#+id/specilization"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_spec"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/citys"
android:weightSum="4"
android:layout_below="#+id/streets">
<TextView
android:id="#+id/text_city"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/citys"
android:paddingLeft="16dp"
android:text="Job Title"
android:paddingRight="16dp"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp" />
<TextView
android:id="#+id/job_title"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_city"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/districts"
android:weightSum="4"
android:paddingBottom="3dp"
android:layout_below="#+id/citys">
<TextView
android:id="#+id/text_district"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/bill_district"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Contact Type"
android:layout_weight="2"
android:textColor="#color/gray"
android:textSize="15sp"
/>
<TextView
android:id="#+id/contact_type"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text_district"
android:layout_weight="2"
android:paddingRight="16dp"
android:textColor="#color/gray"
android:textSize="15sp"/>
</LinearLayout>
<!-- <TextView-->
<!-- android:id="#+id/textViewOptions"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_alignParentRight="true"-->
<!-- android:layout_alignParentTop="true"-->
<!-- android:paddingLeft="#dimen/activity_horizontal_margin"-->
<!-- android:text="⋮"-->
<!-- android:textColor="#color/gray"-->
<!-- android:layout_marginRight="10dp"-->
<!-- android:textAppearance="?android:textAppearanceLarge" />-->
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/slivergray"
android:layout_below="#id/districts"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:layout_below="#id/districts">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="#+id/email"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="ramesh#vshospital.com"
android:drawableLeft="#drawable/ic_email_black_24dp"
android:textColor="#color/gray"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="2"
android:layout_gravity="right">
<Button
android:id="#+id/mobile"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number"
android:drawableLeft="#drawable/ic_phone_black_24dp"
android:inputType="number"
android:textColor="#color/password"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<!-- </com.daimajia.swipe.SwipeLayout>-->
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
you issue is that you are setting two different listeners so the final one is excecuting.
it seems the you are using a RecyclerView so set your listeners in the ViewHolder class
public class MViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public View x;
public View y;
public MViewHolder(#NonNull View itemView) {
super(itemView);
x = itemView.findViewById(R.id.....);
y = itemView.findViewById(R.id.....);
x.setOnClickListener(this);
y.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.....:
// code
break;
case R.id....;
//code
break;
}
}`
I'm implementing a service and receiver in SettingsActivity and when i open the app for the first time there is no issue but once I visit SettingsActivity and come back, closing the navigation drawer from then on opens SettingsActivity everytime even if don't click any item in the listview
This is my MainActivity layout file where i have implemented navigation drawer :-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
tools:context=".MainActivity"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:id="#+id/drawer_layout">
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical">
<LinearLayout
android:id="#+id/top_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="#00AFF0"
android:orientation="vertical">
<TextView
android:id="#+id/ship_name"
android:layout_width="wrap_content"
android:minWidth="120dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#00AFF0"
android:background="#drawable/button_background"
android:padding="10dp"
android:textSize="16sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="10dp">
<ImageButton
android:id="#+id/date_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_reset"
android:background="#android:color/transparent"
android:layout_marginEnd="10dp"
android:visibility="gone"/>
<TextView
android:id="#+id/current_date"
android:layout_width="wrap_content"
android:minWidth="120dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/white"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="16sp"
/>
<ImageButton
android:id="#+id/date_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_date"
android:background="#android:color/transparent"
android:layout_marginStart="10dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tickets_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:background="#drawable/button_border"
android:gravity="center"
android:minWidth="120dp"
android:orientation="horizontal"
android:textColor="#android:color/white">
<TextView
android:id="#+id/ticket_no_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="end|center"
android:padding="2dp"
android:text=""
android:textColor="#FFFF00"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="2dp"
android:gravity="center_vertical"
android:text="#string/tickets"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="16sp" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/white" />
<TextView
android:id="#+id/no_of_people_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:gravity="end|center"
android:padding="2dp"
android:textSize="16sp"
android:textColor="#FFFF00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="2dp"
android:text="#string/people"
android:textSize="16sp"
android:textAllCaps="true"
android:gravity="center_vertical"
android:textColor="#android:color/white" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt_no_data"
android:gravity="center|center_horizontal"
android:textSize="18sp"
android:textColor="#color/textColor"
android:textStyle="bold"
android:visibility="gone"
android:paddingTop="15dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/search_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="#+id/ticket_no"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.8"
android:layout_marginStart="20dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:background="#drawable/edittextbackground"
android:imeOptions="actionDone"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:maxLength="19"
android:inputType="number"
android:digits="01234 56789"
android:maxLines="1"
/>
<Button
android:id="#+id/search_ticket_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginEnd="20dp"
android:layout_weight="0.2"
android:textColor="#android:color/white"
android:text="#string/go"
android:textSize="16sp"
android:textAllCaps="true"
android:background="#drawable/go_button_shape"/>
</LinearLayout>
<LinearLayout
android:id="#+id/or_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="30dp"
android:orientation="horizontal"
android:paddingRight="30dp">
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:background="#android:color/black"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"
android:text="#string/or"
android:textSize="20sp"
android:gravity="center"/>
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_weight="0.4"
android:layout_gravity="center_vertical"
android:background="#android:color/black"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/scan_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.5">
<Button
android:id="#+id/scanQr"
android:layout_width="wrap_content"
android:minWidth="120dp"
android:layout_height="40dp"
android:text="#string/scan_qr"
android:layout_gravity="center|top"
android:gravity="center"
android:textColor="#android:color/white"
android:background="#drawable/scan_button"
android:padding="10dp"
android:layout_marginTop="10dp"
android:textSize="16sp"
/>
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<TextView
android:id="#+id/scan_results"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="50dp"
android:layout_marginBottom="10dp"
android:visibility="visible"
android:textStyle="bold"
android:textSize="18sp"
android:gravity="left"
android:layout_marginStart="20dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:paddingStart="10dp"
android:paddingEnd="0dp"
/>
</LinearLayout>
<TextView
android:id="#+id/no_trips_tv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="18sp"
android:text="#string/no_trips"
android:visibility="gone"/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/layout_layer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:alpha="0.5"
/>
<ListView
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFF"
android:choiceMode="singleChoice"
/>
</android.support.v4.widget.DrawerLayout>
And in my MainActivity i handle item click events like this where navList is Listview and drawer is DrawerLayout :-
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
if (position == 0){
} else if(position == 1)
{
getTicketsInfo(Helper.getValueFromSharedPreferences("shipId",getApplicationContext()),currentDate);
} else if(position == 2) {
submitTickets();
} else if(position == 3) {
Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
} else {
Helper.removeKeyStoredinSharedPreferences("sessionHandle", getApplicationContext());
finish();
}
}
});
drawer.closeDrawer(navList);
}
});
remove drawer.setDrawerListener and place the code directly inside onItemClick
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case:0
{
break;
}
case:1
{
getTicketsInfo(Helper.getValueFromSharedPreferences("shipId",getApplicationContext()),currentDate);
break;
}
case:2
{
submitTickets();
break;
}
case:3
{
Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
break;
}
case:4{
Helper.removeKeyStoredinSharedPreferences("sessionHandle", getApplicationContext());
finish();
break;
}
default:
}
}
});
drawer.closeDrawer(navList);
}
I made an EditText OnFocusChangeListener that supposedly hides the cursor and another view when it's not in focus (i.e. clicking anywhere on the screen aside from the EditText will hide those items). Clicking on the EditText is the only time that will display them.
This is my OnFocusChangeListener for my EditText pageTitle:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
I also made an OnclickListener:
pageTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
}
});
The OnClickListener works but not the OnFocusListener. Ideally, pageTitle's cursor and saveCancelBar are initially hidden and will only appear when pageTitle is clicked.
This my XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:ignore="missingPrefix"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="app.wanderast.activity.AddPhotoActivity">
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal">
<TextView
android:id="#+id/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/arrow_left"
android:textSize="20sp"
android:clickable="true"
android:textColor="#color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_below="#id/toolbar"
android:orientation="vertical">
<EditText
android:id="#+id/title"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp"
android:background="#color/transparent"
android:maxLength="64"
android:privateImeOptions="nm"
android:inputType="textNoSuggestions|textMultiLine"
android:cursorVisible="false"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="16sp"
app:autoSizeMaxTextSize="24sp"
app:autoSizeStepGranularity="4sp"
android:textColor="#color/grey700"/>
<View
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:background="#color/grey700"
/>
<LinearLayout
android:id="#+id/sort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="#+id/sort_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:text="List View"
android:textColor="#color/grey700"
android:textSize="14sp"/>
<TextView
android:id="#+id/sort_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chevron_circle_down"
android:textColor="#color/grey700"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/add_photo_layout"
android:layout_marginTop="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<TextView
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="30dp"
android:background="#null"
android:minLines="0"
android:text="Add your first travel moment to your story"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/capture_photo_button"
android:layout_marginBottom="15dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/green_pill_thick">
<TextView
android:id="#+id/capture_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/camera"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/capture_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Moment"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/add_photo_button"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/blue_pill_thick">
<TextView
android:id="#+id/add_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/photo"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/add_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add from Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/list_view_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="50dp">
<ListView
android:id="#+id/list_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
</RelativeLayout>
</LinearLayout>
<!--Navbar-->
<LinearLayout
android:id="#+id/photo_story_navbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/navbar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingStart="15dp"
android:orientation="horizontal"
android:layout_marginEnd="10dp">
<!-- Take Photo Button -->
<LinearLayout
android:id="#+id/nav_capture_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/green_pill_button"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_capture_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/camera"
android:textColor="#color/green500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_capture_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
<!-- Add from Gallery Button -->
<LinearLayout
android:id="#+id/nav_gallery_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:background="#drawable/blue_pill"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_gallery_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/photo"
android:textColor="#color/blue500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_gallery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingEnd="15dp"
android:orientation="horizontal">
<!-- Review button -->
<LinearLayout
android:id="#+id/nav_review_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/red_500_pill"
android:layout_gravity="right"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_review_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/red500"
android:textAlignment="center"
android:textSize="12sp"/>
<TextView
android:id="#+id/nav_review_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/angle_double_right"
android:textColor="#color/red500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingStart="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/save_cancel_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/linearLayout"
android:layout_alignParentStart="true"
android:background="#drawable/black_border_top"
android:visibility="gone">
<RelativeLayout
android:id="#+id/save_title_btn"
android:clickable="true"
android:layout_width="84dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:background="#drawable/blue_pill"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="fill"
android:text="Save Title"
android:textSize="12sp"
android:textColor="#2196F3"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/cancel_title_btn"
android:layout_width="84dp"
android:layout_height="32dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="#drawable/grey_700_pill"
android:layout_marginRight="105dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cancel"
android:layout_gravity="fill"
android:textSize="12sp"
android:textColor="#616161" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
If you check the definition of onFocusChange(View v, boolean hasFocus) you will notice that your code must change like this:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
This is the code for checking the soft keyboard visibility in Android
public class MainActivity extends AppCompatActivity
{
public static String TAG = MainActivity.class.getSimpleName();
private TextView saveCancelBar;
private EditText pageTitle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListenerToRootView();
saveCancelBar = (TextView) findViewById(R.id.cancel_bar);
pageTitle = (EditText) findViewById(R.id.title);
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean hasFocus)
{
...
}
});
}
public void setListenerToRootView()
{
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = activityRootView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.e(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) {
// 0.15 ratio is perhaps enough to determine keypad height.
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard open", Toast.LENGTH_SHORT).show();
} else {
// keyboard is closed
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard closed", Toast.LENGTH_SHORT).show();
saveCancelBar.setVisibility(View.GONE);
}
}
});
}
}
I think you forgot to add focusableInTouchMode. Add focusableInTouchMode to your editText to get focused on touching the view.
android:focusableInTouchMode="true"
I've look in the android documentation on how to implement alertdialog with a xml layout
here is the documentation
when i run the program and click the todo button the program crashes can anyone help?
here's my source code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StartMoving extends Activity implements OnClickListener {
Button todo;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start_moving);
todo = (Button) findViewById(R.id.bTodo);
todo.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bTodo:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.to_do_list, null);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
break;
default:
break;
}
}
}
here's the xml file named to_do_list.xml
<?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"
android:weightSum="100" >
<ScrollView
android:id="#+id/svTips"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="87" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTipsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:text="#string/tips"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvtmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/two_months_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbSAP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_sort_and_purge"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_sap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_sap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_research"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_r_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_r_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbCAMB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_create_a_moving_builder"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_camb_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_camb_content" />
<TextView
android:id="#+id/tvswb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/six_weeks_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_ordersupplies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_order_supplies"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_os_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_os_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_use_it_or_lose_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_use_it_or_lose_it"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_uioli_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_uioli_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_take_measurement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_take_measurement"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_tm_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_tm_content" />
<TextView
android:id="#+id/tvomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/one_month_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_choose_your_mover_and_confirm_the_arragements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_choose_your_mover_and_confirm_the_arragements"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_cymacta_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_cymacta_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_begin_packing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_begin_paking"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_bp_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_bp_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_label"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_l_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_l_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_separate_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_separate_values"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_sv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_sv_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_do_a_change_of_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_do_a_change_of_address"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_dacoa_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_dacoa_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_notify_important_parties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_notify_all_important_parties"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_naip_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_naip_content" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="13"
android:orientation="horizontal" >
<Button
android:id="#+id/bReturntoTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go back to top" />
<Button
android:id="#+id/bgoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go to" />
</LinearLayout>
</LinearLayout>
this is what the logcat shows
use
Context mContext = v.getApplicationContext();
instead of
Context mContext = getApplicationContext();
or
builder = new AlertDialog.Builder(StartMoving.this);
EDIT :
You are reading button
todo = (Button) findViewById(R.id.bgoto); <-----------
^^^^^
todo.setOnClickListener(this);
AlertDialog.Builder diag = new AlertDialog.Builder(this);
diag.setTitle("DIALOG TITLE")
.setMessage("YOUR MESSAGE")
.setCancelable(false)
.setNegativeButton("WHAT EVER THE BUTTON TEXT",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enter code here
//do what ever the button in the dialog is clicked
dialog.cancel();
}
});
diag.show();
Easiest way to have a alert dialog
and dont forget to change the Button id in java to R.id.goto
if your are using alertDialog in TabHost then you have to write..
final AlertDialog alertDialog = new AlertDialog.Builder(**getParent()**).create();
//alertDialog.setTitle("Alert....");
alertDialog.setMessage("Your Text");
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
finish();
}
});
alertDialog.show();
It seems that I've been reading the button from the Context and not from the alertDialog. The solution was to create a View that will be set as View for the AlertDialog and declare a button and set it as a child for the View.
final View view;
LayoutInflater inf = LayoutInflater.from(StartMoving.this);
view = inf.inflate(R.layout.rename, null);
final EditText newname = (EditText) view.findViewById(R.id.etNewRoomName);
final Button todo = (BUtton) view.findViewById(R.id.bTodo);
new AlertDialog.Builder(StartMoving.this)
.setView(view)
.setTitle("Rename")
.setMessage("Enter new name for room " + currentRoom)
.show();