ScrollView isn`t working. What should I do? - java

How can I scroll in my Android app?
I already tried Scrollview but it doesn`t work.
So this is my MainActivity:
package com.example.myapplication2;
import android.content.DialogInterface;
import android.content.Intent;
import android.icu.text.IDNA;
import android.nfc.Tag;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import static com.example.myapplication2.R.id.*;
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private boolean loadFragment(Fragment fragment){
if(fragment!=null){
getSupportFragmentManager()
.beginTransaction()
.replace(fragment_container, fragment)
.commit();
return true;
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.bottomnav);
navigation.setOnNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction().replace(fragment_container, new Home_Screen()).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment fragment = null;
switch(menuItem.getItemId()){
case kontakt:
break;
case Termine:
fragment = new TermineFragment();
break;
case wilkommen:
fragment = new Home_Screen();
break;
}
return loadFragment(fragment);
}
}
And this is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/darkgrey">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Termine"
android:textSize="30sp"
android:textColor="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
></TextView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="80dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<Button
android:text="Mehr infos"
android:layout_width="120dp"
android:layout_height="35dp"
android:id="#+id/button1"
android:layout_weight="1"
android:layout_marginTop="130dp"
android:layout_marginRight="170dp"
/>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/maincolor"
>
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 29.06.19"
android:textSize="24dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Juni22, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/colorPrimaryDark"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 13.07.19"
android:textSize="24dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="July13, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp"
/>
</android.support.v7.widget.CardView>
</RelativeLayout>
What should I do? I want these Cardviews in my Scrollview.

Replace your XML layout code with the below. Also, be notified, there seems to be an awkward amount of topMargin between your two cards, not sure if that's intended or not but as of now, there is huge gap between your two cards :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/darkgrey">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Termine"
android:textSize="30sp"
android:textColor="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="80dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<Button
android:text="Mehr infos"
android:layout_width="120dp"
android:layout_height="35dp"
android:id="#+id/button1"
android:layout_weight="1"
android:layout_marginTop="130dp"
android:layout_marginRight="170dp" />
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/maincolor">
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 29.06.19"
android:textSize="24dp" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Juni22, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="173dp"
app:cardCornerRadius="8dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/colorPrimaryDark"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="HPI - 13.07.19"
android:textSize="24dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="Sitzung: H-E.51"
android:textSize="16dp"
android:layout_marginTop="40dp"
/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:text="July13, 2019"
android:textSize="16dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="270dp"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>

Use ScrollView in place of RelativeLayout. Add a LinearLayout with vertical orientation before the first TextView and close the LinearLayout after the CardView.

Related

Why do I get a null object reference on my RealtiveLayout.setBackgroundColor although I have initialized it

Here is my java code:
package com.example.project_final;
import static android.content.ContentValues.TAG;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import java.util.ArrayList;
public class history extends AppCompatActivity {
FirebaseFirestore Fstore;
String userID;
FirebaseAuth fAuth ;
int times;
int heart;
int avg_heart;
String date;
int count = 0;
TextView Data;
RelativeLayout [] L = new RelativeLayout[7];
TextView [] hr = new TextView[7];
TextView [] dates = new TextView[7];
ImageView [] img = new ImageView[7];
TextView last;
ArrayList<Integer> intheart = new ArrayList<>();
ArrayList<String> intdates = new ArrayList<>();
TextView average;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Fstore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
userID= fAuth.getUid();
DocumentReference documentReference = Fstore.collection("users").document(userID);
Data = findViewById(R.id.txtNodata);
Intent intent1 = getIntent();
String signup = intent1.getStringExtra("signup");
last = findViewById(R.id.txtlast);
average = findViewById(R.id.txtaverage);
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout"+ i+1, "id", this.getPackageName());
L[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("heartrate"+ i+1, "id", this.getPackageName());
hr[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("txtdate"+ i+1, "id", this.getPackageName());
dates[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("img"+ i+1, "id", this.getPackageName());
img[i] = findViewById(resourceId);
}
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
times = Math.toIntExact(task.getResult().getLong("times"));
avg_heart = Math.toIntExact(task.getResult().getLong("average_heart_rate"));
for (int i = 0 ; i <times ;i++){
intdates.add(task.getResult().getString("date" + i));
intheart.add(Math.toIntExact(task.getResult().getLong("heart"+i)));
}
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!(intheart.isEmpty()||intdates.isEmpty())){
int count =0;
if(times>7){
times=7;
}
last.setText("Last "+ times +" Days history");
for(int i =0 ; i<intdates.size();i++){
if(1.2*avg_heart<intheart.get(i)||0.8*avg_heart<intheart.get(i)){
L[i].setBackgroundColor(R.drawable.color_gradient1);
img[i].setImageResource(R.drawable.sad);
}
else{
L[i].setBackgroundColor(R.drawable.gradient_color);
img[i].setImageResource(R.drawable.happy);
}
dates[i].setText(intdates.get(i));
hr[i].setText(intheart.get(i)+" Bpm");
L[i].setVisibility(View.VISIBLE);
}
}
else{
handler.postDelayed(this, 100);
}
}
}, 100);
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
tools:context=".history">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtlast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text=""
android:textSize="30sp"
android:layout_gravity="center"
android:textColor="#000"
android:textStyle="bold"
/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=""
android:id="#+id/txtaverage"
android:textColor="#000"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
android:visibility="invisible"
>
<TextView
android:id="#+id/txtday1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtdate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday1"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate1"
android:id="#+id/heartrate1"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:background="#drawable/color_gradient1"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 2"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday2"
/>
<TextView
android:id="#+id/txtdate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:id="#+id/heartrate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout3"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 3"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday3"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate3"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate3"
/>
<ImageView
android:id="#+id/img3"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/color_gradient1"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 4"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday4"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate4"
/>
<TextView
android:id="#+id/heartrate4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate4"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img4"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout5"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday5"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate5"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate5"
/>
<ImageView
android:id="#+id/img5"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout6"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 6"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday6"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate6"
android:id="#+id/heartrate6"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img6"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:id="#+id/layout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 7"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday7"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate7"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate7"
/>
<ImageView
android:id="#+id/img7"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<TextView
android:id="#+id/txtNodata"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:fontFamily="#font/lovelo"
android:gravity="center"
android:text="No data Available"
android:textColor="#color/colorRed"
android:textSize="20sp"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
When I run the code I get this error on a null object reference when I try to setbackground color inside the handler:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)'
The code should run fine since I initialized all the views. I've been stuck for a while I don't know where is the problem help.
I think you are not getting the name correct, so it's not initializing it, name must be a string as a whole :
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout" + (i+1).toString(), "id", this.getPackageName());
L[i] = findViewById(resourceId);
}

ActivityBinding Not Recognized

My DetailActivity.java file shows an error by not picking ActivityDetailBinding. Other Activity files are working fine and I put;
buildFeatures{
viewBinding true
}
in build.gradle file before start working on xml files. Only DetailActivity.java file is showing this error.
Error displaying is;
import com.example.juizeesapp.databinding.ActivityDetailBinding; ^ symbol: class ActivityDetailBinding location: package com.example.juizeesapp.databinding
Please look into this help me through this. TIA.
Here is my code.
activity_detail.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DetailActivity"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/detailImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/apple" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Juice Name"
android:textColor="#000000"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/subtract"
android:layout_width="30dp"
android:layout_height="wrap_content"
app:srcCompat="#drawable/minus" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/add"
android:layout_width="30dp"
android:layout_height="wrap_content"
app:srcCompat="#drawable/plus" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<TextView
android:id="#+id/detailDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam iaculis." />
<EditText
android:id="#+id/nameBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/edit_textbackground"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:minHeight="48dp"
android:padding="10dp" />
<EditText
android:id="#+id/phoneBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/edit_textbackground"
android:ems="10"
android:hint="Phone Number"
android:inputType="phone"
android:minHeight="48dp"
android:padding="10dp" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:gravity="center"
android:text="Price"
android:textColor="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs. "
android:textColor="#color/red"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="#+id/priceLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="45"
android:textColor="#color/red"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="#+id/insertBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/btn_background"
android:text="Place Your Order" />
</LinearLayout>
</LinearLayout>
DetailActivity.java
package com.example.juizeesapp;
import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.juizeesapp.databinding.ActivityDetailBinding;
public class DetailActivity extends AppCompatActivity {
ActivityDetailBinding binding;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityDetailBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
int image = getIntent().getIntExtra("image" , 0);
int price = Integer.parseInt(getIntent().getStringExtra("price"));
String name = getIntent().getStringExtra("name");
String description = getIntent().getStringExtra("desc");
binding.detailImage.setImageResource(image);
binding.priceLbl.setText(String.format("%d", price));
binding.nameBox.setText(name);
binding.detailDescription.setText(description);
}
}
you have not declared binding
ActivityDetailBinding binding;
It was a typo error in the manifest file. I stated .DetailActivity at the manifest file is;
<activity
android:name=".DetailActivity"
android:exported="false" />
But when I changed it into;
<activity android:name=".DetailActivity"></activity>
Problem solved.

Android Studio Code in Java to take user input and display output

Here I am trying to take user input and display output in a Text View, but checkgen and checkuser is returning -1 despite of selecting them and also both the radio buttons are getting selected not any one of them, I want to display all the details entered in the text view. I have pasted the activity_reg.xml code below too
reg.java
package com.example.bloodbank;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class reg extends AppCompatActivity {
//login button
Button btn_login;
//firstname
EditText fn;
//lastname
EditText ln;
//phone number
EditText pn;
//output
TextView op;
//registration button
Button btn_reg;
//radio group for gender
RadioGroup rggen;
//radio group for user(donor/in search of blood)
RadioGroup rguser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
btn_login=findViewById(R.id.btn_login);
fn=findViewById(R.id.txtfirstname);
ln=findViewById(R.id.txtlastname);
pn=findViewById(R.id.txtmobile);
op=findViewById(R.id.outputreg);
rggen=findViewById(R.id.rggender);
rguser=findViewById(R.id.rgreg);
btn_reg=findViewById(R.id.main_btn_reg);
btn_reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String fname=fn.getText().toString();
String lname=ln.getText().toString();
int pno= new Integer(pn.getText().toString());
int checkgen=rggen.getCheckedRadioButtonId();
//Toast.makeText(reg.this, "checkgen= "+checkgen, Toast.LENGTH_SHORT).show();
int checkuser=rguser.getCheckedRadioButtonId();
//Toast.makeText(reg.this, "checkuser= "+checkuser, Toast.LENGTH_SHORT).show();
if(((checkgen == -1)||(checkuser == -1))||((checkgen == -1)&&(checkuser == -1)))
{
//no radio button selected
Toast.makeText(reg.this, "Please select an option", Toast.LENGTH_SHORT).show();
}
else {
//radio button is checked
findRadioButton1(checkgen);
findRadioButton2(checkuser);
op.setText("Welcome " + fname + " " + lname + "Phone Number: " +pno );
}
}
});
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(reg.this,login.class);
startActivity(i);
}
});
}
private void findRadioButton2(int checkuser) {
switch(checkuser){
case R.id.rbdonor:
op.setText("\nUser: Donor\n");
break;
case R.id.rbneedy:
op.setText("\nUser: In search of blood\n");
break;
}
}
private void findRadioButton1(int checkgen) {
switch(checkgen){
case R.id.rbmale:
op.setText("\nGender: Male\n");
break;
case R.id.rbfemale:
op.setText("\nGender: Female\n");
break;
}
}
}
activity_reg.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rbmale"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".reg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="23dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtregister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Registration"
android:textColor="#color/colorPrimaryDark"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtfirstname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="First Name*"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtlastname"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Last Name*"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
>
<TextView
android:id="#+id/textView"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text=" Gender*:"
android:textColor="#A31D13" />
<RadioGroup
android:id="#+id/rggender"
android:layout_width="309dp"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male"
android:textColor="#BE202F" />
<RadioButton
android:id="#+id/rbfemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female"
android:textColor="#BE202F" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>
<EditText
android:id="#+id/txtmobile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Mobile Number*"
android:inputType="number"
android:maxLength="10"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<EditText
android:id="#+id/txtpassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:hint="Password*"
android:inputType="textPassword"
android:maxLength="10"
android:paddingLeft="8dp"
android:paddingTop="15dp"
android:singleLine="true"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="13dp" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="111dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:text=" Register as*:"
android:textColor="#BE202F" />
<RadioGroup
android:id="#+id/rgreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rbdonor"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="I am a donor"
android:textColor="#BE202F" />
<RadioButton
android:id="#+id/rbneedy"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="I am in search of blood"
android:textColor="#BE202F" />
</LinearLayout>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="1dp"
android:orientation="horizontal">
</LinearLayout>
<Button
android:id="#+id/main_btn_reg"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="20dp"
android:background="#color/colorPrimaryDark"
android:text="REGISTER"
android:textColor="#color/white"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0.6dp"
android:layout_marginTop="16dp"
android:background="#color/colorPrimaryDark"></View>
<Button
android:id="#+id/btn_login"
android:layout_width="86dp"
android:layout_height="44dp"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="LOGIN"
android:textColor="#color/colorPrimaryDark"
android:textSize="14dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/outputreg"
android:layout_width="match_parent"
android:layout_height="60dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> ```

After trying a long not able to provide space between 2 card view elements inside a recycler view for both horizontal and vertical [duplicate]

This question already has answers here:
How to add dividers and spaces between items in RecyclerView
(45 answers)
Closed 2 years ago.
I want to add some space between 2 cards in both the horizontal recycler view and the vert recycler view but not able to add tried everything but won't getting a sol as u can see in the below image For horizontal both the cards are over each other.
My XML code in which I had defined recycler view Both hor. and ver. :-
<?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/AppBackground">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/AppBackground">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="#+id/nestedsview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appBar">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Looking for a delecious cake?"
android:textColor="#android:color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#id/recyclerViewHorizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="More"
android:textColor="#android:color/black"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp"
android:layout_marginEnd="20dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="266dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
app:layout_constraintBottom_toTopOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear"
tools:context=".FirstActivity">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="10dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Best Selling"
android:textColor="#android:color/black"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/recyclerViewVertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/recyclerViewHorizontal" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewVertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6"
tools:context=".FirstActivity"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My Xml Code for card :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="170dp"
android:layout_height="220dp"
android:padding="10dp"
android:elevation="10dp"
app:cardBackgroundColor="#color/CardColor"
app:cardCornerRadius="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:background="#color/CardColor"
android:padding="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="90dp"
android:layout_height="100dp"
android:foregroundGravity="center_horizontal"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:gravity="center"
android:text="Great"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textViewForPieceInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Ram"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textViewforPrice"
android:text="Shyam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewForPieceInfo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
My recycler view class code:
package com.example.cako;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class FirstActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
String []x={"Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Abhinav","Ankit"};
String []image= {"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Aloo_Methi_%28Aaloo_Methi%29.JPG/180px-Aloo_Methi_%28Aaloo_Methi%29.JPG",
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Spicy_alloo_with_tadka_mirchi.jpg/180px-Spicy_alloo_with_tadka_mirchi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Aloo_gobi.jpg/180px-Aloo_gobi.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Aloo_Tikki_served_with_chutneys.jpg/180px-Aloo_Tikki_served_with_chutneys.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Aloo_Mattar.jpg/180px-Aloo_Mattar.jpg"
};
Toolbar toolbar=findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
RecyclerView horizontalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewHorizontal);
horizontalrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));
horizontalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
RecyclerView verticalrecyclerView =(RecyclerView)findViewById(R.id.recyclerViewVertical);
verticalrecyclerView.setLayoutManager(new GridLayoutManager(this,2));
verticalrecyclerView.setAdapter(new RecyclerVview(this,x,image));
}
}
class RecyclerVview extends RecyclerView.Adapter<RecyclerVview.Holder>{
Activity co;
String[] name;
String[] image;
public RecyclerVview(Context applicationContext, String[] x, String[] image) {
co=(Activity)applicationContext;
name=x;
this.image=image;
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater.from(co).inflate(R.layout.layout_for_horizontal_recyclerview,parent,false);
return new Holder(v);
}
#Override
public void onBindViewHolder(#NonNull final Holder holder, int position) {
holder.textViewForItemName.setText(name[position]);
Glide.with(co).load(image[position]).into(holder.image);
holder.floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(co, "yesWorking", Toast.LENGTH_SHORT).show();
Log.i("yes","log");
}
});
holder.textViewForPrice.setText("30$");
holder.textViewForPieceInfo.setText("Single Piece Only.");
}
#Override
public int getItemCount() {
return name.length;
}
public class Holder extends RecyclerView.ViewHolder{
ImageView image;
TextView textViewForItemName;
FloatingActionButton floatingActionButton;
TextView textViewForPrice;
TextView textViewForPieceInfo;
public Holder(#NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.imageView);
textViewForItemName=itemView.findViewById(R.id.textView4);
floatingActionButton=itemView.findViewById(R.id.floatingActionButton);
textViewForPieceInfo=itemView.findViewById(R.id.textViewForPieceInfo);
textViewForPrice=itemView.findViewById(R.id.textViewforPrice);
}
}
}
Add margin for the CarView layout and inside the ContraintLayout
and Use this for your card.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:margin="8dp"
>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="170dp"
android:layout_height="220dp"
android:margin="8dp"
android:elevation="10dp"
app:cardBackgroundColor="#color/CardColor"
app:cardCornerRadius="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraint"
android:layout_width="130dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:background="#color/CardColor"
android:padding="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="90dp"
android:layout_height="100dp"
android:foregroundGravity="center_horizontal"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:gravity="center"
android:text="Great"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textViewForPieceInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
android:text="Ram"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textViewforPrice"
android:text="Shyam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/playfair_display_bold_italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewForPieceInfo" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="2dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Relative layout alignment in android

In the below posted code, i'm not able to see the text in single line and at a time i want only 6 text to be displayed, but i'm able to see morethan 20 text's. can you have a look once why it is..
i need my view to be like this, full view should be covered by 6 texts only 3 by 3
|afsafasfaf afasfaffasfas|
| |
|cascsdfs asdaddasdada|
| |
Mainactivity.java
package com.example.sampleoporj;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
Button mbutton;
MainActivity activity;
LinearLayout mEventsLayout;
HorizontalScrollView mHorizontalscroll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbutton = (Button)findViewById(R.id.button1);
mEventsLayout = (LinearLayout)findViewById(R.id.eventslayout);
mbutton.setOnClickListener(btnlistener);
activity = this;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
OnClickListener btnlistener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0 ;i <10 ; i++)
{
LayoutInflater inflater = activity.getLayoutInflater();
View view = (View)inflater.inflate(R.layout.samplelayout,null);
mEventsLayout.addView(view);
}
}
};
}
Activity_main.xml
<RelativeLayout 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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/helloworld"
android:text="#string/hello_world" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/helloworld"
android:id="#+id/horizontalscroll">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/eventslayout"></LinearLayout>
</HorizontalScrollView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_toLeftOf="#+id/helloworld"
android:text="Button" />
</RelativeLayout>
Samplelayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".45"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".10"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".45"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
i request the output should look like this, i need a horizontal scroll & at a time i need only 3 + 3 text to display (in image i have showed only 2 + 2 ). As i notice horizontal scroll shrinks if i add linearlayout with "fill_parent" param.

Categories

Resources