Android: Can't initialize array of TextView - java

I have a very crazy bug. When I create a TextView array and initialize them in onCreate with my id from xml, nothing is changed. I create a simple function to change anything, but I get a nullpointer exception. I don't have any idea what I'm doing wrong with my code.
I will be very gratefull for any help.
XML:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
>
<LinearLayout
android:id="#+id/keys"
.....
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/keys"
android:weightSum="6" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="#+id/data1" android:layout_alignTop="#+id/linearLayout"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="#+id/data2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="#+id/data3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="#+id/data4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="#+id/data5"/>
</LinearLayout>
</RelativeLayout>
CODE:
public class XXX extends Activity {
private String login;
....
private TextView[] dates;
public void change(String x) {
if(dates[0].getEditableText().toString().compareTo("") == 0)
dates[0].setText(x);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ABC);
dates = new TextView[5];
dates[0]=(TextView)findViewById(R.id.data1);
dates[1]= (TextView) findViewById(R.id.data2);
dates[2]=(TextView)findViewById(R.id.data3);
dates[3]=(TextView)findViewById(R.id.data4);
dates[4]=(TextView)findViewById(R.id.data5);
dates[0].setText("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
for (int i= 0; i < 5; i++){
dates[i].setText("LOL");
};
change("ROTFL");
}

Related

Views losing margin when activity created

I have a strange behavior here
When I start this dialog (which is actually an activity with dialog Theme) the EditTexts are losing the left margin.
This is my XML
`<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/alert_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primary">
<TextView
android:id="#+id/user_entry_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:text="#string/input_user"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="#+id/user_edit_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#color/icons"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/user_entry_hint">
<LinearLayout
android:id="#+id/editTextLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/icons"
android:orientation="vertical">
<EditText
android:id="#+id/user_name_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_medium"
android:hint="#string/userName"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/primary_text"
android:textColorHint="#color/primary_text"
android:textSize="14sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/primary_dark" />
<EditText
android:id="#+id/user_address_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/comfortaa_medium"
android:hint="#string/userAddress"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/primary_text"
android:textColorHint="#color/primary_text"
android:textSize="14sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/places_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/icons"
android:orientation="vertical"
android:padding="10dp"
app:layout_constrainedHeight="true" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal"
android:weightSum="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/user_edit_card">
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:text="#string/dialog_cancel"
android:textAllCaps="false" />
<Button
android:id="#+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:text="#string/dialog_save"
android:textAllCaps="false" />
</LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
`
And this is how I initialize it in activity.
public class AlertDialogActivity extends AppCompatActivity implements PlacesAutoCompleteAdapter.ClickListener {
private static final String TAG = "AlertDialogActivity";
private ActivityAlertDialogBinding binding;
private EditText userNameEdit;
private EditText userAddressEdit;
private Button saveUserButton;
private Button cancelButton;
private RecyclerView recyclerViewPlaces;
private PlacesAutoCompleteAdapter adapter;
private UserViewModel userViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityAlertDialogBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
this.setFinishOnTouchOutside(false);
initViews(binding);
private void initViews(ActivityAlertDialogBinding binding) {
userNameEdit = binding.userNameEt;
userAddressEdit = binding.userAddressEt;
saveUserButton = binding.saveButton;
cancelButton = binding.cancelButton;
recyclerViewPlaces = binding.placesRecyclerView;
}
I am trying to figure out why the views lose margin and when the Edittext is gaining focus the margins are restored
The Ui works fine on emulator. The video is from my device.
I would first remove all match_parent sizes from the children of the ConstraintLayout and replace with 0dp and the appropriate constraints. See the "important" comment here.
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

How to retrieve RecyclerView position

I am trying to get the position of items in my RecyclerView, mainly those that are at [0], [1], and [2] positions. The reason being is currently I am creating a leader board that will highlight the top 3 players with the highest score.
I have tried calling getAdapterPosition() but I guess I am using it incorrectly.
if(holder.getAdapterPosition() == 0)
{
Glide.with(holder.firstPlace.getContext())
.load(model.getUrl())
.into(holder.firstPlace);
}
I would greatly appreciate if someone could point me to the right direction. Thank you.
Edit 1:
My UserAdapter
public class UserAdapter extends FirestoreRecyclerAdapter<UserModel, UserAdapter.UserViewHolder> {
public UserAdapter(#NonNull FirestoreRecyclerOptions<UserModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull UserAdapter.UserViewHolder holder, int position, #NonNull UserModel model) {
holder.username.setText(model.getFullName());
holder.email.setText(model.getEmail());
holder.score.setText(model.getScore()+"");
holder.rank.setText(String.valueOf(position + 1));
Glide.with(holder.userImage.getContext())
.load(model.getUrl())
.into(holder.userImage);
//error here
// if(holder.getAdapterPosition() == 0)
// {
// Glide.with(holder.firstPlace.getContext())
// .load(model.getUrl())
// .into(holder.firstPlace);
// }
}
#NonNull
#Override
public UserAdapter.UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_leaderboard_single, parent, false);
return new UserViewHolder(view);
}
public class UserViewHolder extends RecyclerView.ViewHolder {
CircleImageView userImage;
TextView username;
TextView email;
TextView score;
TextView rank;
CircleImageView firstPlace;
CircleImageView secondPlace;
CircleImageView thirdPlace;
public UserViewHolder(#NonNull View itemView) {
super(itemView);
userImage = itemView.findViewById(R.id.list_image);
username = itemView.findViewById(R.id.list_username);
email = itemView.findViewById(R.id.list_email);
score = itemView.findViewById(R.id.list_score);
rank = itemView.findViewById(R.id.leaderboard_position);
firstPlace = itemView.findViewById(R.id.place1stProfile);
secondPlace = itemView.findViewById(R.id.place2ndProfile);
thirdPlace = itemView.findViewById(R.id.place3rdProfile);
}
}
list_leaderboard_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:background="#EFCDB4">
<RelativeLayout
android:layout_width="32dp"
android:layout_height="match_parent"
android:padding="5dp"
android:background="#FFE5C6"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/leaderboard_position"
android:textStyle="bold"
android:textColor="#000"
android:text="##"
android:layout_centerInParent="true"
android:textSize="18sp"
/>
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:paddingTop="5dp"
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/list_image"
android:src="#mipmap/ic_launcher_round"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#id/leaderboard_position"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/list_username"
android:layout_width="173dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_marginTop="26dp"
android:fontFamily="#font/poppinsmedium"
android:layout_toRightOf="#id/list_image"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Username"
android:textColor="#000"
android:textSize="18sp" />
<TextView
android:id="#+id/list_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_toRightOf="#+id/list_username"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:textColor="#000"
android:visibility="gone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_marginTop="0dp"
android:id="#+id/list_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="25sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PTS"
android:textStyle="bold"
android:fontFamily="#font/poppinsbold"
android:textSize="15sp"
android:textColor="#000"/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_blood_drop"
android:visibility="gone"
/>
</LinearLayout>
fragment_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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=".Leaderboard"
android:background="#790604">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="15dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="L E A D E R B O A R D"
android:fontFamily="#font/poppinsbold"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginBottom="16dp"
android:minHeight="150dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/place2ndProfile"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher_round"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:layout_below="#id/place2ndProfile"
android:fontFamily="#font/poppinsbold"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:textColor="#ffffff"
android:text="2ND"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/place1stProfile"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher_round"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:fontFamily="#font/poppinsbold"
android:layout_below="#id/place1stProfile"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="1ST"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/place3rdProfile"
android:src="#mipmap/ic_launcher_round"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:fontFamily="#font/poppinsbold"
android:layout_below="#id/place3rdProfile"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="3RD"/>
</RelativeLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/leaderboard_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
You should call holder.getAdapterPosition only within the ViewHolder; instead you can simply use the parameter position passed in onBindViewHolder().
#Override
protected void onBindViewHolder(#NonNull UserAdapter.UserViewHolder holder, int position, #NonNull UserModel model) {
// .....
if(position == 0)
{
Glide.with(holder.firstPlace.getContext())
.load(model.getUrl())
.into(holder.firstPlace);
}

Constraint layout has space bottom and top

I'm building an app using Constraint layout, but it has extra space bottom and top. I can't find where they come from. I checked maybe ten times from top to bottom all code but there is no margin or padding or anything else for it.
<?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:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/Brand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/brandname"
style="#style/viewParent.headerText"
android:text="Did"
android:textColor="#color/white" />
<TextView
android:id="#+id/brandname2"
style="#style/viewParent.headerText2"
android:layout_toRightOf="#id/brandname"
android:text="You"
android:textColor="#color/yellow" />
<TextView
style="#style/viewParent.headerText2"
android:layout_toRightOf="#id/brandname2"
android:text="Know!"
android:textColor="#color/red"
android:textStyle="bold|italic" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container_main"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="#drawable/card_bg"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_constraintBottom_toTopOf="#id/guideline2"
app:layout_constraintEnd_toEndOf="#id/guidelineyazisag"
app:layout_constraintStart_toStartOf="#id/guidelineyazisol"
app:layout_constraintTop_toBottomOf="#id/guideline">
<TextView
android:id="#+id/activity_main_text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="simple Title Text"
android:textColor="#5E4C4C"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/activity_main_did_u_know"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Category"
android:textColor="#5E4C4C"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="#id/activity_main_image_view"
app:layout_constraintTop_toBottomOf="#id/activity_main_did_u_know">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/activity_main_image_view"
app:layout_constraintTop_toBottomOf="#id/activity_main_did_u_know">
<TextView
android:id="#+id/factTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:lineSpacingExtra="8dp"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
android:textColor="#000000"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<ImageView
android:id="#+id/activity_main_image_view"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:clickable="true"
android:src="#drawable/ic_share_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/scrollview" />
<ImageView
android:id="#+id/activity_main_fav_button"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_favorite_border_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/scrollview" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/LeftRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline2">
<ImageView
android:id="#+id/activity_main_left_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:clickable="true"
android:src="#drawable/ic_previous"
android:elevation="3dp"
android:background="#drawable/okarka"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/activity_main_right_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:clickable="true"
android:src="#drawable/ic_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:background="#drawable/okarka"
app:layout_constraintStart_toEndOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintLeft_toLeftOf="parent"
ads:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/LeftRight" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.076" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineyazisol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.07" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineyazisag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.93" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried adding guidelines to 0 points in the bottom but it also starts in that space. I gave -(minus) values to margin and padding but doesn't work. I don't know what else to do. You can see from screenshots how it looks.
public class MainActivity extends AppCompatActivity {
private ColorWheel colorWheel = new ColorWheel();
private TextView factTextView;
private TextView textViewId;
private TextView textViewTableName;
ImageView tableImageView;
AdView adView;
ImageView shareImageView;
ImageView favImageView;
float x1,x2,y1,y2;
int i = 1;
int adCounter = 0;
private ConstraintLayout constraintLayout;
String fact;
ArrayList<Fact> favList = new ArrayList<>();
private InterstitialAd mInterstitial;
AdRequest interAdRequest;
int favId;
String favTable;
Typeface typeface;
List<Fact> mData;
private DatabaseFacts db;
GradientDrawable shape;
#SuppressLint({"ClickableViewAccessibility", "SetTextI18n"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseFacts(this);
db.copyDbIfNotExists();
shape = new GradientDrawable();
shape.setCornerRadius(40);
mData = new ArrayList<>();
MobileAds.initialize(this, "ca-app-pub-2469721886989416~7390658870");
adView = findViewById(R.id.adView);
AdRequest bannerAdRequest = new AdRequest.Builder().build();
adView.loadAd(bannerAdRequest);
interAdRequest = new AdRequest.Builder().build();
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId("ca-app-pub-2469721886989416/9441000894");
mInterstitial.loadAd(interAdRequest);
factTextView = findViewById(R.id.factTextView);
constraintLayout = (ConstraintLayout) findViewById(R.id.container_main);
textViewId = findViewById(R.id.activity_main_text_view_id);
favImageView = findViewById(R.id.activity_main_fav_button);
shareImageView = findViewById(R.id.activity_main_image_view);
textViewTableName = findViewById(R.id.activity_main_did_u_know);
// tableImageView = findViewById(R.id.img_user);
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// typeface = getResources().getFont(R.font.sourceserifproregular);
// }
factTextView.setTypeface(typeface);
Intent intent = getIntent();
final int choice = intent.getIntExtra("choice",0);
switch (choice) {
case 1:
textViewTableName.setText("General Facts");
factTextView.setText(db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).getFact());
textViewId.setText("Fact " + (readFromShared("defaultKey") - 1) + " of " + db.getFactsCount(DatabaseFacts.TABLE_GENERAL_NAME));
if (db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).isFavorite() == 0) {
favImageView.setImageResource(R.drawable.ic_favorite_border_black_24dp);
} else {
favImageView.setImageResource(R.drawable.ic_favorite_black_24dp);
}
ImageView leftClick = (ImageView) findViewById(R.id.activity_main_left_button);
leftClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
leftClickMethod("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME);
}
});
ImageView rightClick = (ImageView) findViewById(R.id.activity_main_right_button);
rightClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rightClickMethod("defaultKey",DatabaseFacts.TABLE_GENERAL_NAME);
return;
}
});
favImageView.setOnClickListener(v -> handleFavorites("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME));
break;
Remove app:layout_constraintBottom_toTopOf="#id/guideline" from Brand and app:layout_constraintTop_toBottomOf="#id/LeftRight" from adView. You can't put constraints for Top and Bottom because it will center your View between those two constraints. Same about left and right (start, end), but you have match_parent here, so this problem doesn't occur (visualy - if you set wrap_content, view will go to the center too).

android.widget.RelativeLayout cannot be cast to androidx.recyclerview.widget.RecyclerView

I wrote a little code, but I had a problem. Error that I encounter as: android.widget.RelativeLayout cannot be cast to androidx.recyclerview.widget.RecyclerView
  I looked at the questions that look like this, but the answers that were given did not solve my problem. Can you help me?
Java code as:
public class Fehrest extends AppCompatActivity {
DatabaseManager dbManager = new DatabaseManager();
int tedad,tedad_fav;
private String[] Name, Text,Image,seen, English, Farsi;
private int[] id,fav,id_sen;
Items items = new Items();
SharedPreferences sharedP;
RecyclerView recyclerView;
Story_Adapter adapter;
ArrayList<Book_Items> MyArrayList = new ArrayList<>();
String button_name,tbl_name="story_tbl";
TextView txt_toolbar,txt_sen;
BrokenView brokenView;
BrokenTouchListener listener;
EditText edt_search;
LinearLayout lin_main;
RelativeLayout rlt_buttons;
boolean night_mode,show_english=true;
int day=0,date_mod,today;
Button btn_love,btn_pand,btn_tanz,btn_mazhabi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies_list);
recyclerView = findViewById(R.id.rcl_movies);
edt_search = findViewById(R.id.edt_search);
rlt_buttons = findViewById(R.id.rlt_buttons);
txt_toolbar = findViewById(R.id.txt_toolbar);
lin_main = findViewById(R.id.lin_main);
txt_sen = findViewById(R.id.txt_sen);
btn_love = findViewById(R.id.btn_love);
btn_pand = findViewById(R.id.btn_pand);
btn_mazhabi = findViewById(R.id.btn_mazhabi);
btn_tanz = findViewById(R.id.btn_tanz);
brokenView = BrokenView.add2Window(Fehrest.this);
listener = new BrokenTouchListener.Builder(brokenView).build();
sharedP = getSharedPreferences(Items.SETTINGS, Context.MODE_PRIVATE);
night_mode = sharedP.getBoolean(Items.NIGHT_MODE,false);
today = sharedP.getInt(Items.TODAY,0);
date_mod = sharedP.getInt(Items.DATE_MODIFIED,0);
day = (today - date_mod);
get_night();
Get_sentence();
adapter = new Story_Adapter(this, MyArrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(Fehrest.this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(adapter);
button_name = getIntent().getStringExtra("button");
and this my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fehrest"
android:orientation="vertical"
android:id="#+id/lin_main"
android:background="#drawable/background4">
<LinearLayout
android:id="#+id/lin_toolbar"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="8dp"
android:background="#color/nephritis"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="7"
>
<EditText
android:id="#+id/edt_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="6"
android:background="#drawable/textviewstyle2"
android:gravity="center"
android:hint="جستجو..."
android:padding="4dp"
android:textColorHint="#color/White"
android:textColor="#color/White"
android:visibility="gone"/>
<TextView
android:id="#+id/txt_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="6"
android:text="لیست خانوائخ خاا"
android:textColor="#color/White"
android:textSize="21sp"
android:fontFamily="#font/medium"/>
<ImageView
android:id="#+id/img_search"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/magnify"
android:onClick="img_search_click"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcl_movies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:visibility="gone"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rlt_buttons">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_sen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="20dp"
android:background="#drawable/textviewstyle"
android:fontFamily="#font/zahraroosta"
android:padding="8dp"
android:text="Sentence"
android:textColor="#color/description"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_love"
android:layout_width="290dp"
android:layout_height="70dp"
android:layout_below="#id/txt_sen"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:background="#drawable/back_btn3"
android:fontFamily="#font/boldmayriad"
android:onClick="btn_click"
android:paddingEnd="40dp"
android:text="#string/euphorbiaceae"
android:textAllCaps="false"
android:textColor="#color/White"
android:textSize="24sp" />
This is part of my MLX code and the tags are not closed for this reason.

by clicking on TextView i want to see the list of items

In my android application, i want to display a list of items when i click on a Textview, it display a list of items and i can add and delete items from that list. how can i do it through java code
Kindly guide. i will be very thankful to you
my code is:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cus_name"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/cus_name_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/contact_no"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/contact_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ticket_no"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ticket_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<requestFocus />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="#string/task_detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/task_detail_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
If I understood right I would suggest extending a Dialog to make custom dialog that pops out whenever user clicks on TextView (as included in Imran Khan's answer). This dialog would contain ListView and whatever else you need for handling the list. Example of such approach as I used it some time ago:
public class LogOverlay extends Dialog{
private Server mItem;
private boolean end=false;
private int mLimitHigh = 15;
private int mLimitLow = 0;
private ListView mListView;
private LogListAdapter mAdapter;
private ArrayList<LogUnit> mLog = new ArrayList<LogUnit>();
private boolean mScroll;
private Context context;
ProgressDialog pd;
public LogOverlay(Context context,Session session,Server server) {
super(context);
this.setContentView(R.layout.overlay_logs);
mListView=(ListView) this.findViewById(R.id.log_list);
mAdapter = new LogListAdapter(context, new ArrayList<LogUnit>(), new String[] {});
mListView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
mListView.setDividerHeight(1);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
....
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}

Categories