How can I Remove title from DialogFragment? - android

I have been trying for a while to remove the title of a DialogFragment, but several attempts have failed.I have made a DialogFragment which shows the profile of the user but it has a top title show. When I use getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); then the card that I have shown on the dialog CardView disappears and show the simple detail.
When I don't use getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); DialogFragment show like this
When use getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); DialogFragment show like this
I want the title to be removed and the background to remain.
Here is my Code
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (_main == null) _main = (MainActivity) getActivity();
context = _main;
View v = inflater.inflate(R.layout.fragprofile, container, false);
layProfile = (ConstraintLayout) v.findViewById(R.id.layProfile);
txtID = v.findViewById(R.id.txtID);
txtUserID = v.findViewById(R.id.txtBenutzername);
txtName = v.findViewById(R.id.txtName);
txtAdresse = v.findViewById(R.id.txtAdresse);
lineAdresse = v.findViewById(R.id.lineAdresse);
txtFocusOfWork = v.findViewById(R.id.txtFocusOfWork);
lineFocusOfWork = v.findViewById(R.id.lineFocusOfWork);
txtOnlineState = v.findViewById(R.id.txtOnlineState);
imgProfileBlurred = v.findViewById(R.id.imgProfileBlurred);
imgProfileCircle = v.findViewById(R.id.imgProfileCircle);
btnPhone = (TextView) v.findViewById(R.id.btnPhone);
linePhone = v.findViewById(R.id.linePhone);
btnEMail = (TextView) v.findViewById(R.id.btnEMail);
lineEMail = v.findViewById(R.id.lineEMail);
btnWhatsApp = (ImageView) v.findViewById(R.id.btnWhatsApp);
if (this.BenutzerName != null) {
try {
setValuesTextAndImage(this.BenutzerID, this.BenutzerName);
} catch (Throwable throwable) {
throwable.printStackTrace();
lib.ShowException(TAG, context, throwable, false);
}
}
Objects.requireNonNull(getDialog()).getWindow().setBackgroundDrawableResource(R.drawable.bg_round_corner);
return v;
}
XML
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/layProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
tools:context="de.com.limto.limto1.dlgProfile">
<ImageView
android:id="#+id/imgProfileBlurred"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/vn_logo_c02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:hint="#string/ID"
android:shadowColor="#000000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:textColor="#android:color/white"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/imgProfileCircle"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="4dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintEnd_toStartOf="#id/txtOnlineState"
app:layout_constraintHorizontal_bias="0.179"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/txtID"
app:shapeAppearanceOverlay="#style/roundedImageViewRounded"
app:srcCompat="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/txtOnlineState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="#string/onlinestate"
android:shadowColor="#000000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:textColor="#android:color/white"
android:textSize="16sp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#id/imgProfileBlurred"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/txtName"
app:layout_constraintVertical_bias="0.873" />
<TextView
android:id="#+id/txtBenutzername"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:hint="#string/Benutzername"
android:shadowColor="#000000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:textColor="#android:color/white"
app:layout_constraintBottom_toTopOf="#+id/txtName"
app:layout_constraintStart_toEndOf="#+id/imgProfileCircle"
app:layout_constraintTop_toTopOf="#+id/imgProfileCircle" />
<TextView
android:id="#+id/txtName"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/name2"
android:shadowColor="#000000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:textColor="#android:color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#+id/imgProfileCircle"
app:layout_constraintStart_toStartOf="#id/txtBenutzername"
app:layout_constraintTop_toBottomOf="#id/txtBenutzername" />
<TextView
android:id="#+id/txtAdresse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:drawablePadding="32dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="#string/Adresse"
android:textColor="#android:color/black"
android:textSize="16sp"
app:drawableStartCompat="#drawable/ic_adress"
app:layout_constraintTop_toBottomOf="#id/imgProfileBlurred" />
<View
android:id="#+id/lineAdresse"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#c0c0c0"
app:layout_constraintTop_toBottomOf="#id/txtAdresse" />
<TextView
android:id="#+id/txtFocusOfWork"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:drawablePadding="32dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="#string/Taetigkeitsschwerpunkte"
android:textColor="#android:color/black"
android:textSize="16sp"
app:drawableStartCompat="#drawable/ic_work"
app:layout_constraintTop_toBottomOf="#id/lineAdresse" />
<View
android:id="#+id/lineFocusOfWork"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#c0c0c0"
app:layout_constraintTop_toBottomOf="#id/txtFocusOfWork" />
<TextView
android:id="#+id/btnPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:drawablePadding="32dp"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:text="#string/phone"
android:textColor="#android:color/black"
android:textSize="16sp"
app:drawableStartCompat="#drawable/ic_phone"
app:layout_constraintStart_toStartOf="#id/txtFocusOfWork"
app:layout_constraintTop_toBottomOf="#id/lineFocusOfWork" />
<ImageView
android:id="#+id/btnWhatsApp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="#dimen/dialog_vertical_margin"
android:layout_marginLeft="#dimen/dialog_vertical_margin"
android:layout_marginTop="#dimen/dialog_horizontal_margin"
android:contentDescription="#string/whatsapp"
android:src="#drawable/cc"
app:layout_constraintBottom_toTopOf="#id/linePhone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="#id/btnPhone"
app:layout_constraintTop_toBottomOf="#id/lineFocusOfWork" />
<View
android:id="#+id/linePhone"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#c0c0c0"
app:layout_constraintTop_toBottomOf="#id/btnPhone" />
<TextView
android:id="#+id/btnEMail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:drawablePadding="32dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="#string/prompt_email"
android:textColor="#android:color/black"
android:textSize="16sp"
app:drawableStartCompat="#drawable/ic_email"
app:layout_constraintTop_toBottomOf="#id/linePhone" />
<View
android:id="#+id/lineEMail"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#c0c0c0"
app:layout_constraintTop_toBottomOf="#id/btnEMail" />
</androidx.constraintlayout.widget.ConstraintLayout>
R.drawable.bg_round_corner
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="20dp" />
</shape>

Create your dialog normally, and add a line like this before showing:
myDialog.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

Just give a little margin from the top in constraint layout and you will achieve the result as intended.

Related

RecyclerView catch catch onItemClickListener

I have 2 RecyclerViews that uses the same XML file to inflate views through the adapter.
One RecyclerView does catch clicks while the other doesn't.
The first RecyclerView is on another fragment and the code is similar to the second RecyclerView.
I have tried a lot of changes to the XML file(MaterialCardView) such as removing clickable attribute or changing the layout_weight to be 1 on the card but all of them didn't work.
Also, I can't understand why it would work only on 1 with the same settings on the other.
Here is the code for the second part which is not working:
Fragment - section of matter
list = view.findViewById(R.id.user_display_details_rv); //Getting the RecyclerView
list.setHasFixedSize(true);
int numOfColumns = 2;
list.setLayoutManager(new GridLayoutManager(getContext(), numOfColumns,GridLayoutManager.VERTICAL,false));
adapter = new UserDisplayDetailsAdapter(viewModel,getLayoutInflater());
list.setAdapter(adapter);
list.addItemDecoration(new RecyclerView.ItemDecoration() {
#Override
public void getItemOffsets(#NonNull Rect outRect, #NonNull View view, #NonNull RecyclerView parent, #NonNull RecyclerView.State state) {
outRect.set(5,20,5,20);
}
});
adapter.setOnItemClickListener((v,pos)->{
String pollId = Objects.requireNonNull(viewModel.getUserFilledPolls().get(pos).getPollId());
Navigation.findNavController(v).navigate(FragmentUserDisplayDetailsDirections.actionFragmentUserDisplayDetailsToFragmentOtherUserPoll(pollId,userId));
});
Adapter
public class UserDisplayDetailsAdapter extends RecyclerView.Adapter<UserDisplayDetailsHolder> {
UserDisplayDetailsViewModel viewModel;
LayoutInflater layoutInflater;
OnItemClickListener onItemClickListener;
public UserDisplayDetailsAdapter(UserDisplayDetailsViewModel viewModel, LayoutInflater layoutInflater) {
this.viewModel = viewModel;
this.layoutInflater = layoutInflater;
}
#NonNull
#Override
public UserDisplayDetailsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) MyApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.poll_list_square,parent,false);
return new UserDisplayDetailsHolder(view,onItemClickListener);
}
#Override
public void onBindViewHolder(#NonNull UserDisplayDetailsHolder holder, int position) {
Poll poll = Objects.requireNonNull(viewModel.getUserFilledPolls().get(position));
holder.bind(poll);
}
#Override
public int getItemCount() {
if(viewModel.getUserFilledPolls() == null){
return 0;
}
return viewModel.getUserFilledPolls().size();
}
public void setOnItemClickListener(OnItemClickListener listener){
this.onItemClickListener = listener;
}
}
Holder
public class UserDisplayDetailsHolder extends RecyclerView.ViewHolder {
MaterialTextView pollsName;
ShapeableImageView icon;
public UserDisplayDetailsHolder(#NonNull View itemView, OnItemClickListener onItemClickListener) {
super(itemView);
pollsName = itemView.findViewById(R.id.homescr_poll_pollName);
icon=itemView.findViewById(R.id.homescr_poll_icon);
itemView.setOnClickListener(v->{
int pos = getAdapterPosition();
onItemClickListener.onItemClick(v,pos);
});
}
public void bind(Poll poll) {
pollsName.setText(poll.getPollName());
}
}
Common XML
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
android:id="#+id/homescr_btn_poll"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_weight="1"
android:clickable="true"
android:focusable="true"
android:layout_width="110dp"
android:layout_height="90dp"
android:layout_gravity="center"
app:cardBackgroundColor="#color/primeOrng"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:strokeWidth="2dp"
android:theme="#style/Theme.MaterialComponents.Light">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="horizontal">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/homescr_poll_mainImage"
android:layout_width="60dp"
android:layout_height="40dp"
android:scaleType="fitStart"
app:strokeWidth="0dp"
app:srcCompat="#drawable/ic_poll" />
<Space
android:id="#+id/homescr_poll_space"
android:layout_width="10dp"
android:layout_height="wrap_content" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/homescr_poll_icon"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="end"
app:srcCompat="#drawable/ic_feed_arrow"
app:strokeWidth="0dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/homescr_poll_pollName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="sans-serif-medium"
android:text="#string/poll_name"
android:textAppearance="?attr/textAppearanceHeadline6"
android:textColor="#color/white"
android:textSize="14dp"
android:textStyle="bold" />
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/homescr_poll_doneImage"
android:layout_width="60dp"
android:layout_height="50dp"
android:visibility="gone"
app:srcCompat="#drawable/done_nobg" />
</com.google.android.material.card.MaterialCardView>
Working Fragment XML(Catching clicks on RecyclerView)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#color/black"
app:cardElevation="0dp"
app:strokeColor="#color/stroke_color">
tools:context=".FragmentHomeScreen">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/homeScreen_logo"
android:layout_width="306dp"
android:layout_height="160dp"
android:src="#drawable/logo__nobg_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/homeScr_text_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:fontFamily="sans-serif-black"
android:gravity="center"
android:text="Hello, "
android:textColor="#color/white"
android:textSize="32dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/homeScreen_logo" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/homeScr_text_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:fontFamily="sans-serif-black"
android:gravity="start"
android:text="User Name"
android:maxLines="1"
android:textColor="#color/white"
android:textSize="32dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/homeScr_text_title"
app:layout_constraintTop_toBottomOf="#+id/homeScreen_logo" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/home_layout_const"
android:layout_width="350dp"
android:layout_height="250dp"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:foregroundGravity="center_horizontal"
android:paddingLeft="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/homeScr_text_name">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/home_layout_poll_refresh"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/home_poll_rv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbarSize="3dp"
android:scrollbarThumbVertical="#color/white"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/homescr_btn_line"
android:layout_width="350dp"
android:layout_height="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="90dp"
android:backgroundTint="#978F8F"
app:cornerRadius="100px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="#+id/homescr_btn_feed"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/black"
android:drawableTop="#drawable/ic_feed"
android:fontFamily="sans-serif-medium"
android:text="FEED"
android:textColor="#color/primeOrng"
android:textSize="14dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/homescr_btn_map"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/homescr_btn_line"
app:strokeColor="#color/black"
app:strokeWidth="2dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/homescr_btn_map"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/black"
android:drawableTop="#drawable/ic_map"
android:fontFamily="sans-serif-medium"
android:text="#string/map"
android:textColor="#color/primeOrng"
android:textSize="14dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/homescr_btn_feed"
app:layout_constraintTop_toBottomOf="#+id/homescr_btn_line"
app:strokeColor="#color/black"
app:strokeWidth="2dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
NOT Working Fragment XML(NOT Catching clicks on RecyclerView)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="#color/black"
android:scrollbars="vertical"
android:scrollbarSize="3dp"
android:scrollbarStyle="insideInset"
tools:context=".feed.FragmentUserDisplayDetails">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/user_display_details_img_main"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.CornerSize50Percent"
app:srcCompat="#drawable/avatar"
app:strokeColor="#color/white"
app:strokeWidth="1dp" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/user_display_details_txt_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fontFamily="sans-serif-black"
android:gravity="center"
android:text="User Name"
android:textColor="#color/white"
android:textSize="32dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_display_details_img_main" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/user_dusplay_details_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_display_details_txt_username">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/user_display_details_txt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:drawableStart="#drawable/ic_account"
android:drawablePadding="10dp"
android:fontFamily="sans-serif-light"
android:gravity="left"
android:paddingLeft="20dp"
android:text="#string/email"
android:textColor="#color/white"
android:textSize="28dp"
android:textStyle="bold" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/user_display_details_txt_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:drawableStart="#drawable/ic_white_location"
android:drawablePadding="10dp"
android:fontFamily="sans-serif-light"
android:gravity="left"
android:maxLines="2"
android:paddingLeft="20dp"
android:text="address"
android:textColor="#color/white"
android:textSize="28dp"
android:textStyle="bold" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/user_display_details_txt_education"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:drawableStart="#drawable/ic_education"
android:drawablePadding="10dp"
android:fontFamily="sans-serif-light"
android:gravity="left"
android:paddingLeft="20dp"
android:text="education"
android:textColor="#color/white"
android:textSize="28dp"
android:textStyle="bold" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/user_display_details_txt_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:drawableStart="#drawable/sex_icon"
android:drawablePadding="10dp"
android:fontFamily="sans-serif-light"
android:gravity="left"
android:paddingLeft="20dp"
android:text="SEX"
android:textColor="#color/white"
android:textSize="28dp"
android:textStyle="bold" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/user_display_details_poll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/textviewbg">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/user_display_details_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.button.MaterialButton
android:id="#+id/feed_back_btn"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="30dp"
android:backgroundTint="#color/black"
android:fontFamily="sans-serif"
android:text="BACK "
android:textSize="20sp"
app:cornerRadius="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_dusplay_details_layout"
app:strokeColor="#color/primeOrng"
app:strokeWidth="1dp" />
<ProgressBar
android:id="#+id/user_display_details_progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

EditText.getText().toString() is not returning current value

I´m trying to get the text that was inserted into a EditText in the UI.
The UI looks like this. In a fragment the EditText for name has a default value "Hello".
After the user has entered a new value (for example "Hello2") I´d like to get the new value when the user clicks the Add Button.
But what I recive is still the default value "Hello".
My Code looks like this:
XML
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Name"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_addingredient"
app:layout_constraintVertical_bias="0.050" />
JAVA Code in Fragment
public class AddIngredient extends Fragment{
//Controls
public EditText etN;
public EditText etK;
public EditText etF;
public EditText etC;
public EditText etP;
public TextView tv1;
//ViewModel
AddIngredientViewModel model;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_addingredient, container, false);
// Get the values
tv1 = rootView.findViewById(R.id.textview_addingredient);
etN = rootView.findViewById(R.id.editText_name);
etK = rootView.findViewById(R.id.editText_kcal);
etF = rootView.findViewById(R.id.editText_fat);
etC = rootView.findViewById(R.id.editText_carbs);
etP = rootView.findViewById(R.id.editText_protein);
// Inflate the layout for this fragment
return rootView;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.fragmentbutton_addingredient).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Get current values
String name = etN.getText().toString();
int kcal = Integer.parseInt(etK.getText().toString().trim());
int fat = Integer.parseInt(etF.getText().toString().trim());
int carbs = Integer.parseInt(etC.getText().toString().trim());
int protein = Integer.parseInt(etP.getText().toString().trim());
model = new ViewModelProvider(requireActivity()).get(AddIngredientViewModel .class);
Ingredient ingredient = new Ingredient(name, kcal, fat, carbs, protein){};
model.SaveIngredient(ingredient);
}
});
}
}
So has anyone an idea, what I have tho change to recive the current value? Thanks!
-- EDIT --
Entire XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#color/screen_background"
tools:context=".fragments.AddIngredient">
<TextView
android:id="#+id/textview_addingredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Add Ingredient"
android:textColor="#color/headercolor"
android:textSize="#dimen/header_fontsize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.126"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!-- Name -->
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Name"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_addingredient"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
android:text="Hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_name"
app:layout_constraintVertical_bias="0.0" />
<!-- Kcal -->
<TextView
android:id="#+id/textView_kcal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Kcal"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_name"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_kcal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_kcal"
app:layout_constraintVertical_bias="0.0" />
<!-- Fat -->
<TextView
android:id="#+id/textView_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Fat"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_kcal"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_fat"
app:layout_constraintVertical_bias="0.0" />
<!-- Carbs -->
<TextView
android:id="#+id/textView_carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Carbs"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_fat"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_carbs"
app:layout_constraintVertical_bias="0.0" />
<!-- Protein -->
<TextView
android:id="#+id/textView_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Protein"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_carbs"
app:layout_constraintVertical_bias="0.052" />
<EditText
android:id="#+id/editText_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_protein"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/fragmentbutton_addingredient"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/button_background"
android:gravity="left|center_vertical"
android:padding="20dp"
android:text="Add"
android:textColor="#color/button_foreground"
android:textSize="#dimen/button_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.243"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_protein"
app:layout_constraintVertical_bias="0.250" />
</androidx.constraintlayout.widget.ConstraintLayout>
Remove the default static text set to the editTextName field in your xml layout file. Due to this, the value is always getting stored as Hello. You shouldnt set text in EditTextview.
<EditText
android:id="#+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
android:text="Hello" //Remove this line.

Android: Custom List View is not working properly

My apologies for not providing a proper source code.
basically what I have in my list view xml layout is:
listleave.xml
leaveSummary.xml
leavedB
Custom Adapter
ApplyLeave
I'm creating an app where I have a list view and when I'm running my app I'm getting an error in Locgat.
Caused by:java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null
object reference
at com.example.huzai.civilsoft1.ApplyLeave.loadDataInListView(ApplyLeave.java:258 )
at com.example.huzai.civilsoft1.ApplyLeave.onCreate(ApplyLeave.java:84)
In the line loadDataInListView when I'm not commenting l1.setAdapter(customAdapter); the app crashes but when I'm commenting it works properly.
Result of not commenting: l1.setAdapter(customAdapter);
ApplyLeave Activity
public class ApplyLeave extends AppCompatActivity {
LeaveService myDb;
//Declaration EditTexts
EditText editstartDate;
EditText editendDate;
DatePickerDialog datePickerDialog;
CheckBox checkgoingAbroad;
Spinner spinnertypess;
Spinner editleavess;
EditText editremarks;
EditText editnoOfDays1;
TextView noOfDays;
TextView endd;
Button btnApply;
Button addfield;
Button deletefield;
LinearLayout parentlinearlayout;
ListView l1;
ArrayList<Leave> arrayList;
CustomAdapter customAdapter;
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply_leave);
myDb = new LeaveService (this);
checkgoingAbroad =(CheckBox) findViewById(R.id.GoingAbroad);
spinnertypess = (Spinner)findViewById(R.id.types);
editleavess = (Spinner)findViewById(R.id.Leaves);
editremarks = (EditText)findViewById(R.id.reason);
editnoOfDays1 = (EditText)findViewById(R.id.noOfDays);
parentlinearlayout =
(LinearLayout)findViewById(R.id.parent_linear_layout);
addfield = (Button)findViewById(R.id.addField);
deletefield = (Button)findViewById(R.id.removeField);
endd = (TextView)findViewById(R.id.date);
l1 = (ListView)findViewById(R.id.leaveApplicationSummary);
btnApply = (Button)findViewById(R.id.apply);
AddData();
arrayList = new ArrayList<>();
loadDataInListView();
//Created a method for loadDataInListView
private void loadDataInListView() {
arrayList = myDb.getAllData();
customAdapter = new CustomAdapter(this,arrayList);
l1.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}
private void AddData() {
btnApply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData("", checkgoingAbroad.toString(), spinnertypess.toString(), editleavess.toString(), editnoOfDays1.getText().toString(), editstartDate.getText().toString(), editendDate.getText().toString(), editremarks.getText().toString());
if (isInserted == true){
Toast.makeText(ApplyLeave.this, "Data Inserted Successfully",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ApplyLeave.this, "Data Not Inserted",Toast.LENGTH_LONG).show();
}
}
});
}
Result of commenting: //l1.setAdapter(customAdapter);
CustomAdapter class
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<Leave> arrayList;
public CustomAdapter(Context context, ArrayList<Leave>arraylist) {
this.context = context;
this.arrayList = arraylist;
}
#Override
public int getCount() {
return this.arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.leavesummary, null);
TextView types = (TextView)convertView.findViewById(R.id.textViewtypes);
TextView startDate = (TextView)convertView.findViewById(R.id.textViewstartDate);
TextView endDate = (TextView)convertView.findViewById(R.id.textViewendDate);
TextView leavetype = (TextView)convertView.findViewById(R.id.textViewleaveType);
TextView noOfDays = (TextView)convertView.findViewById(R.id.textViewnoOfDays);
TextView reason = (TextView)convertView.findViewById(R.id.textViewreason);
Leave leave = arrayList.get(position);
types.setText(leave.getTypes());
startDate.setText(leave.getStartDate());
endDate.setText(leave.getEndDate());
leavetype.setText(leave.getLeaves());
noOfDays.setText(leave.getNoOfDays());
reason.setText(leave.getReason());
return convertView;
}
}
listleave.xml Over here I'm using ConstraintLayout
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/leaveApplicationSummary">
</ListView>
</android.support.constraint.ConstraintLayout>
leaveSummary.xml
<android.support.constraint.ConstraintLayout
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/background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leave Application Summary"
android:textColor="#080808"
android:textSize="20dp"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="72dp"
android:text="Applicant Name :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewApplicantName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginLeft="188dp"
android:layout_marginTop="72dp"
android:text="Huzaifa [001]"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="116dp"
android:text="Type of Leave :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewtypes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="116dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="164dp"
android:text="Leave Start Date :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewstartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginLeft="188dp"
android:layout_marginTop="164dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="208dp"
android:text="Rejoining Date :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewendDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="208dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="248dp"
android:text="Leave Type :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewleaveType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="180dp"
android:layout_marginLeft="180dp"
android:layout_marginTop="248dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="288dp"
android:text="Total Days Applied :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewnoOfDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="216dp"
android:layout_marginLeft="216dp"
android:layout_marginTop="288dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="332dp"
android:text="Remarks :"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewreason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginLeft="144dp"
android:layout_marginTop="332dp"
android:text="TextView"
android:textColor="#080808"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_margin="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete" />
<Button
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Back" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
apply_leave.xml
<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/background"
tools:context=".ApplyLeave">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_linear_layout"
android:layout_margin="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/GoingAbroad"
android:hint="Going Abroad"
android:inputType="text"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Need Advance Payment?"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/types"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/types"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Leave Type"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/Leaves"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/leaves"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/noOfDays"
android:hint="No. of Days"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/addField"
android:text="+"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/removeField"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Leave starts on" />
<EditText
android:id="#+id/startDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:editable="false"
android:hint="Select" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rejoining will be on" />
<EditText
android:id="#+id/endDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:editable="false"
android:hint="Select" />
</LinearLayout>
<EditText
android:id="#+id/reason"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Remarks" />
<TextView
android:id="#+id/days"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total leave days = 0"/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Leave ends on = "/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:gravity="bottom">
<Button
android:id="#+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
<Button
android:id="#+id/apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Apply" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please tell me where I'm going wrong.
You are getting null l1.
Confirm your id of ListView in your activity_apply_leave.xml. It should be leaveApplicationSummary.
EDIT:
Your ListView is in listleave.xml but you are using activity_apply_leave.xml in your Activity.
Move your ListView from listleave.xml to activity_apply_leave.xml.

Manage RadioButton without RadioGroup (Click on ParentView)

I have created one XML with 4 CardView contains 4 RadioButton and other layouts as follow:
fragment_list_of_transport.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:id="#+id/parent_card_1"
android:layout_width="0dp"
android:background="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_5sdp"
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view_1"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/radio_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="#+id/image_truck_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/image_truck_1" />
<ImageView
android:id="#+id/image_truck_1"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="#string/app_name"
app:layout_constraintBottom_toBottomOf="#+id/title_text_view_1"
app:layout_constraintStart_toEndOf="#+id/radio_button_1"
app:layout_constraintTop_toTopOf="#+id/title_text_view_1"
app:srcCompat="#drawable/ic_truck" />
<TextView
android:id="#+id/title_text_view_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/image_truck_1"
app:layout_constraintTop_toBottomOf="#+id/image_view_1" />
<Button
android:layout_width="#dimen/_90sdp"
android:layout_height="#dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/background_round_colors"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:text="#string/button_next"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_text_view_1"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/parent_card_2"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_5sdp"
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/parent_card_1">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view_2"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/radio_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="#+id/image_truck_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/image_truck_2" />
<ImageView
android:id="#+id/image_truck_2"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="#string/app_name"
app:layout_constraintBottom_toBottomOf="#+id/title_text_view_2"
app:layout_constraintStart_toEndOf="#+id/radio_button_2"
app:layout_constraintTop_toTopOf="#+id/title_text_view_2"
app:srcCompat="#drawable/ic_truck" />
<TextView
android:id="#+id/title_text_view_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/image_truck_2"
app:layout_constraintTop_toBottomOf="#+id/image_view_2" />
<Button
android:layout_width="#dimen/_90sdp"
android:layout_height="#dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/background_round_colors"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:text="#string/button_next"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_text_view_2"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:id="#+id/parent_card_3"
android:layout_width="0dp"
android:background="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_5sdp"
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/parent_card_2">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view_3"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/radio_button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="#+id/image_truck_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/image_truck_3" />
<ImageView
android:id="#+id/image_truck_3"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="#string/app_name"
app:layout_constraintBottom_toBottomOf="#+id/title_text_view_3"
app:layout_constraintStart_toEndOf="#+id/radio_button_3"
app:layout_constraintTop_toTopOf="#+id/title_text_view_3"
app:srcCompat="#drawable/ic_truck" />
<TextView
android:id="#+id/title_text_view_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/image_truck_3"
app:layout_constraintTop_toBottomOf="#+id/image_view_3" />
<Button
android:layout_width="#dimen/_90sdp"
android:layout_height="#dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/background_round_colors"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:text="#string/button_next"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_text_view_3"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:clickable="true"
android:focusable="true"
android:id="#+id/parent_card_4"
android:background="?attr/selectableItemBackground"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_5sdp"
android:layout_marginBottom="#dimen/_10sdp"
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/parent_card_3">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view_4"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_transport_list_height"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/now_courier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/radio_button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="#+id/image_truck_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/image_truck_4" />
<ImageView
android:id="#+id/image_truck_4"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:contentDescription="#string/app_name"
app:layout_constraintBottom_toBottomOf="#+id/title_text_view_4"
app:layout_constraintStart_toEndOf="#+id/radio_button_4"
app:layout_constraintTop_toTopOf="#+id/title_text_view_4"
app:srcCompat="#drawable/ic_truck" />
<TextView
android:id="#+id/title_text_view_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:text="Small express under 10 items"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/image_truck_4"
app:layout_constraintTop_toBottomOf="#+id/image_view_4" />
<Button
android:layout_width="#dimen/_90sdp"
android:layout_height="#dimen/_30sdp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/background_round_colors"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:text="#string/button_next"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_text_view_4"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</layout>
Output:
I want to manage RadioButton on click of both CardView and RadioButton , I have used following code:
/**
* A simple {#link Fragment} subclass.
*/
public class FragmentListTransport extends Fragment implements View.OnClickListener {
FragmentListOfTransportBinding mBinding;
public FragmentListTransport() {
// Required empty public constructor
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_list_of_transport, container, false);
mBinding.parentCard1.setOnClickListener(this);
mBinding.parentCard2.setOnClickListener(this);
mBinding.parentCard3.setOnClickListener(this);
mBinding.parentCard4.setOnClickListener(this);
return mBinding.getRoot();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.parent_card_1:
mBinding.radioButton1.setChecked(true);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(false);
break;
case R.id.parent_card_2:
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(true);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(false);
break;
case R.id.parent_card_3:
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(true);
mBinding.radioButton4.setChecked(false);
break;
case R.id.parent_card_4:
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(true);
break;
}
}
}
Is there any best way to manage it?
I don't easiest way then following (If you have easy way then I will accept it.):
/**
* A simple {#link Fragment} subclass.
*/
public class FragmentListTransport extends Fragment implements View.OnClickListener {
FragmentListOfTransportBinding mBinding;
public FragmentListTransport() {
// Required empty public constructor
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_list_of_transport, container, false);
mBinding.parentCard1.setOnClickListener(this);
mBinding.parentCard2.setOnClickListener(this);
mBinding.parentCard3.setOnClickListener(this);
mBinding.parentCard4.setOnClickListener(this);
mBinding.radioButton1.setOnClickListener(this);
mBinding.radioButton2.setOnClickListener(this);
mBinding.radioButton3.setOnClickListener(this);
mBinding.radioButton4.setOnClickListener(this);
return mBinding.getRoot();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.parent_card_1:
case R.id.radio_button_1:
clickedRadioButton(mBinding.radioButton1);
break;
case R.id.parent_card_2:
case R.id.radio_button_2:
clickedRadioButton(mBinding.radioButton2);
break;
case R.id.parent_card_3:
case R.id.radio_button_3:
clickedRadioButton(mBinding.radioButton3);
break;
case R.id.parent_card_4:
case R.id.radio_button_4:
clickedRadioButton(mBinding.radioButton4);
break;
}
}
private void clickedRadioButton(RadioButton radioButton) {
mBinding.radioButton1.setChecked(false);
mBinding.radioButton2.setChecked(false);
mBinding.radioButton3.setChecked(false);
mBinding.radioButton4.setChecked(false);
radioButton.setChecked(true);
}
}
If anyone has easiest way then this, please post answer. I will accept.

layout is not showing up

I'm trying to create a layout for my Fragment . Here is my xml layout.
Fragment_profil.xml
<FrameLayout 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="com.example.boby.srsly.ProfilFragment">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.027">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/fullnametext"
android:layout_width="144dp"
android:layout_height="18dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="64dp"
android:text="fullnametext"
app:layout_constraintBottom_toTopOf="#+id/notelptext"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.716"
app:layout_constraintStart_toEndOf="#+id/fullnameLabel"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26" />
<TextView
android:id="#+id/fullnameLabel"
android:layout_width="117dp"
android:layout_height="23dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="76dp"
android:text="Fullname"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView4"
android:layout_width="117dp"
android:layout_height="23dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="64dp"
android:text="No Telp"
app:layout_constraintBottom_toTopOf="#+id/textView6"
app:layout_constraintEnd_toStartOf="#+id/notelptext" />
<TextView
android:id="#+id/notelptext"
android:layout_width="144dp"
android:layout_height="20dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="28dp"
android:text="notelp"
app:layout_constraintBottom_toTopOf="#+id/deskripsitxt"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="117dp"
android:layout_height="23dp"
android:layout_marginEnd="60dp"
android:text="Deskripsi"
app:layout_constraintEnd_toStartOf="#+id/deskripsitxt"
tools:layout_editor_absoluteY="180dp" />
<TextView
android:id="#+id/deskripsitxt"
android:layout_width="144dp"
android:layout_height="63dp"
android:layout_marginBottom="252dp"
android:layout_marginEnd="28dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
But, when i run it i only see white background, i mean because this is bottomnavigationview i only can see the navigation from what i write above. How can i fix it, is there something missing ? Thanks in advance and sorry for my bad english.
Here is my ProfilFragment.java
public class ProfilFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profil, container, false);
SharedPreferences mPrefs = this.getActivity().getSharedPreferences("userinfoafterlogin", MODE_PRIVATE); //add key
String response = mPrefs.getString("userinfo", null);
try{
JSONObject json = new JSONObject(response);
json = (JSONObject)json.get("nameValuePairs");
json = (JSONObject)json.get("userData");
json = (JSONObject)json.get("nameValuePairs");
String fullname = (String)json.get("fullname");
String phone = (String)json.get("phone");
String desc = (String)json.get("Deskripsi");
TextView labelfullname = (TextView)v.findViewById(R.id.fullnametext);
TextView labeltelp = (TextView)v.findViewById(R.id.notelptext);
TextView labeldesc = (TextView)v.findViewById(R.id.deskripsitxt);
labelfullname.setText(fullname);
labeltelp.setText(phone);
labeldesc.setText(desc);
return v;
}catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
return v;
}
}
}
Try this
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.027">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/fullnametext"
android:layout_width="144dp"
android:layout_height="18dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="64dp"
android:text="fullnametext"
app:layout_constraintBottom_toTopOf="#+id/notelptext"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.716"
app:layout_constraintStart_toEndOf="#+id/fullnameLabel"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26" />
<TextView
android:id="#+id/fullnameLabel"
android:layout_width="117dp"
android:layout_height="23dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="76dp"
android:text="Fullname"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView4"
android:layout_width="117dp"
android:layout_height="23dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="64dp"
android:text="No Telp"
app:layout_constraintBottom_toTopOf="#+id/textView6"
app:layout_constraintEnd_toStartOf="#+id/notelptext" />
<TextView
android:id="#+id/notelptext"
android:layout_width="144dp"
android:layout_height="20dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="28dp"
android:text="notelp"
app:layout_constraintBottom_toTopOf="#+id/deskripsitxt"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/textView6"
android:layout_width="117dp"
android:layout_height="23dp"
android:layout_marginEnd="60dp"
android:text="Deskripsi"
app:layout_constraintEnd_toStartOf="#+id/deskripsitxt"
tools:layout_editor_absoluteY="180dp" />
<TextView
android:id="#+id/deskripsitxt"
android:layout_width="144dp"
android:layout_height="63dp"
android:layout_marginBottom="252dp"
android:layout_marginEnd="28dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
OUTPUT

Categories

Resources