I have a few cardviews in a recyclerview. When I click on one I want it to show a different layout.
This is the 'template' that each card should have when I click on it.
<?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:id="#+id/scrollview_sam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/reading_bg"
android:fillViewport="true"
tools:context=".ScrollviewSam"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="188dp"
android:layout_marginStart="15dp"
android:layout_marginTop="56dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/sam" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="56dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/name"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="32dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/about"
android:textColor="#color/white"
android:textSize="35sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/background"
android:textColor="#color/white"
android:textSize="35sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="17dp"
android:fontFamily="#font/sackers_gothic_light"
android:lineHeight="20dp"
android:paddingLeft="17sp"
android:paddingRight="17sp"
android:text="#string/about_sam"
android:textAlignment="textStart"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="17dp"
android:fontFamily="#font/sackers_gothic_light"
android:lineHeight="20sp"
android:paddingLeft="17sp"
android:paddingRight="17sp"
android:text="#string/background_sam1"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView19" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="17dp"
android:fontFamily="#font/sackers_gothic_light"
android:lineHeight="20sp"
android:paddingLeft="17sp"
android:paddingRight="17sp"
android:text="#string/background_sam2"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/name_sam"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="88dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/gender_male"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView10"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="88dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/gender"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="116dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/hair_color"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView12"
android:layout_width="56dp"
android:layout_height="21dp"
android:layout_marginStart="8dp"
android:layout_marginTop="115dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/color_brown"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView11"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="148dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/eye_color"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="176dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/cast"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView16"
android:layout_width="101dp"
android:layout_height="39dp"
android:layout_marginStart="8dp"
android:layout_marginTop="176dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:lineHeight="20dp"
android:text="#string/cast_norman"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="148dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/color_blue"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="224dp"
android:fontFamily="#font/sackers_gothic_heavy"
android:text="#string/status_alive"
android:textColor="#color/white"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="224dp"
android:fontFamily="#font/sackers_gothic_light"
android:text="#string/status"
android:textColor="#color/gray"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="#+id/like"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="30dp"
android:layout_marginTop="56dp"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_baseline_favorite_border_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
This is my RecyclerViewAdapter.
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import com.example.myapplication.ScrollviewSam;
import java.util.List;
public class CharacterAdapter extends RecyclerView.Adapter<CharacterAdapter.ViewHolder> {
List<String> name;
List<Integer> images;
LayoutInflater layoutInflater;
public CharacterAdapter( Context context, List<String> name, List<Integer> images) {
this.name = name;
this.images = images;
this.layoutInflater = LayoutInflater.from(context);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.character_grid_layout,parent,false);
return new ViewHolder(view);
}
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(name.get(position));
holder.image.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return name.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView name;
ImageView image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
image = itemView.findViewById(R.id.image);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getAdapterPosition() == 0 ){
Intent intent = new Intent(v.getContext(), ScrollviewSam.class);
v.getContext().startActivity(intent);
}
}
});
}
}
}
As you can see I set a clickListener for the item. This is where it gets confusing for me since I created a new activity for the view but that will get super repetitive with creating the activity and the view over and over again.
You are almost there. Instead of checking the adapterPosition, and starting an Activity, you want to pass your data to the ViewHolder, and then to the Activity.
You can use onBindViewHo\lder() for the first, and [Intents][1] for the second part.
// In your adapter's onBindViewHolder
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bindDataToYourViewHolder(name.get(position), images.get(position))
}
The above, will set the name and image in your ViewHolder. You need to adjust your ViewHolder slightly:
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
image = itemView.findViewById(R.id.image);
}
public void bindDataToYourViewHolder(String yourName, int yourImage){
name.setText(yourName);
image.setImageResource(yourImage);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ScrollviewSam.class);
intent.putExtra("name", yourName)
// You can use yourImage here as well
v.getContext().startActivity(intent);
}
});
}
This way, you can pass name.getText() to your ScrollviewSam Activity.
Inside ScrollviewSam.onCreate() you can grab the extra by doing:
// In ScrollviewSam Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String name = getIntent().getStringExtra("name");
}
Besides Strings, you can pass Booleans, Integer and more through Bundles. You can even pass around whole objects if they implement the Parcelable interface. But mostly, basic primitives should get you going.
Related
I am trying to do same design in here. I have nested Recyclerview in my structure. I have BasketParentItem class and it keeps child products list. As you can see in here when i click the checkbox list expanding itself. This happens when i call notifyDataChangeSetChildAndParent() in listener. Any idea how to solve it ?
Edit: I solved problem. I removed divider on parent adapter now it's working well here
holder.rv_child_Basket_Parent.addItemDecoration(new DividerItemDecoration(mActivity.getApplicationContext(), DividerItemDecoration.VERTICAL));
holder.cb_Basket_CardView_Child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(list.get(position).isChecked()){
holder.cb_Basket_CardView_Child.setChecked(false);
list.get(position).setChecked(false);
}else{
holder.cb_Basket_CardView_Child.setChecked(true);
list.get(position).setChecked(true);
}
boolean allCheck = false;
for (BasketParentItem b : Basket.basketList) {
ArrayList<MyProduct> tempList = b.getChildItemList();
if (tempList.contains(list.get(position)) ) {
for (MyProduct mP : tempList) {
if(mP.isChecked()){
allCheck = true;
}
}
if(!allCheck){
//set parent uncheck
b.setChecked(false);
}
}
}
notifyDataChangeSetChildAndParent();
}
});
#Getter
#Setter
public class BasketParentItem {
ArrayList<MyProduct> childItemList = new ArrayList<>();
boolean checked=true;
public void addToList(MyProduct myProduct){
childItemList.add(myProduct);
}
public boolean removeFromList(MyProduct myProduct){ return childItemList.remove(myProduct); }
}
#Getter
#Setter
#NoArgsConstructor
public class MyProduct {
#NonNull
String id;
#NonNull
String name;
#NonNull
String description;
#NonNull
String imageURL;
#NonNull
String type;
#NonNull
ArrayList<String> seller;
int stock;
double price;
Boolean fastDelivery;
int amount;
String sellerName;
double sellerRate;
boolean checked=true;
//Returns selled Id
public String getSellerId() {
return seller.get(0);
}
}
public class Basket {
public static ArrayList<BasketParentItem> basketList = new ArrayList<>();
public static ArrayList<String> bestSellerStringList = new ArrayList<>();
public static ArrayList<MyProduct> bestSeller = new ArrayList<>();
}
public class BasketFragment extends Fragment {
//...
public void basketNotifyDataSetChanged(){
if(!Basket.basketList.isEmpty()){
ll_BasketFragment.setVisibility(View.GONE);
rv_parent_FragmentBasket.setVisibility(View.VISIBLE);
}else{
ll_BasketFragment.setVisibility(View.VISIBLE);
rv_parent_FragmentBasket.setVisibility(View.GONE);
}
if(parentAdapter != null){
parentAdapter.notifyDataSetChanged();
}
}
public void bestSellerNotifyDataSetChanged(){
if(adapter!=null){
adapter.notifyDataSetChanged();
}
}
}
private void notifyDataChangeSetChildAndParent(){
BasketFragment basketFragment = (BasketFragment) ((MainActivity) mActivity).findFragment(MainActivity.BASKET_FRAGMENT);
basketFragment.basketNotifyDataSetChanged();
basketFragment.bestSellerNotifyDataSetChanged();
}
Parent Row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="0dp"
android:background="#drawable/custom_background_3"
android:elevation="3dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/cb_Basket_CardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_Basket_Parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Satıcı"
android:textColor="#color/grey"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/green_ice"
android:gravity="center"
android:orientation="horizontal"
android:paddingVertical="2dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_package" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_shipping"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_child_Basket_Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"/>
</LinearLayout>
Child Row
<?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="wrap_content"
android:layout_marginVertical="14dp">
<CheckBox
android:id="#+id/cb_Basket_CardView_Child"
android:layout_width="14dp"
android:layout_height="15dp"
android:layout_marginRight="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.367" />
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/iv_Basket_CardView_Child"
android:layout_width="70dp"
android:layout_height="102dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/linearLayout"
app:layout_constraintStart_toEndOf="#+id/cb_Basket_CardView_Child"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="#style/roundedImageViewRounded"
app:srcCompat="#drawable/ic_home" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintTop_toTopOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="#+id/tv_productName_Basket_CardView_Child"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/iv_deleteProduct_Basket_CardView_Child"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="15dp"
app:srcCompat="#drawable/ic_trash" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:background="#drawable/custom_background_7"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="1.0">
<ImageView
android:id="#+id/iv_Decrease_CardView_Child"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="2dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_decrease_disabled" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="#+id/tv_ProductAmount_Basket_CardView_Child"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginVertical="2dp"
android:background="#drawable/custom_background_8"
android:gravity="center"
android:maxLength="4"
android:text="1"
android:textColor="#color/pumpkin"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/pb_CardView_Child"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_gravity="center"
android:indeterminateTint="#color/pumpkin"
android:visibility="invisible"/>
</FrameLayout>
<ImageView
android:id="#+id/iv_Increase_CardView_Child"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="2dp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_increase" />
</LinearLayout>
<TextView
android:id="#+id/tv_FastDelivery_Basket_CardView_Child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:text="TextView"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout2"
app:layout_constraintEnd_toEndOf="#+id/linearLayout"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/iv_Basket_CardView_Child"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/tv_Price_Basket_CardView_Child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TL"
android:textColor="#color/pumpkin"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/linearLayout2"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
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>
I'm pretty much a beginner still and I've tried to make an App to ease my work in the Lab. It's just some basic calculation. I want to make an App that is compatible to the most devices, in case its useful information. Even though the App seems to Build successfully, as soon as it starts on my Device it just crashes immediatelly. Files used are listed below. I tried all the hints Android Studio gave me with no result. App still crashed.
Would be very thankful for some hints and constructive feeback!
Main Activity.java
package com.e.concalc;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText protein_sp;
private EditText salt_sp;
private EditText protein_st;
private EditText salt_st;
private EditText volume_sp;
private TextView tv_resultH2O;
private TextView tv_resultSalt;
private TextView tv_resultProtein;
private Button button1;
public MainActivity(TextView tv_resultH2O, TextView tv_resultSalt, TextView tv_resultProtein, Button button1) {
this.tv_resultH2O = tv_resultH2O;
this.tv_resultSalt = tv_resultSalt;
this.tv_resultProtein = tv_resultProtein;
this.button1 = button1;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protein_sp = findViewById(R.id.edit1);
protein_st = findViewById(R.id.edit2);
salt_sp = findViewById(R.id.edit3);
salt_st = findViewById(R.id.edit4);
volume_sp = findViewById(R.id.edit5);
button1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
makeCalculations();
}
});
}
private void makeCalculations() {
double p_sp = Double.valueOf(protein_sp.getText().toString());
double p_st = Double.valueOf(protein_st.getText().toString());
double s_sp = Double.valueOf(salt_sp.getText().toString());
double s_st = Double.valueOf(salt_st.getText().toString());
double v_sp = Double.valueOf(volume_sp.getText().toString());
double resultH2O;
double resultSalt;
double resultProtein;
resultProtein = p_sp * v_sp / p_st;
resultSalt = s_sp * v_sp / s_st;
resultH2O = v_sp - resultProtein - resultSalt;
tv_resultH2O.setText(Double.toString(resultH2O));
tv_resultSalt.setText(Double.toString(resultSalt));
tv_resultProtein.setText(Double.toString(resultProtein));
}
}
activity_main.xml - Layout
<?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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.77"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/SampleVolume"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.35" />
<EditText
android:id="#+id/edit1"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toBottomOf="#+id/text1"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit2"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toBottomOf="#+id/text2"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit4"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text4"
app:layout_constraintTop_toBottomOf="#+id/text4"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit3"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toBottomOf="#+id/text3"
android:importantForAutofill="no" />
<EditText
android:id="#+id/edit5"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text5"
app:layout_constraintTop_toBottomOf="#+id/text5"
android:importantForAutofill="no" />
<Button
android:id="#+id/button1"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginBottom="128dp"
android:text="#string/button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:text="#string/button2"
app:layout_constraintBottom_toBottomOf="#+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.745"
app:layout_constraintStart_toEndOf="#+id/button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/button1" />
<TextView
android:id="#+id/tv_resultH2O"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultSalt"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultProtein"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.9"
app:layout_constraintVertical_bias="0.6"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You forgot to findViewById the button1.So in onCreate method, before button1.setOnClickListener.... add this line:
button1 = findViewById(R.id.button1);
Remove your custom constructor.
Activities must have a no-argument constructor that Android uses to create your class and when you define any override then the no-argument constructor is no longer automatically created for you in Java...
I am planing a little app with recyclerviews in it.
Sometimes I have to load only 4 items to it and sometimes over 200.
Now I don't get it, how to center the custom recyclerviews items!
I have a custom recyclerview (grid) and I want to have 4 (2x2) items in it beeing centered.
What I have:
What I want:
So where is my mistake in following code?
fragment_single_pokemon.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="#+id/cLayTypOne"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#drawable/customborder_typ"
app:layout_constraintBottom_toTopOf="#+id/cLayTypTwo"
app:layout_constraintEnd_toStartOf="#+id/civSprite"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/txtTypOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="8sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<View
android:id="#+id/vLine2"
android:layout_width="50dp"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="#+id/civSprite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/civSprite"
app:layout_constraintTop_toTopOf="#+id/civSprite">
</View>
<TextView
android:id="#+id/txtPokemonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Pokéball"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/civSprite"
android:layout_width="148dp"
android:layout_height="148dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:src="#mipmap/ic_launcher"
app:civ_border_width="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtPokemonName" />
<View
android:id="#+id/vLine"
android:layout_width="50dp"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#000000"
app:layout_constraintBottom_toBottomOf="#+id/civSprite"
app:layout_constraintEnd_toStartOf="#+id/civSprite"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/civSprite">
</View>
<ImageView
android:id="#+id/imgIsRaidBoss"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="#+id/vLine2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/civSprite"
app:srcCompat="#drawable/raid_sprite" />
<ImageView
android:id="#+id/imgShinyExists"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toTopOf="#+id/imgIsRaidBoss"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/civSprite"
app:srcCompat="#drawable/ic_shiny" />
<android.support.constraint.ConstraintLayout
android:id="#+id/cLayTypTwo"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#drawable/customborder_typ"
app:layout_constraintBottom_toTopOf="#+id/vLine"
app:layout_constraintEnd_toStartOf="#+id/civSprite"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/txtTypTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="8sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/btnPokeDexEnty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:layout_marginStart="48dp"
android:layout_marginTop="16dp"
android:background="#drawable/boarder_shape"
android:text="Pokédex Eintrag"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/civSprite" />
<TextView
android:id="#+id/txtPokeDexEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textAlignment="center"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="#+id/btnPokeDexEnty"
app:layout_constraintStart_toStartOf="#+id/btnPokeDexEnty"
app:layout_constraintTop_toBottomOf="#+id/btnPokeDexEnty" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rViewPokeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:foregroundGravity="center_vertical"
android:orientation="vertical"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtPokeDexEntry" />
</android.support.constraint.ConstraintLayout>
gridview_pokedex_pokeinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rLayMainContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtPokeInfoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="#+id/txtPokeInfoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPokeInfoTitle"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
recyclerview code
RecyclerView recyclerViewPokeInfo = (RecyclerView)rLayout_mainFragment.findViewById(R.id.rViewPokeInfo);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),2);
recyclerViewPokeInfo.setLayoutManager(layoutManager);
CustomRecyclerAdapterPokeInfoGrid adapter = new CustomRecyclerAdapterPokeInfoGrid(getActivity(), db.getPokeInfo(intPokemonID));
recyclerViewPokeInfo.setAdapter(adapter);
recyclerview adapter
package com.spicysoftware.goexpert;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomRecyclerAdapterPokeInfoGrid extends RecyclerView.Adapter<CustomRecyclerAdapterPokeInfoGrid.ViewHolder> {
private final ArrayList<String> strPokeInfo;
private Context context;
MySQLiteHelper db;
int locationSpriteNo;
String strCatchRate = "";
String strEscapeRate = "";
String strInEggs = "";
String strKmAsFriend = "";
public CustomRecyclerAdapterPokeInfoGrid(Context context, ArrayList<String> strPokeInfo) {
this.strPokeInfo = strPokeInfo;
this.context = context;
}
#Override
public CustomRecyclerAdapterPokeInfoGrid.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.gridview_pokedex_pokeinfo, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final CustomRecyclerAdapterPokeInfoGrid.ViewHolder viewHolder, final int i) {
viewHolder.setClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
}
});
// load image
try {
db = new MySQLiteHelper(context);
String currentString = strPokeInfo.get(i);
if(i == 0){
viewHolder.txtPokeInfoTitle.setText("Fangchance");
}else if(i == 1){
viewHolder.txtPokeInfoTitle.setText("Fluchtchance");
}else if(i == 2){
viewHolder.txtPokeInfoTitle.setText("In Eiern");
}else if(i == 3){
viewHolder.txtPokeInfoTitle.setText("Distanz als Kumpel");
}
viewHolder.txtPokeInfoText.setText(currentString);
}
catch(Exception ex) {
Log.v("Error: ", "-> "+ex);
}
}
#Override
public int getItemCount() {
return strPokeInfo.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
private TextView txtPokeInfoTitle,txtPokeInfoText;
private ItemClickListener clickListener;
private ConstraintLayout cLayoutSingleItem;
public ViewHolder(View view) {
super(view);
txtPokeInfoTitle = (TextView)view.findViewById(R.id.txtPokeInfoTitle);
txtPokeInfoText = (TextView)view.findViewById(R.id.txtPokeInfoText);
cLayoutSingleItem = (ConstraintLayout)view.findViewById(R.id.cLayoutSingleItem);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
#Override
public void onClick(View view) {
clickListener.onClick(view, getPosition(), false);
}
#Override
public boolean onLongClick(View view) {
clickListener.onClick(view, getPosition(), true);
return true;
}
}
}
Set the list item layout to match_parent like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rLayMainContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtPokeInfoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textStyle="bold" />
<TextView
android:id="#+id/txtPokeInfoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPokeInfoTitle"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
Try setting layout_gravity in your textViews instead of the relativeLayout.
I am making a sign up and sign in form using TabLayout. The height of the signup form will be long, but due to some reasons I am not able to add ScrollView inside fragment.
Here is what I am doing: https://imgur.com/9p42i43
Here is my layout Main Activity
<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="#color/appBackground"
tools:context=".LoginActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/logo_view"
android:layout_width="0dp"
android:layout_height="70dp"
android:background="#color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar2" />
<android.support.design.widget.TabLayout
android:id="#+id/signin_signup_tab_lay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo_view"
app:tabBackground="#drawable/curve_login"
app:tabIndicatorColor="#color/yellow_btn"
app:tabSelectedTextColor="#color/primaryColor"
app:tabTextAppearance="#style/tabstyle"
app:tabTextColor="#7d98c3">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signin_signup_tab_lay"></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
Java Main Activity
package com.example.mobilestyx.fantasy_arena;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
TabLayout tabLayout;
private int[] tabIcons = {
R.drawable.curve_login,
R.drawable.curve_login
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = (TabLayout) findViewById(R.id.signin_signup_tab_lay);
tabLayout.addTab(tabLayout.newTab().setText("Signin"));
tabLayout.addTab(tabLayout.newTab().setText("Signup"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//
// setupTabIcons();
final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabLayout.getTabAt(1).setCustomView(tabTwo);
}}
Here is my fragment layout
<?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:id="#+id/constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
tools:context=".SignupFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="#drawable/login_fb_btn"
android:fontFamily="#font/mavenpro_regular_font"
android:text="Signup with Facebook"
android:textColor="#ffffff"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:fontFamily="#font/mavenpro_regular_font"
android:gravity="center"
android:text="OR"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button3" />
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2">
<EditText
android:id="#+id/signup_name_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/startborder_input"
android:ems="10"
android:fontFamily="#font/mavenpro_regular_font"
android:hint="John Doe"
android:inputType="textPersonName"
android:paddingLeft="15dp" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Full Name"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Email"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<android.support.v7.widget.CardView
android:id="#+id/cardView3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3">
<EditText
android:id="#+id/signup_email_et"
style="#style/MavenproEdittextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/startborder_input"
android:ems="10"
android:hint="johndoe#mail.com"
android:inputType="textEmailAddress"
android:paddingLeft="15dp" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Phone"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView3" />
<android.support.v7.widget.CardView
android:id="#+id/cardView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4">
<EditText
android:id="#+id/signup_phone_et"
style="#style/MavenproEdittextStyle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/startborder_input"
android:ems="10"
android:hint="000-000-0000"
android:inputType="number"
android:paddingLeft="15dp"
android:textColor="#000000" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Gender"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView4" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
<RadioButton
android:id="#+id/signup_gender_m_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="#+id/signup_gender_f_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>
<TextView
android:id="#+id/textView6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="DOB"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioGroup" />
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6">
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
</android.support.v7.widget.CardView></android.support.constraint.ConstraintLayout>
Java Fragment
package com.example.mobilestyx.fantasy_arena;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class SignupFragment extends Fragment {
public SignupFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
While using Constraint Layout, try using NestedScrollView for scrolling.
Make sure your ScrollView has constraints to parent which is ConstraintLayout view in each edges.