Button does not get clicked on the first attempt - android

There are a few buttons in my activity which do not get clicked on the first attempt but they get clicked from the second time onwards. It seems that the button gets focus on the first click and gets actually pressed from the second time onwards. What can be the reason that these buttons do not get clicked the first time?
I have noticed that this behaviour is only shown by buttons that are at the very bottom of my activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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.me.proj.view.activities.MallActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
app:title=" "
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_mall_showcase_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/info_card" />
<include layout="#layout/photo_card" />
<include layout="#layout/address_card" />
<include layout="#layout/opening_hours_card" />
<include layout="#layout/review_card" /> <!-- Button in this file has the problem-->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
review_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0.5dp"
card_view:cardMaxElevation="1dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_marginTop="7dp"
android:background="#color/colorWhite"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<TextView
android:id="#+id/txt_rating"
android:background="#drawable/blue_button_background"
android:text="3.8"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:textSize="24sp"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_width="60dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/txt_reviews_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:layout_toEndOf="#+id/txt_rating"
android:layout_marginStart="20dp"
android:textStyle="bold"
android:paddingTop="1dp"
android:layout_alignTop="#+id/txt_rating"
android:text="Based on 98 reviews"/>
<TextView
android:id="#+id/txt_read_all_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read All Reviews"
android:layout_alignStart="#id/txt_reviews_title"
android:layout_toEndOf="#id/txt_rating"
android:layout_alignBottom="#id/txt_rating"
android:gravity="bottom"
android:paddingBottom="1dp"
android:textColor="#color/red"
android:layout_below="#id/txt_reviews_title"/>
<View
android:id="#+id/view_dummy1"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="#id/txt_read_all_reviews"
android:layout_marginTop="10dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="16dp"
android:layout_below="#+id/view_dummy1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.6"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txt_how_did_your_visit_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How did your visit go?"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#color/primary_text" />
<TextView
android:id="#+id/txt_tell_everyone_about_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tell everyone about it!"
android:layout_marginTop="5dp"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
</LinearLayout>
<!--This got some problem-->
<Button
android:id="#+id/btn_add_review"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="Add Your Review"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:background="#drawable/blue_button_background"
android:layout_weight="0.5"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_add_review).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
}

Code in my MainActivity.class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.sub_layout);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your stuff
Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
}
});
}
}
Code in my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="eflair.myapplication.MainActivity">
<include
android:id="#+id/sub_layout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
Code in my sub_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>

This issue is actually a bug in the NestedScrollView class for a while now.
I solved this issue my making some tweaks to the source code of the NestedScrollView class, as follows :-
public class NestedScrollView extends FrameLayout implements NestedScrollingParent,
NestedScrollingChild, ScrollingView {
...
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (actionMasked) {
case MotionEvent.ACTION_DOWN: {
if (getChildCount() == 0) {
return false;
}
//add this condition
if (!inChild((int) ev.getX(), (int) ev.getY())) {
return false;
}
if ((mIsBeingDragged = !mScroller.isFinished())) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
...
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
...
switch (action & MotionEventCompat.ACTION_MASK) {
...
case MotionEvent.ACTION_DOWN: {
...
// replace this line:
// mIsBeingDragged = !mScroller.isFinished();
mIsBeingDragged = false;
...
}
}
}
}
After doing this it should work fine.

Related

Android Fragment RecyclerView Transparent when replace

i just want to replace a fragment by using this code:
fragmentManager.beginTransaction()
.replace(R.id.homepage_fragment_menu, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
the fragment has recyclerview. but when i see the result of fragment, there's no shown of recyclerview. but when i checked in LayoutInspector, it's showed, but invisible like
this
And here's the code when set the adapter on RecyclerView
bookingHistoryProgressBar.setVisibility(View.GONE);
bookingHistoryList.setVisibility(View.VISIBLE);
adapterBookingHistoryList = new AdapterBookingHistoryList(this, bookingHistoryListModels);
bookingHistoryList.setAdapter(adapterBookingHistoryList);
Here's the layout code for RecyclerView and Adapter
XML Fragment Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
tools:context=".home.bookinghistorypage.BookingHistoryFragment"
android:gravity="center">
<android.support.v7.widget.RecyclerView
android:id="#+id/bookingHistory_listBooking"
android:background="#color/white_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Adapter
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/booking_history_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="6dp"
app:cardElevation="6dp"
android:layout_marginTop="#dimen/margin_small"
android:layout_marginBottom="#dimen/margin_small"
app:cardUseCompatPadding="true">
<LinearLayout
android:background="#color/white"
android:paddingLeft="#dimen/padding_large"
android:paddingRight="#dimen/padding_large"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<helper.CustomFontMontserratBoldTextView
android:id="#+id/booking_history_name"
android:layout_marginTop="#dimen/margin_large"
android:layout_marginBottom="3dp"
android:textColor="#color/black"
android:textSize="#dimen/text_medium"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<helper.CustomFontMontserratMediumTextView
android:id="#+id/booking_history_code"
android:gravity="end"
android:layout_marginTop="#dimen/margin_large"
android:layout_marginBottom="#dimen/margin_small"
android:textColor="#color/grey_2"
android:textSize="#dimen/text_medium"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<helper.CustomFontMontserratBoldTextView
android:id="#+id/booking_history_status"
android:background="#color/lightOrange"
android:textColor="#color/black"
android:textSize="#dimen/text_very_small"
android:paddingLeft="#dimen/padding_small"
android:paddingRight="#dimen/padding_small"
android:paddingTop="#dimen/padding_small"
android:paddingBottom="#dimen/padding_small"
android:layout_marginBottom="#dimen/padding_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_marginBottom="#dimen/padding_medium"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#mipmap/ic_tanggal"
android:layout_width="20dp"
android:layout_height="20dp" />
<helper.CustomFontMontserratRegularTextView
android:visibility="gone"
android:id="#+id/booking_history_day_booked"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginLeft="#dimen/margin_small"
android:textColor="#color/grey"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<helper.CustomFontMontserratMediumTextView
android:id="#+id/booking_history_actual_date"
android:layout_marginStart="#dimen/margin_small"
android:layout_marginLeft="#dimen/margin_small"
android:textColor="#color/grey"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<helper.CustomFontMontserratMediumTextView
android:id="#+id/booking_history_time_booked"
android:layout_marginStart="#dimen/margin_medium"
android:layout_marginLeft="#dimen/margin_medium"
android:textColor="#color/grey"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
XML Activity (Parent of Fragment)
<com.flipboard.bottomsheet.BottomSheetLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottomsheet"
android:background="#color/black_image_view">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_height="?android:attr/actionBarSize"
android:layout_width="match_parent">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_title_and_description"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/homepage_fragment_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appbar_layout"
android:layout_above="#id/navigation"
/>
<include
android:id="#+id/navigation"
layout="#layout/element_bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<ProgressBar
android:id="#+id/homepage_progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>
AdapterJava
#NonNull
#Override
public BookingHistoryListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.adapter_booking_history,parent,false);
context = parent.getContext();
return new BookingHistoryListViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final BookingHistoryListViewHolder holder, final int position) {
String data = GetDateUtils.convertEpochToDate(dataList.get(position).getActualDate());
if(dataList.get(position).getBookingStatus().equals("Completed") && dataList.get(position).getFlagRating().equals(false)){
Intent intent = new Intent(holder.itemView.getContext(), ProfessionalRatingActivity.class);
intent.putExtra("professionalId", dataList.get(position).getProfessionalId());
intent.putExtra("expertiseId", dataList.get(position).getExpertiseId());
intent.putExtra("professionalName",dataList.get(position).getProfessionalName());
holder.itemView.getContext().startActivity(intent);
}
if(dataList.get(position).getBookingStatus().equals("OnProcess")){
holder.bookingHistoryStatus.setBackgroundColor(ContextCompat.getColor(this.context,R.color.on_process));
}else if(dataList.get(position).getBookingStatus().equals("Cancel")){
holder.bookingHistoryStatus.setBackgroundColor(ContextCompat.getColor(this.context,R.color.cancelled));
}else if(dataList.get(position).getBookingStatus().equals("Completed")){
holder.bookingHistoryStatus.setBackgroundColor(ContextCompat.getColor(this.context,R.color.completed));
}else if(dataList.get(position).getBookingStatus().equals("Reserved")){
holder.bookingHistoryStatus.setBackgroundColor(ContextCompat.getColor(this.context,R.color.reserved));
}else if(dataList.get(position).getBookingStatus().equals("Reschedule")){
holder.bookingHistoryStatus.setBackgroundColor(ContextCompat.getColor(this.context,R.color.rescheduled));
}else if(dataList.get(position).getBookingStatus().equals("Confirmed")){
holder.bookingHistoryStatus.setBackgroundColor(ContextCompat.getColor(this.context,R.color.confirmed));
}
holder.bookingHistoryName.setText(dataList.get(position).getProfessionalName());
holder.bookingHistoryStatus.setText(dataList.get(position).getBookingStatus());
holder.bookingHistoryDayBooked.setText(dataList.get(position).getDayBooked());
holder.bookingHistoryCode.setText(String.format(Locale.getDefault(),"ID: %s",dataList.get(position).getCode()));
holder.bookingHistoryActualDate.setText(String.format(Locale.getDefault(),"%s",data));
holder.bookingHistoryTimeBooked.setText(String.format(Locale.getDefault(),"# %s",dataList.get(position).getTimeBooked()));
holder.bookingHistoryCardview.setOnClickListener(v -> {
Intent intent = new Intent(v.getContext(), BookingHistoryDetailActivity.class);
intent.putExtra("idProfessionalAdapter", dataList.get(position).getId());
holder.itemView.getContext().startActivity(intent);
});
}
#Override
public int getItemCount() {
return dataList!=null ? dataList.size() :0 ;
}
You're nesting your fragment view in a BottomSheetLayout, and according to the source code, the initial visibility is View.INVISIBLE:
private void init() {
// ...
dimView = new View(getContext()); // Line 150
dimView.setBackgroundColor(Color.BLACK);
dimView.setAlpha(0);
dimView.setVisibility(INVISIBLE);
// ...
}
public void setContentView(View contentView) {
super.addView(contentView, -1, generateDefaultLayoutParams());
super.addView(dimView, -1, generateDefaultLayoutParams());
}
I'm not familiar with Flipboard library, but I've got a feeling that you might be using it incorrectly. Maybe try:
bottomSheetLayout.showWithSheetView(...)
Or please check on their GitHub for usage.
Your recycler view in xml layout has `android:visibility="gone". Remove this line or change it to visible.

RecyclerView not showing up properly (scrunched up)

I've been trying to implement multiple RecyclerView within a layout that is part of a collapsing tab. However, my RecyclerView hasn't been working and I don't know what I did wrong in my code. Please help me!
Here is the Github Link: github.com/arxbombus/RecipeDetails
Here is the desired view: image
Here is what I'm getting for some reason: image
As you can see, everything is all scrunched up. :(
Below I've included the layouts for my MainActivity and also the Java files for my Adapters.
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
app:title="Some Randome Recipe"
app:titleEnabled="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/ivParallax"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/food"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtAbout"
android:text="About this recipe"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-25dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtRecipeDescription"
android:text="#string/recipe_description"
android:padding="15dp"
android:layout_marginBottom="-5dp"
android:textSize="12sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtInfo"
android:text="Info"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-5dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeInfo"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtIngredient"
android:text="Ingredients"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-5dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeIngredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorDivider"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
walking
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtProcedures"
android:text="Procedures"
android:textStyle="bold"
android:textSize="13sp"
android:padding="15dp"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-5dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeProcedure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
And my CardView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recipeInfoCards"
android:layout_width="81dp"
android:layout_height="75dp"
app:cardCornerRadius="6dp"
android:elevation="15dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorRecipeInfoCardBG"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtRecipeInfoCardTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cooking Time:"
android:textSize="11sp"
android:textColor="#android:color/black"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/txtRecipeInfoCardDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20 Minutes"
android:textSize="11sp"
android:textColor="#color/colorTextSecondary"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
And here are my MainActivity and Adapters respectiviely
public class MainActivity extends AppCompatActivity {
ArrayList < Recipe > recipeData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
recipeData = new ArrayList < Recipe > ();
createData();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
RecyclerView recipeInfoCardRV = (RecyclerView) findViewById(R.id.rvRecipeInfo);
recipeInfoCardRV.setHasFixedSize(true);
recipeInfoCardRV.setNestedScrollingEnabled(false);
RecipeInfoAdapter recipeInfoAdapter = new RecipeInfoAdapter(this, recipeData);
recipeInfoCardRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recipeInfoCardRV.setAdapter(recipeInfoAdapter);
RecyclerView recipeIngredientRV = (RecyclerView) findViewById(R.id.rvRecipeIngredient);
recipeIngredientRV.setHasFixedSize(true);
recipeIngredientRV.setNestedScrollingEnabled(false);
recipeIngredientRV.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
RecipeIngredientAdapter recipeIngredientAdapter = new RecipeIngredientAdapter(this, recipeData);
recipeIngredientRV.setAdapter(recipeIngredientAdapter);
}
public void createData() {
ArrayList < RecipeInfoCard > recipeInfoCards = new ArrayList < RecipeInfoCard > ();
recipeInfoCards.add(new RecipeInfoCard("Cooking Time", "20 Minutes"));
recipeInfoCards.add(new RecipeInfoCard("Calories", "3501"));
recipeInfoCards.add(new RecipeInfoCard("Procedures", "Three"));
ArrayList < RecipeIngredient > recipeIngredients = new ArrayList < RecipeIngredient > ();
for (int i = 1; i <= 10; i++) {
recipeIngredients.add(new RecipeIngredient("Ingredient " + i, String.valueOf(i), "grams"));
}
Recipe dm = new Recipe(recipeInfoCards, recipeIngredients);
recipeData.add(dm);
}
}
My adapter for my CardView
public class RecipeInfoAdapter extends RecyclerView.Adapter < RecipeInfoAdapter.RecipeInfoCardItemRowHolder > {
private Context mContext;
private ArrayList < Recipe > recipeData;
public RecipeInfoAdapter(Context mContext, ArrayList < Recipe > recipeData) {
this.mContext = mContext;
this.recipeData = recipeData;
}
#Override
public RecipeInfoCardItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_info_card_view, null);
return new RecipeInfoCardItemRowHolder(v);
}
#Override
public void onBindViewHolder(RecipeInfoCardItemRowHolder recipeInfoCardItemRowHolder, int position) {
Recipe recipe = recipeData.get(position);
recipeInfoCardItemRowHolder.infoCardTitle.setText(recipe.getRecipeInfoCards().get(position).getRecipeInfoCardTitle());
recipeInfoCardItemRowHolder.infoCardDescription.setText(recipe.getRecipeInfoCards().get(position).getRecipeInfoCardDescription());
}
#Override
public int getItemCount() {
return (null != recipeData ? recipeData.size() : 0);
}
public class RecipeInfoCardItemRowHolder extends RecyclerView.ViewHolder {
protected TextView infoCardTitle;
protected TextView infoCardDescription;
public RecipeInfoCardItemRowHolder(View view) {
super(view);
this.infoCardTitle = (TextView) view.findViewById(R.id.txtRecipeInfoCardTitle);
this.infoCardDescription = (TextView) view.findViewById(R.id.txtRecipeInfoCardDescription);
}
}
}
I didn't put all my code here because I think the question is long enough but I would really appreciate if someone helped me. Thank you!
Try to change your rvRecipeInfo RecyclerView height.. becasue your hardicoading it to 75 dp which is wrong.
<android.support.v7.widget.RecyclerView
android:id="#+id/rvRecipeInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp"></android.support.v7.widget.RecyclerView>
similer to cardview as well
< ?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recipeInfoCards"
android:layout_width="100dp"
android:layout_height="75dp"
app:cardCornerRadius="6dp"
android:elevation="15dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorRecipeInfoCardBG"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtRecipeInfoCardTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cooking Time:"
android:textSize="11sp"
android:textColor="#android:color/black"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/txtRecipeInfoCardDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20 Minutes"
android:textSize="11sp"
android:textColor="#color/colorTextSecondary"
android:layout_gravity="center_horizontal"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>

Clicking Children Item in Sliding Panel triggers parent first?

I am using sliding panel layout and in that i have ONE floating buttons.Now when i Click on the button the view Slides first and then i have to click again on button to perform action.
I want to perform action on only single click without the sliding movement of parent view.
Look at these images
Look at 1 images
look at this
Here is my
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/Sliding"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/leftSide"
android:background="#ef3"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contact"
android:background="#drawable/bg_key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/rightSide"
android:layout_marginLeft="170dp"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:focusable="false"
app:srcCompat="#drawable/ic_menu_camera"
android:id="#+id/call"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/message" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
here is my java code
SlidingPaneLayout sp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call = (FloatingActionButton) findViewById(R.id.call);
sp = (SlidingPaneLayout) findViewById(R.id.Sliding);
assert sp != null;
sp.setPanelSlideListener(this);
sp.setSliderFadeColor(Color.TRANSPARENT);
sp.openPane();
mList = new ArrayList<Contacts>();
call.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.e("CALL","CALL");
try
{
if(number.isEmpty())
{
Toast.makeText(context,"Please select contact first",Toast.LENGTH_SHORT).show();
}
else
{
calling();
number=null;
}
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(context,"This is not the valid contact",Toast.LENGTH_SHORT).show();
}
}
});
}
Use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SlidingPaneLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/Sliding"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/leftSide"
android:background="#ef3"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contact"
android:background="#drawable/bg_key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
<RelativeLayout
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/rightSide"
android:layout_marginLeft="170dp"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:focusable="false"
app:srcCompat="#drawable/ic_menu_camera"
android:id="#+id/call"
android:layout_alignParentTop="true" />
</RelativeLayout>
</RelativeLayout>

Android: EditText inside ScrollView : hide keyboard generates strange scrolling

I have this layout example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFF880"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/action_bar_layout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/emotion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:inputType="textMultiLine|textCapSentences"
android:singleLine="false"
android:text="On sait depuis longtemps "
android:textColor="#android:color/black" />
<FrameLayout
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#EE00FF">
</FrameLayout>
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#990000"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
And my sample Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edittext);
final ScrollView scrollView = (ScrollView)findViewById(R.id.scroll);
final EditText edittext = (EditText)findViewById(R.id.edit);
// Setup scrollview
scrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
// Hide keyboard
UIUtils.hideKeyboard(edittext, EditTextActivity.this);
break;
}
return false;
}
});
public static void hideKeyboard(View view, Activity activity) {
if (view != null) {
view.clearFocus();
if (activity != null) {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
InputMethodManager imm = (InputMethodManager) activity.
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
So I would like the same behavior like Facebook new post: when you want to scroll the keyboard disappears automatically.
But in my case, there is a strange behavior when you want to scroll when the keyboard has been hide.
Thanks guys for your help!
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/action_bar_layout"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FFF880"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/emotion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:inputType="textMultiLine|textCapSentences"
android:singleLine="false"
android:text="On sait depuis longtemps "
android:textColor="#android:color/black" />
<FrameLayout
android:id="#+id/photo"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#EE00FF">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#990000"
android:gravity="center_vertical"
android:orientation="horizontal">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Try this hope it will solve your problem.
Instead of hiding keyboard on scroll ,you can hide keyboard on outsidetouch of edit text
Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
android:clickable="true"
android:focusableInTouchMode="true"
Implement a hideKeyboard() method
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
and set the onFocusChangeListener of your edittext.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
or if you want to hide on scroll then you can add on touch listner for your main view instead of scroll view
findViewById(R.id.mainLayout).setOnTouchListener(this)
set android:focusableInTouchMode="true" on main container

android navigationLayout use headerview can't close in Fragment

I customize my DrawerLayout HeaderView, and when I click the button on HeaderView, it can't close the drawer like click the button on drawer menu.
I try to use in official demo , it's the same,too.
So i just create a new class BackListener to override onBackPress();
and just new it in my button, it doesn't work, too.
Who has the solution tell me, please. I will be grateful.
DrawerLayout activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.taiwandigestionsociety_v11.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/mainFrame"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#01000000"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
HeaderView layout:
<?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"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="#66CDAA"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_grid_on_black_24dp" />
<TextView
android:id="#+id/logIn"
android:textSize="15dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pageLogIn"
android:textColor="#android:color/background_light"/>
<Button
android:id="#+id/logOut"
android:layout_marginLeft="15dp"
android:layout_width="60dp"
android:layout_height="38dp"
android:text="#string/logOut" />
</LinearLayout>
</LinearLayout>
I tried the method:
public class BackListener extends AppCompatActivity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("back onKeyDown()");
return false;
}else {
return super.onKeyDown(keyCode, event);
}
}
}
MainActivity:
logIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switchFragment(LogInFragment.newInstance());
new BackListener();
}
});

Categories

Resources