I have a mainActivity.java
class with a layout called activity_main.xml
and a .xml file named drawer_menu.xml
which has a textView
that's displayed in the app's drawer menu
.I want to set the drawer menu's textView
text to a String
value that i have in my mainActivity.java
class. How can i access that textView inside the mainActivity class?
activity_main.java (the part accessing the textView):
/**
* Prompts the user for his/her username
* when the tutorial is done
*/
private void promptForUsername() {
UsernameDialog dialog = new UsernameDialog();
dialog.setCancelable(false);
dialog.show(getFragmentManager(),"USERNAME_DIALOG");
username.setText(getUsername());
}
public void setUsername(String name) {
tempName = name;
}
public String getUsername() {
return tempName;
}
UsernameDialog.java class:
public class UsernameDialog extends DialogFragment {
@BindView(R.id.editText) EditText mEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// inflate the layout using the dialog themed context
final Context context = getActivity();
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(R.layout.username_dialog,null,false);
final MainActivity activity = new MainActivity();
ButterKnife.bind(this,view);
DialogInterface.OnClickListener posListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
activity.setUsername(mEditText.getText().toString());
Log.d("USERNAME",mEditText.getText().toString());
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Choose your username")
.setView(view)
.setPositiveButton("OK",posListener);
return builder.create();
}
}
drawer_menu.xml (contains the TextView to be accessed):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/NavigationDrawerHeader">
<ImageView
android:id="@+id/menu_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:contentDescription="@string/logo"
app:srcCompat="@drawable/user_icon" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="66dp"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/menu_logo"
android:layout_marginStart="93dp"
android:layout_marginTop="5dp"
android:fontFamily="@font/alegreya_sans_extrabold"
android:paddingTop="25dp"
android:text="username"
android:textColor="#ffffff"
android:textSize="18sp" />
</RelativeLayout>