In my fragment, if a certain text field is not filled out a error message is displayed to the user in the form of a Dialog, which is displaying as itended. However, I require the height of the Dialog box to change programatically depending on the lenth of the error message.
In the onCreateView() method of my fragment, I have successfully been able to print the value of one of the TextView elements from the Dialog's xml file.
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
View t = deetsView.findViewById(R.id.dialogText);
String val = ((TextView)t).getText().toString();
System.out.println(val);
val successfully prints as "TeStInG", which is the correct value. The xml file is as follows, the confirms the android:text="TeStInG".
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signUpFillDeets"
android:orientation="vertical"
android:layout_width="210dp"
android:layout_height="230dp"
android:background="#ffffffff">
<ImageView
android:id="@+id/crossImage"
android:layout_width="match_parent"
android:src="@drawable/dialog_cross"
android:layout_height="120dp"
android:gravity="center"
android:background="#1F1C1C"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/dialogText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/crossImage"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:text="TeStInG"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:id="@+id/dialogButton"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="OK"
android:textStyle="italic"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/dialogText"
android:layout_marginBottom="20dp"
android:background="@drawable/button_background_red"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
However when I try to access layout file via its ID (signUpFillDeets), no change occurs.
THE CODE BELOW IS ALL DONE IN THE onCreateView() AND THE CODE WHICH ACTUALLY DISPLAYS THE DIALOG IS WITHIN THE setUserVisibleHint() METHOD. So logically, if I am setting the height of the error message at this point, it should have a permenant effect on the dimensions of the dialog until the application is closed.
Attempt 1:
deetsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000));
Attempt 2:
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
Results in error - java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on a null object reference
Attempt 3:
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams dimensions = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000);
layout.setLayoutParams(dimensions);
Can anyone assist on this?
Update - onCreateView() and setUserVisibleHint()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.sign_up_fragment_third, container, false);
// we need to apply our custom font to the TextViews on our fragment. So first need to reference the TextView components via our sign_up_fragment_third view, and apply the TypeFact
View title = view.findViewById(R.id.signUpTertTitle);
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
View t = deetsView.findViewById(R.id.dialogText);
String val = ((TextView)t).getText().toString();
System.out.println(val);
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
/*
Attempt 1
deetsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000));
Attempt 2
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
*/
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// if the user has not supplied a first name then we will display to them an prompt to enter a first name.
String firstName = ((EditText)SignUpFragmentSecond.firstName).getText().toString();
System.out.println("First name val is " + firstName);
String errorMessage = "Need to fill out ";
if(firstName.equals("")){
System.out.println("First name is empty");
errorMessage += " First Name, ";
System.out.println("Now is " + errorMessage);
}
if(!errorMessage.equals("Need to fill out ")){
SignUpActivity.pager.setCurrentItem(1);
SignUpFillDetails dialog = new SignUpFillDetails();
dialog.showDialog(getActivity(), errorMessage);
}
} else {
System.out.println("THIRD is not visible to the user");
}
}
UPDATED onViewCreated() added and also listed imports incase it comes in use
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(getContext());
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
}