I made a progress-bar fragment; the goal is to show it whenever the buttons in my MainActivity are clicked. However, I encountered this dilemma (using Android 5.1):
https://gyazo.com/8632f6e50cdce599847258939cc4f109
Any ideas what's the deal here? I can't pin-point the problem. The only solution is to hide the button when clicked. It works but I interpret is as badly-written code.
Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
final Button bRegister = (Button) findViewById(R.id.bRegister);
final Button bTest = (Button) findViewById(R.id.bTest);
bTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.activity_register, progressFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
ProressFragment
public class ProgressFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_progress, container, false);
ProgressBar pb = (ProgressBar) view.findViewById(R.id.progressBar);
pb.setVisibility(View.VISIBLE);
return view;
}
}
XML for MainActivity (Looks like this:https://gyazo.com/8ecef7ada74cf92730b4d99757383da7)
<?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:id="#+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="googleplayservices.samples.android.teamtreehouse.com.kibbleuserc.RegisterActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Password"
android:ems="10"
android:id="#+id/etPassword"
android:layout_below="#+id/etEmail"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/etEmail"
android:layout_alignEnd="#+id/etEmail"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Phone Number"
android:ems="10"
android:id="#+id/etPhoneNum"
android:layout_below="#+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/etPassword"
android:layout_alignEnd="#+id/etPassword"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/etName"
android:layout_below="#+id/etPhoneNum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/etPhoneNum"
android:layout_alignEnd="#+id/etPhoneNum"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Email"
android:ems="10"
android:layout_marginTop="10dp"
android:id="#+id/etEmail"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/etEmail"
android:id="#+id/tvEmailError"
android:layout_above="#+id/etPassword"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/etEmail"
android:textColor="#FFFF4444"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPasswordError"
android:layout_alignBottom="#+id/etPassword"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/etPassword"
android:layout_toEndOf="#+id/etPassword"
android:layout_below="#+id/etEmail"
android:textColor="#FFFF4444"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/etPhoneNum"
android:layout_toRightOf="#+id/etPhoneNum"
android:id="#+id/tvPhoneNumError"
android:layout_below="#+id/etPassword"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#FFFF4444"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/etName"
android:layout_toRightOf="#+id/etName"
android:id="#+id/tvNameError"
android:layout_below="#+id/etPhoneNum"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#FFFF4444"/>
<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bRegister"
android:layout_below="#+id/etName"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bRegister"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="31dp"
android:id="#+id/bTest"/>
</RelativeLayout>
XML for ProgressFragment (Looks like this:https://gyazo.com/93bbb11af8cb8a0df8d4d6cf97b84ef1)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
It happens because the two buttons have an elevation that is not 0 while your RelativeLayout in your Fragment have an elevation of 0.
You can fix it wrapping the RelativeLayout of the Activity in a FrameLayout, like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="googleplayservices.samples.android.teamtreehouse.com.kibbleuserc.RegisterActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Add all the views that you have inserted in your RelativeLayout -->
</RelativeLayout>
</FrameLayout>
However the optimized solution is to set the elevation in your Fragment's root View with ViewCompat.setElevation(View,int) but you have to know the right elevation to set to bring the View on top of the others. In this case you won't need the FrameLayout wrapper and the onDraw() and onLayout() calls will be optimized.
Related
I'm trying to make a fragment appear next to my activity view when I click on a button, but I keep having this error and I can't solve this.
java.lang.IllegalArgumentException: No view found for id 0x7f090081 (nofix.oc:id/register_form_container) for fragment FragmentRegisterClient{6a297ba #0 id=0x7f090081}
I took a screenshot to explain you what I want to happen : http://prntscr.com/i6dx9f
Here is the code of the Activity I'm calling my Fragment from :
public class RegisterActivity extends AppCompatActivity{
[...]
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tmpSavedInstanceState = savedInstanceState;
setContentView(R.layout.activity_register);
[...]
FrameLayout frame = new FrameLayout(this);
frame.setId(CONTENT_VIEW_ID);
setContentView(frame, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));
if (tmpSavedInstanceState == null) {
Fragment newFragment = new FragmentRegisterClient();
Log.e("error","eezrzezeerez");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.register_form_container, newFragment).commit();
}
Here is the layout f RegisterActivity:
<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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="nofix.Links.RegisterActivity">
<!-- Login progress -->
<ProgressBar
android:id="#+id/register_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/register_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/email_register_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:layout_marginBottom="15dp">
<TextView
android:id="#+id/textView"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_weight="0.80"
android:text="#string/account_type"
android:textSize="20sp"
app:layout_constraintRight_toLeftOf="#+id/dropdown_account_type"
tools:layout_editor_absoluteY="8dp" />
<Spinner
android:id="#+id/dropdown_account_type"
android:layout_width="179dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="157dp"
android:layout_weight="0.67" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/name_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_name"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_register"
android:imeOptions="actionUnspecified"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/first_name_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_first_name"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_register"
android:imeOptions="actionUnspecified"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_register"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/email_register_form_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/action_register"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/register_form_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
And here is code and XML of my fragment :
public class FragmentRegisterClient extends Fragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("First test", "Please tell me it's ok");
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_register_client, container, false);
Log.e("Second Test", "NICE");
return view;
}
}
Here is the XML
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context="nofix.Links.FragmentRegisterClient">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bite"/>
</FrameLayout>
EDIT:
I had to remove the second setContentView() to make it work.
I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).
I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.
My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.
The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<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:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">
<android.support.design.widget.TextInputEditText
android:id="#+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/teamName"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="#android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="#android:color/background_light"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="#android:color/background_light" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the AddTeamMembers Class that calls the above XML
public class AddTeamMembers extends Fragment implements
View.OnClickListener
{
private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;
//The below method must be overridden in order to implement a fragment -
this changes the lifecycle method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_team_members,
container, false);
initViews(view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 0, Sets Shared pref mode to private
pref = getActivity().getPreferences(0);
tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));
}
private void initViews(View view) {
tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);
mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);
tv_add_name.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_name:
createEditTextView();
break;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void createEditTextView() {
try{
//dynamically create new EditText when user clicks to add another
name
//target user using API 22 and above (lollipop and above)
EditText editTextView = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.M) {
editTextView = new EditText(getContext());
}else{
Toast.makeText(getActivity(), "App is not supported on this
device",
Toast.LENGTH_LONG).show();
}
editTextView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
mLayout.addView(editTextView);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
}
[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]
[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png
i made a 2 fragment layout one for the login screen and the other for the Register screen and in the main screen i put a fragment for login screen and a button if the user want to register he click in that button and the register fragment appears to him and the login fragment disappear
the problem is after i press the register button in the main screen the login screen appears to be behind the register screen
here's a photo for it
login behind Register
i don't know what's the problem or i misunderstanding the fragments i don't know
here's the onclick method on the MainActity class
#Override
public void onClick(View view) {
String mytxt = inversebutton.getText().toString();
Fragment fragmentSign = new Signin();
Fragment fragmentregister = new Register();
if(mytxt.equals("Register")){ // call signin frame
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentplace ,fragmentregister).commit();
inversebutton.setText("SignIn");
}else{ // call register fra
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentplace ,fragmentSign).commit();
inversebutton.setText("Register");
}
}
});
here's the activitymain.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.abdelmagied.myapplication.MainActivity"
android:background="#222">
<fragment
android:layout_width="match_parent"
android:layout_height="300dp"
android:name="com.example.abdelmagied.myapplication.Signin"
android:id="#+id/fragmentplace"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/reverse"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="16dp" />
</RelativeLayout>
here's the register_fragment class
public class Register extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
here's the signin fragment class
public class Register extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
here's the fragment_register.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="com.example.abdelmagied.myapplication.Register"
android:background="#090"
android:id="#+id/registerfragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="49dp"
android:id="#+id/textView" />
<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="61dp"
android:id="#+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/Rbutton"
android:layout_alignStart="#+id/Rbutton"
android:layout_marginTop="30dp"
android:id="#+id/registerN" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="#+id/registerN"
android:layout_alignRight="#+id/registerN"
android:layout_alignEnd="#+id/registerN"
android:layout_marginTop="36dp"
android:id="#+id/Rpassword" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/RRpassword"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/Rbutton"
android:layout_alignStart="#+id/Rbutton" />
<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Rbutton"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<TextView
android:text="Rpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
</RelativeLayout>
here's the fragment_signin.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="com.example.abdelmagied.myapplication.Signin"
android:background="#878"
android:id="#+id/signinfragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"
android:id="#+id/sn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp" />
<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sn"
android:layout_alignLeft="#+id/sn"
android:layout_alignStart="#+id/sn"
android:layout_marginTop="79dp"
android:id="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_above="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/sname" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/sname"
android:layout_alignStart="#+id/sname"
android:id="#+id/signpassword" />
<Button
android:text="SignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Sbuton"
android:layout_below="#+id/signpassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp" />
</RelativeLayout>
Rather than taking fragment tag in xml,it would be good for taking just a layout by giving its id and replace it by fragment transaction dynamically
In your layout, change it by taking simple Relative or Linear layout and must give that layout background,remove that fragment tag and replace fragment by java coding according to you need
and then replace it at start of your class in onCreate() method by->`
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentplace, new Your_frag(), Constant.FragmentTags.fragmentTag).commit();
I think you should use DialogFragment which are here for those kind of popups in front of others.
You use it like that
DialogFragment registerFragment = RegisterFragment.newInstance(0);
registerFragment.show(getFragmentManager().beginTransaction(), "RegisterPopup");
And your fragment will be of class DialogFragment. Usually you pop a Dialog starting like that. Then depending on what you want many options are possible.
public class RegisterFragment extends DialogFragment {
...
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
}
And you can find the example on the Android Developer
DialogFragment
I'm trying to use fragments to programatically change the text on a screen. To do this I'm setting up an on click listener on a text view and then if it's clicked starting a fragment manager, replacing the current fragment with the new fragment. However, this causes my app to crash when it's started.
From reading the crash report it seems like the error is happening at tv1.setOnClickLIstener...
Finally, Android Studio keeps giving me a type mismatch when I use fragment or support fragment. That is why you see android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Java Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//begin transaction
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//replace the contents of the container with the new fragment
ft.replace(R.id.placeHolder, new SplashScreenFragment());
ft.commit();
TextView tv1 = (TextView) findViewById(R.id.whatIsHumanTrafficing);
tv1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.placeHolder, new whatIsHumanTrafficing());
ft.commit();
}
});
}
}
XML from activity_main:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/placeHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
XML displayed before click:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.piatt.worksafe.MainActivity"
android:id="#+id/splashScreenId"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Safe!"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Human Trafficing?"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/whatIsHumanTrafficing"
android:clickable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I get safe labor?"
android:layout_below="#+id/whatIsHumanTrafficing"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/howDoIGetSafeLabor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I check that my job / job offer is legal?"
android:layout_below="#+id/howDoIGetSafeLabor"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"
android:id="#+id/checkLegality"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How can I get help?"
android:layout_below="#+id/checkLegality"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/getHelp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:layout_below="#+id/getHelp"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/aboutUs"
/>
</RelativeLayout>
XML to be displayed after click on text view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Human Trafficing Is:"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confiscation of travel documents"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HTdescription1"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unregistered Labor"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription2"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Isolation from friends and family"
android:layout_below="#+id/HTdescription2"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription3"
android:layout_gravity="center"
/>
</LinearLayout>
Fragment named human trafficking that I'm trying to inflate:
public class whatIsHumanTrafficing extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
//inflate the layout for this fragment
return inflater.inflate(R.layout.what_is_human_trafficing, container, false);
}
}
You have used activity_main in your MainActivity as follows
setContentView(R.layout.activity_main);
and you are accessing whatIsHumanTrafficingnamed TextView in the same MainActivity which is not possible as it is not present in activity_main and which is present in another xml. So you are getting NullPointerException there.
Usually, if we want to go to other page. We use code
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity(i);
finish();
I confuse to go to other page with fragment.
TEACHER_Class.java
public class TEACHER_Class extends Fragment {
Button tambahKelasButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.teacher_class, container, false);
tambahKelasButton = (Button) rootView.findViewById(R.id.tambah_kelas_button);
tambahKelasButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment FRAGMENT = new TEACHER_AddClass();
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//I think I get error in here
fragmentTransaction.replace(R.layout.teacher_class, FRAGMENT);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return rootView;
}
}
TEACHER_AddClass.java
public class TEACHER_AddClass extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.teacher_add_class, container, false);
return rootView;
}
}
I want to move from TEACHER_Class.java to TEACHER_AddClass.java
This is teacher_class.xml
<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:gravity="center_horizontal"
android:orientation="vertical" android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin" android:background="#393f46"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/teacher_class_layout"
>
<!-- Login progress -->
<Button android:id="#+id/tambah_kelas_button"
android:layout_width="150dp" android:layout_height="40dp"
android:layout_marginTop="20dp" android:layout_marginLeft="100dp"
android:text="TAMBAH KELAS" android:background="#f8255f"
android:textSize="18sp" android:textColor="#FFFFFF"
android:textStyle="bold" />
<ScrollView android:id="#+id/insert_class_form" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="30dp">
<TableLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#999999"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="3">
<TextView
android:id="#+id/header_class_name"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text=" NAMA KELAS"
android:layout_marginLeft="0dp"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
<TextView
android:id="#+id/header_bank_soal"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="BANK SOAL"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
<TextView
android:id="#+id/header_resources"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="RES"
android:layout_marginLeft="30dp"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
</ScrollView>
teacher_add_class.xml
<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:orientation="vertical" android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin" android:background="#393f46"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/teacher_add_class_layout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD CLASS"
android:textSize="28sp"
android:textColor="#FFFFFF"
android:id="#+id/loginTittle"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:layout_marginTop="60dp"
/>
<EditText
android:id="#+id/fullname_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Full Name"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/shortname_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Short Name"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="#+id/summary_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Summary"
android:layout_marginBottom="30dp"
/>
<Button
android:id="#+id/add_class_button"
android:layout_width="100dp" android:layout_height="60dp"
android:layout_marginTop="0dp" android:layout_marginLeft="150dp"
android:text="ADD" android:background="#f8255f"
android:textSize="22sp" android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
FragmentManager fragmentManager = getActivity().getFragmentManager();
you should use getChildFragmentManager, to perform transaction from a Fragment
fragmentTransaction.replace(R.layout.teacher_class, FRAGMENT);
the first parameter has to be the id (R.id.) of the ViewGroup that hosts the Fragment itself, and not the id of layout.
I suspect R.id.layout.teacher_class is the fragment you want removed? The first argument for the replace function is the container holding the fragment, not the actual fragment to be replaced.
fragmentTransaction.replace([TARGET_CONTAINER], FRAGMENT);