EditText doesn't update its text - java

EDIT2:
Using
fragTransaction.add(fragment, tag);
instead of
fragTransaction.add(id, fragment);
OR
Removing all Fragments code in MainActivity makes the app works again, for some odd reason.
Getting the text via .getText().toString() returns null/empty when button is pressed.
Tried to manually .setText("something"), it works when .getText().toString() is called but,
when the text is modified, calling .getText().toString() will still return "something".
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
Fragment loginFrag = new LoginFragment();
fragTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragTransaction.add(R.id.mainFrag, loginFrag);
fragTransaction.commit();
}
LoginFragment.java
public class LoginFragment extends Fragment{
EditText fieldID;
EditText fieldPassword;
Button logButton;
private FirebaseAuth mAuth;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.login_menu, container, false);
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
mAuth = FirebaseAuth.getInstance();
view = v;
fieldID = v.findViewById(R.id.fieldID);
fieldPassword = v.findViewById(R.id.fieldPassword);
logButton = v.findViewById(R.id.button_login);
logButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
signIn(fieldID.getText().toString(), fieldPassword.getText().toString());
}
});
}
private void signIn(String email, String password) {
System.out.println("Email: " + email);
System.out.println("Pass: " + password);
if(!email.isEmpty() && !password.isEmpty()) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("Auth", "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
view.findViewById(R.id.logoMain).setVisibility(View.GONE);
} else {
// If sign in fails, display a message to the user.
Log.w("Auth", "signInWithEmail:failure", task.getException());
//updateUI(null);
}
}
});
}
}
}
Main Activity
<?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"
tools:context=".presenter.Activity.MainActivity">
<fragment
android:id="#+id/mainFrag"
android:name="com.raze.mfa.presenter.Fragment.LoginFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="110dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logoMain"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/bak"
tools:srcCompat="#drawable/bak" />
<ImageView
android:id="#+id/iconlock"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/fieldPassword"
app:layout_constraintHorizontal_bias="0.523"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iconuser"
app:srcCompat="#mipmap/lockicon" />
<ImageView
android:id="#+id/iconuser"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="320dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/fieldID"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/usericon" />
<Button
android:id="#+id/button_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/loginText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fieldPassword"
app:layout_constraintVertical_bias="0.188" />
<EditText
android:id="#+id/fieldPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="72dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fieldID" />
<EditText
android:id="#+id/fieldID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="320dp"
android:layout_marginEnd="72dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/logoMain"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="108dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/fieldID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/logoimg" />
<Button
android:id="#+id/button_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_login"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Unrelated:
I've Googled mostly everything but it seems that I haven't understand the basics of using Fragments, Activites, context, Views, and etc.

Ok I see, you are using a <fragment/> tag already in your MainActivity layout, and then you add with the FragmentTransaction a fragment to it.
The transaction is for FrameLayout containers that are empty and take a fragment. When you add the fragment statically through the <fragment/> tag the fragment is already loaded into the screen.
Something weird was happening when you added a new fragment to a fragment tag, maybe it duplicated the fragment and it was reading the one that is behind the actual fragment and it wasn't getting the input from the user.
Read here for the proper use of FragmentTransaction: https://developer.android.com/training/basics/fragments/fragment-ui
Taken from the docs above, see how they use a FrameLayout as the container.
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Java:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
}

Related

RecyclerView only lists first item despite layout changes

I understand this might be a duplicate, but every other solution here didn't work in my case, such as changing the layout_height to wrap_content instead of match_parent.
My Firestore database has 2 values stored inside an array, and I want to display both of them (more in the future) in my RecyclerView, however, it only displays the first value, at position 0.
Here is my Activity where the Recycler and the Adapter are created:
ActivityOs
private FirestoreAdapter listAdapter;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference osRef = db.collection("cliente");
private DocumentReference docRef = osRef.document("clienteTeste");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_os);
RecyclerView recyclerView = findViewById(R.id.recyclerViewOs);
/*docRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e("RESULTADO","result:" + document.toString());
}
}
});*/
Query query = osRef.orderBy("serviceOrder",
Query.Direction.ASCENDING);
FirestoreRecyclerOptions<ServiceDocument> options =
new FirestoreRecyclerOptions.Builder<ServiceDocument>()
.setQuery(osRef, ServiceDocument.class)
.build();
listAdapter = new FirestoreAdapter(options);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
#Override
protected void onStart() {
super.onStart();
listAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
listAdapter.stopListening();
}
The commented code shows me that the database does indeed return the 2 values stored in it.
Here is my Adapter class:
public class FirestoreAdapter extends FirestoreRecyclerAdapter<ServiceDocument, FirestoreAdapter.ViewHolder> {
public FirestoreAdapter(#NonNull FirestoreRecyclerOptions<ServiceDocument> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull ServiceDocument model) {
holder.osIdItem.setText(String.valueOf(model.serviceOrderArray.get(position).getId()));
holder.osClientItem.setText(model.serviceOrderArray.get(position).getClient().getName());
holder.osDateItem.setText(model.serviceOrderArray.get(position).getDate());
holder.osValueItem.setText(String.valueOf(model.serviceOrderArray.get(position).getTotalValue()));
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.layout_os_item, parent, false);
return new FirestoreAdapter.ViewHolder(view);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView osIdItem;
private TextView osClientItem;
private TextView osDateItem;
private TextView osValueItem;
public ViewHolder(View itemView) {
super(itemView);
osIdItem = itemView.findViewById(R.id.osIDItem);
osClientItem = itemView.findViewById(R.id.osClientNameItem);
osDateItem = itemView.findViewById(R.id.osDateItem);
osValueItem = itemView.findViewById(R.id.osValueItem);
}
}
}
And here are my layouts, activity_os.xml where my RecyclerView is created, and layout_os_item.xml where the layouts for each item of the recycler are made.
Activity_os.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/activity_os"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:background="#color/white"
tools:context="com.leonardomaito.autocommobile.activities.OsActivity">
<include
android:id="#+id/include"
layout="#layout/layout_header_search" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/glRecyclerLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="124dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewOs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="650dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.0"
tools:listitem="#layout/layout_os_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/btNewOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Inserir OS"
android:onClick="createNewOs"
android:src="#drawable/custom_plus_icon"
app:backgroundTint="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.98"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.988"
app:rippleColor="#color/autocom_blue" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout_os_item.xml
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:showIn="#layout/activity_os"
android:background="#drawable/custom_card_view">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="#drawable/custom_card_view">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/custom_os_icon"
android:paddingLeft="5dp"/>
<TextView
android:id="#+id/osIDItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="N 00000"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/osDateItem"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/osDateItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.943"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.222" />
<TextView
android:id="#+id/osClientNameItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toBottomOf="#+id/osIDItem"
app:layout_constraintVertical_bias="0.111" />
<TextView
android:id="#+id/osValueItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="R$ 10,000"
android:textColor="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.791"
app:layout_constraintStart_toEndOf="#+id/osClientNameItem"
app:layout_constraintTop_toBottomOf="#+id/osDateItem"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Any help is welcome!
Edit
Current Result:
Expected Result
Basically, Item 0 or the first item is correct, however, the second item in my database should also appear in Item 1.

Sending Data from Alert Dialog to New Activity

I am new to Android development and I am trying to send data via EditText to TextView from an alert dialog inflated in the MainActivity to a different activity (NewGame.java). I have tried intent.putExtra (see below) however I am getting null pointer exceptions as it seems I cannot access the views from the dialog layout xml file.
I know there are a few other ways to create an alert dialog so perhaps this way is not best for what i am trying to achieve?
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button newGame, prevGames, newSquad, savedSquads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newGame = findViewById(R.id.buttonNewGame);
newGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
EditText enterTeam1, enterTeam2, enterDate, enterTime, enterLocation;
enterTeam1 = myView.findViewById(R.id.etTeam1);
enterTeam2 = myView.findViewById(R.id.etTeam2);
enterDate = myView.findViewById(R.id.etDate);
enterTime = myView.findViewById(R.id.etTime);
enterLocation = myView.findViewById(R.id.etLocation);
builder.setTitle("New Game")
.setCancelable(true)
.setPositiveButton("Start", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(MainActivity.this, NewGame.class);
intent.putExtra("TEAM1", enterTeam1.getText().toString());
intent.putExtra("TEAM2", enterTeam2.getText().toString());
intent.putExtra("DATE", enterDate.getText().toString());
intent.putExtra("TIME", enterTime.getText().toString());
intent.putExtra("LOCATION", enterLocation.getText().toString());
startActivity(intent);
}
})
.setNegativeButton("Cancel", null);
builder.create();
builder.show();
}
});
NewGame.java
public class NewGame extends AppCompatActivity {
TextView team1, team2, date, time, location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
team1 = findViewById(R.id.tvTeam1);
team2 = findViewById(R.id.tvTeam2);
date = findViewById(R.id.tvDate);
time = findViewById(R.id.tvTime);
location = findViewById(R.id.tvLocation);
team1.setText(getIntent().getStringExtra("TEAM1"));
team2.setText(getIntent().getStringExtra("TEAM2"));
date.setText(getIntent().getStringExtra("DATE"));
time.setText(getIntent().getStringExtra("TIME"));
location.setText(getIntent().getStringExtra("LOCATION"));
}
}
dialog.xml
<?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:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvNewGame"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="#color/black"
android:textSize="30dp" />
<EditText
android:id="#+id/etTeam1"
android:layout_width="211dp"
android:layout_height="53dp"
android:ems="10"
android:hint="Team 1"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etTeam2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Team 2"
android:inputType="textPersonName"/>
<EditText
android:id="#+id/etDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="#+id/etTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Time"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Location"
android:inputType="textPersonName"/>
</LinearLayout>
activity_new_game.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewGame">
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="time"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvNewGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="#color/black"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.085" />
<TextView
android:id="#+id/tvTeam1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="team1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.192" />
</androidx.constraintlayout.widget.ConstraintLayout>
I believe you are having this problem because you are actually inflating 2 views. And referencing the wrong one when you use findById.
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
should look like this
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
builder.setView(myView);

How to remove empty space when visibility is set to GONE

I've set the visibility of RecyclerView to gone. How would I be able to remove the empty space it takes?
I tried putting layout_height as wrap_content, but it still doesn't show anything.
Recycler View:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Card View:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="250dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:id="#+id/cardView"
android:backgroundTint="#5A10E7"
android:elevation="90dp"
android:orientation="vertical"
android:textColor="#FFFFFF"
app:cardCornerRadius="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.282">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/Requestfulfilled"
android:layout_width="139dp"
android:layout_height="41dp"
android:layout_weight="1"
android:backgroundTint="#4CAF50"
android:clickable="true"
android:defaultFocusHighlightEnabled="true"
android:hint="Request Fullfilled"
android:text="Fulfilled"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/DeleteRequest"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/RlocationView"
app:layout_constraintTop_toBottomOf="#+id/RlocationView" />
<Button
android:id="#+id/DeleteRequest"
android:layout_width="139dp"
android:layout_height="41dp"
android:backgroundTint="#F44336"
android:clickable="true"
android:defaultFocusHighlightEnabled="true"
android:hint="Delete"
android:text="Delete"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/RlocationView"
app:layout_constraintHorizontal_bias="0.894"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.904" />
<TextView
android:id="#+id/RemailView"
android:layout_width="340dp"
android:layout_height="35dp"
android:allowUndo="true"
android:focusable="auto"
android:focusableInTouchMode="true"
android:text="Email"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/RdateView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/RbloodView"
android:layout_width="116dp"
android:layout_height="35dp"
android:allowUndo="true"
android:focusable="auto"
android:focusableInTouchMode="true"
android:text="Blood Group"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/RlocationView"
app:layout_constraintEnd_toStartOf="#+id/RdateView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/RemailView" />
<TextView
android:id="#+id/RdateView"
android:layout_width="164dp"
android:layout_height="42dp"
android:allowUndo="true"
android:focusable="auto"
android:focusableInTouchMode="true"
android:text="Date of Requirement"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/RlocationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/RbloodView"
app:layout_constraintTop_toBottomOf="#+id/RemailView" />
<TextView
android:id="#+id/RlocationView"
android:layout_width="340dp"
android:layout_height="35dp"
android:allowUndo="true"
android:focusable="auto"
android:focusableInTouchMode="true"
android:text="Location"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/Requestfulfilled"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/RdateView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Code to set Card visibility as GONE:
public class yourRequestActivity extends AppCompatActivity {
private RecyclerView cardView;
private DatabaseReference dbRefForReq, dbRefForResp;
private FirebaseAuth mAuth;
private String AuthUserEmail, UserID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.request_response_view);
mAuth = FirebaseAuth.getInstance();
AuthUserEmail = mAuth.getCurrentUser().getEmail();
UserID = mAuth.getCurrentUser().getUid();
dbRefForResp = FirebaseDatabase.getInstance().getReference().child("Responses").child(String.valueOf(UserID));
dbRefForReq = FirebaseDatabase.getInstance().getReference().child("Blood Requests");
dbRefForReq.keepSynced(true);
cardView = (RecyclerView) findViewById(R.id.recycleView);
cardView.setHasFixedSize(true);
cardView.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<getDbContents> options =
new FirebaseRecyclerOptions.Builder<getDbContents>()
.setQuery(dbRefForReq, getDbContents.class)
.build();
FirebaseRecyclerAdapter<getDbContents, contentHolder> adapter = new FirebaseRecyclerAdapter<getDbContents, contentHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final contentHolder holder, final int position, #NonNull final getDbContents model) {
final int finalPosition = position + 1;
if (model.getUser().equals(AuthUserEmail)) {
dbRefForReq.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
DataSnapshot checkFulfillment = dataSnapshot.child(String.valueOf(("Request " + finalPosition))).child("Fulfillment");
if (checkFulfillment.exists()) {
if (checkFulfillment.child("Fulfilled").getValue().equals("yes")) {
/*------------- Set current user card as visible -----------------*/
holder.frameView.setVisibility(View.VISIBLE);
holder.fulfilled.setVisibility(View.GONE);
holder.requestFulfilled.setVisibility(View.VISIBLE);
holder.userEmail.setText(AuthUserEmail);
holder.bloodGroup.setText(model.getBloodGroup());
holder.dateOfRequirement.setText(model.getDate());
holder.Location.setText(model.getLocation());
////////////////////////////////////////////////////////////////////
}
} else {
holder.frameView.setVisibility(View.VISIBLE);
holder.fulfilled.setVisibility(View.VISIBLE);
holder.requestFulfilled.setVisibility(View.GONE);
/*------------- Set current user card as visible -----------------*/
holder.userEmail.setText(AuthUserEmail);
holder.bloodGroup.setText(model.getBloodGroup());
holder.dateOfRequirement.setText(model.getDate());
holder.Location.setText(model.getLocation());
////////////////////////////////////////////////////////////////////
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
} else if (model.getUser().equals(null)) {
holder.userEmail.setText("You have not posted any request");
} else {
holder.frameView.setVisibility(View.GONE);
}
/* Fulfilled and Delete button Functionality */
holder.fulfilled.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Map<String, Object> response = new HashMap<>();
response.put("Fulfilled", "yes");
/*-----------------Remove Responses------------------*/
dbRefForResp.removeValue();
///////////////////////////////////////////////////////
/*-----------------Update and insert child with fulfilled status as yes------------------*/
dbRefForReq.child(String.valueOf(("Request " + finalPosition))).child("Fulfillment").setValue(response).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(yourRequestActivity.this, "Request fulfillment status posted", Toast.LENGTH_SHORT).show();
}
});
////////////////////////////////////////////////////////////////////
holder.fulfilled.setVisibility(View.GONE);
holder.requestFulfilled.setVisibility(View.VISIBLE);
}
});
}
#NonNull
#Override
public contentHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.your_request_card, viewGroup, false);
contentHolder contentViewHolder = new contentHolder(view);
return contentViewHolder;
}
};
cardView.setAdapter(adapter);
adapter.startListening();
}
public class contentHolder extends RecyclerView.ViewHolder {
TextView userEmail, bloodGroup, dateOfRequirement, Location, requestFulfilled;
Button fulfilled;
FrameLayout frameView;
public contentHolder(#NonNull View itemView) {
super(itemView);
userEmail = itemView.findViewById(R.id.RemailView);
bloodGroup = itemView.findViewById(R.id.RbloodView);
dateOfRequirement = itemView.findViewById(R.id.RdateView);
Location = itemView.findViewById(R.id.RlocationView);
requestFulfilled = (TextView) itemView.findViewById(R.id.fulfilledTextMessage);
frameView = (FrameLayout) itemView.findViewById(R.id.cardFrame);
fulfilled = (Button) itemView.findViewById(R.id.Requestfulfilled);
}
}
}
I want to remove the blank space which occurs while visibility is set to GONE, as shown in the snapshot below.
Image of empty space on removing top card
Image before setting visibility GONE
Simply remove the object from the list if its user is not equal to AuthUserEmail and notify your adapter.
NO NEED TO MAKE VISBILITY GONE OF THE HOLDER VIEW
Your RecyclerView takes space even if visibility is set to gone. It still takes space because there are items that RecyclerView wants to display. Although you can fix this by removing the items RecyclerView wants to show. It would take a lot of memory if you want show that RecyclerView again because you still have to add the items back. What I would recommend is to wrap your RecyclerView with a layout eg. LinearLayout and set that layout's visibility to gone.
Try doing this:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Place your RecyclerView here -->
<!-- change visibility of container view -->
</LinearLayout>
*Edit: You didn't explain it well. I get what you want now. You simply have to remove the item from your RecyclerView list. The best way to do this is by using DiffUtil. This a great tutorial online showing how you can implement it in Java. https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-1-introduction
Cover your RecyclerView with a FrameLayout and then set your visibility over there.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
If this doesnt works then:
Cover your Cardview which is the layout that you designed for your Recyclerview with a FrameLayout. Anyone of them will surely work out for you.

Bottom Sheet Fragment causes error when called the second time

I have a class BottomSheetFragment extends BottomSheetDialogFragment, which contains sub-fragments, and I call it from a fragment.
The problem is that when I call it the second time, my app crashes with Binary XML file line #69: Duplicate id 0x7f090071, tag null, or parent id 0xffffffff with another fragmenterror.
Bottom sheet class:
public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
return v;
}
}
In its XML it contains a sub-fragment public class CreateNotesFragment extends android.support.v4.app.Fragment
The BottomSheetFragment class is called in another fragment on button click like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_works, container, false);
createNew = view.findViewById(R.id.add_file_btn);
createNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.showBottomSheetDialogFragment();
}
});
return view;
}
And here is the method in MainActivity which shows the bottom sheet:
public void showBottomSheetDialogFragment() {
bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
I assume that the CreateNotesFragment no longer exists when I call the BottomSheetFragment for the second time for some reason, but how can I fix it?
Thank you.
Update
Here is the line 69 in fragment_bottom_sheet.xml
<fragment
android:id="#+id/notes_fragment"
android:name="com.app.parsec.secondary_fragments.CreateNotesFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider9"
tools:layout="#layout/fragment_create_notes" />
And the whole XML to make you sure that there is no ID duplicates:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".secondary_fragments.BottomSheetFragment"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:id="#+id/wasd">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/notes_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorPrimary"
android:drawableTop="#drawable/ic_note"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="10dp"
android:text="НОТЫ"
app:layout_constraintBottom_toBottomOf="#+id/text_btn"
app:layout_constraintEnd_toStartOf="#+id/text_btn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/text_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorAccentDark"
android:drawableTop="#drawable/ic_t"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="10dp"
android:text="ТЕКСТ"
app:layout_constraintEnd_toStartOf="#+id/project_btn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/notes_btn"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/project_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorAccentDark"
android:drawableTop="#drawable/ic_shape_1"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="10dp"
android:text="ПРОЕКТ"
app:layout_constraintBottom_toBottomOf="#+id/text_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/text_btn"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/divider9"
android:layout_width="0dp"
android:layout_height="3dp"
android:background="#color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_btn" />
<fragment
android:id="#+id/notes_fragment"
android:name="com.app.parsec.secondary_fragments.CreateNotesFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider9"
tools:layout="#layout/fragment_create_notes" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
I also noticed that if I remove this fragment from XML, I can open BottomSheetFragment without any crashes, so the problem must be it the sub-fragment.
Solved
The solution is adding fragments into a fragment dynamically, not through XML. So, my BottomSheetFragment now looks like this:
public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
getChildFragmentManager().beginTransaction().replace(R.id.fragment_here, new CreateNotesFragment()).commit();
return v;
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".secondary_fragments.BottomSheetFragment"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:id="#+id/wasd">
<android.support.constraint.ConstraintLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/notes_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorPrimary"
android:drawableTop="#drawable/ic_note"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="10dp"
android:text="НОТЫ"
app:layout_constraintBottom_toBottomOf="#+id/text_btn"
app:layout_constraintEnd_toStartOf="#+id/text_btn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/text_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorAccentDark"
android:drawableTop="#drawable/ic_t"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="10dp"
android:text="ТЕКСТ"
app:layout_constraintEnd_toStartOf="#+id/project_btn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/notes_btn"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/project_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorAccentDark"
android:drawableTop="#drawable/ic_shape_1"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="10dp"
android:text="ПРОЕКТ"
app:layout_constraintBottom_toBottomOf="#+id/text_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/text_btn"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/divider9"
android:layout_width="0dp"
android:layout_height="3dp"
android:background="#color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_btn" />
<FrameLayout
android:id="#+id/fragment_here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider9">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
You are trying to open the same fragment. The current fragment should be dismissed. bottomSheetFragment.dismiss(); then bottomSheetFragment.show(getSupportFragmentManager(), tag)
And you can try to give a different tag to fragment.
public void showBottomSheetDialogFragment() {
bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
Edit :
This error occurs on Nested Fragments which has been defined in layout, try to remove your Fragment from XML Layout and replace it with a FrameLayout then instantiate your Fragment dynamically in code.
You are calling fragment tag inside XML file of YourFragment, basically, it's a wrong way to add bottom sheet to fragment. Here is the way you can do it:
Call this method on botton Click:
callToGuideShareDialog();
Inflate the bottom Sheet View inside this method
private void callToGuideShareDialog() {
final View viewBottom = DataBindingUtil.inflate(getLayoutInflater(), R.layout.------, null, false).getRoot();
ImageView imageView = viewBottom.findViewById(R.id.img_size_guide_share);
TextView textViewMsg = viewBottom.findViewById(R.id.----);
viewBottom.findViewById(R.id.-----).setOnClickListener(this);
TextView textViewMeasurement = viewBottom.findViewById(R.id.----);
sheetDialog = new BottomSheetDialog(context);
sheetDialog.setContentView(viewBottom);
viewBottom.post(new Runnable() {
#Override
public void run() {
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) viewBottom.getParent());
mBehavior.setPeekHeight(viewBottom.getHeight());
}
});
if (sheetDialog != null)
sheetDialog.show();
}

Android app NPE: Unable to invoke virtual method setOnClickListener on null object

I'm building my first Android app (basic calculator) which consists of one Activity and a single fragment. The layout of the app (display and buttons) are defined in the fragment xml fragment_main.xml and should be inflated on startup. However, the app immediately crashed with a NPE. I'm confused as to whether I should set the click listeners for the calculator buttons in the Activity's onCreate() method, or in the fragment class's onCreateView() method. Code and stack trace below.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.github.idclark.calculator/com.github.idclark.calculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
public class MainActivity extends ActionBarActivity implements OnClickListener {
private TextView mCalculatorDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new CalculatorFragment())
.commit();
}
mCalculatorDisplay = (TextView) findViewById(R.id.textView);
findViewById(R.id.AC).setOnClickListener(this);
findViewById(R.id.plusminus).setOnClickListener(this);
findViewById(R.id.percent).setOnClickListener(this);
findViewById(R.id.nine).setOnClickListener(this);
findViewById(R.id.eight).setOnClickListener(this);
findViewById(R.id.seven).setOnClickListener(this);
findViewById(R.id.six).setOnClickListener(this);
findViewById(R.id.five).setOnClickListener(this);
findViewById(R.id.four).setOnClickListener(this);
findViewById(R.id.three).setOnClickListener(this);
findViewById(R.id.two).setOnClickListener(this);
findViewById(R.id.one).setOnClickListener(this);
findViewById(R.id.zero).setOnClickListener(this);
findViewById(R.id.div).setOnClickListener(this);
findViewById(R.id.mult).setOnClickListener(this);
findViewById(R.id.plus).setOnClickListener(this);
findViewById(R.id.minus).setOnClickListener(this);
findViewById(R.id.dec).setOnClickListener(this);
findViewById(R.id.equal).setOnClickListener(this);
}
#Override
public void onClick(View v) {
String buttonPressed = ((Button )v).getText().toString();
mCalculatorDisplay.setText(buttonPressed);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class CalculatorFragment extends Fragment {
public CalculatorFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Activity_Main
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />
Fragment_Main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:textSize="65dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/AC"
android:id="#+id/AC"
android:layout_weight="0.25" />
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/plusminus"
android:id="#+id/plusminus"
android:layout_weight="0.25" />
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/percent"
android:id="#+id/percent"
android:layout_weight="0.25" />
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/div"
android:id="#+id/div"
android:layout_weight="0.25" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/nine"
android:id="#+id/nine"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/eight"
android:id="#+id/eight"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/seven"
android:id="#+id/seven"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:id="#+id/mult"
android:text="#string/mult"
android:layout_weight="0.25"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/six"
android:id="#+id/six"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/five"
android:id="#+id/five"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/four"
android:id="#+id/four"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/minus"
android:id="#+id/minus"
android:layout_weight="0.25"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/three"
android:id="#+id/three"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/two"
android:id="#+id/two"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/one"
android:id="#+id/one"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/plus"
android:id="#+id/plus"
android:layout_weight="0.25"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="#string/zero"
android:id="#+id/zero" />
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/dec"
android:id="#+id/dec"
android:layout_weight="0.25"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="#string/equal"
android:id="#+id/equal"
android:layout_weight="0.25"/>
</LinearLayout>
The reason for the Null Pointer Exception is that the differents views (Buttons and TextView) you are trying to access are not in your activity's layout file, but in your Fragment's layout file.
setContentView adds all the views in the layout file to the activity.
To fix the NPE, you need to instantiate the views (Buttons and TextView) in your fragment's onCreateView, because the fragment's layout file contains all the views you are going to use.
public class MainActivity extends ActionBarActivity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new CalculatorFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class CalculatorFragment extends Fragment implements OnClickListener {
private TextView mCalculatorDisplay;
public CalculatorFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mCalculatorDisplay = (TextView) rootView.findViewById(R.id.textView);
rootView.findViewById(R.id.AC).setOnClickListener(this);
rootView.findViewById(R.id.plusminus).setOnClickListener(this);
rootView.findViewById(R.id.percent).setOnClickListener(this);
rootView.findViewById(R.id.nine).setOnClickListener(this);
rootView.findViewById(R.id.eight).setOnClickListener(this);
rootView.findViewById(R.id.seven).setOnClickListener(this);
rootView.findViewById(R.id.six).setOnClickListener(this);
rootView.findViewById(R.id.five).setOnClickListener(this);
rootView.findViewById(R.id.four).setOnClickListener(this);
rootView.findViewById(R.id.three).setOnClickListener(this);
rootView.findViewById(R.id.two).setOnClickListener(this);
rootView.findViewById(R.id.one).setOnClickListener(this);
rootView.findViewById(R.id.zero).setOnClickListener(this);
rootView.findViewById(R.id.div).setOnClickListener(this);
rootView.findViewById(R.id.mult).setOnClickListener(this);
rootView.findViewById(R.id.plus).setOnClickListener(this);
rootView.findViewById(R.id.minus).setOnClickListener(this);
rootView.findViewById(R.id.dec).setOnClickListener(this);
rootView.findViewById(R.id.equal).setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
String buttonPressed = ((Button )v).getText().toString();
mCalculatorDisplay.setText(buttonPressed);
}
}
}
I hope this helps.
You should override Fragment's onViewCreated() because it is guaranteed that all Views are instantiated.
Create a field in your CalculatorFragment to store the OnClickListener to be assigned to your buttons, you can do it like the following:
public static class CalculatorFragment extends Fragment {
OnClickListener mListener = null;
public CalculatorFragment(OnClickListener listener) {
mListener = listener;
}
//et cetera
In the Activity's onCreate() you must specify the OnClickListener when creating the CalculatorFragment:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new CalculatorFragment(this))
.commit();
}
In the overriden onViewCreated(), use getView() to get the View returned by onCreateView() so most calls become like
getView().findViewById(R.id.the_id).setOnClickListener(this);
.
See findViewById in fragment android to see how you can use findViewById

Categories