Fragments goes one behind the other - android

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

Related

Activity view shows up on fragment

I have button on my activity that show up on the fragment and only the button that show up , i did search about it and find out its a rendering issue but the answers didn't worked for me, here how it looks.
Fragment
Activity
is there any chance that the issue code be from the margin of the button
Fragment.java
public class Smsar extends Fragment {
public Smsar() {
// Required empty public constructor
}
View root;
Button mSmsar,mFinder;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
root= inflater.inflate(R.layout.fragment_smsar, container, false);
mSmsar=(Button)root.findViewById(R.id.smsar);
mFinder=(Button)root.findViewById(R.id.finder);
mSmsar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
mFinder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Open Discover.
}
});
return root;
}
}
Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="#drawable/main_background_difference"
tools:context=".Smsar">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/smsar"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp"
android:background="#drawable/btn_rounded"
android:fontFamily="#font/cabin"
android:text="Smsar ?"
android:textAlignment="center"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="18sp"
android:typeface="normal" />
<Button
android:id="#+id/finder"
android:layout_width="204dp"
android:layout_height="63dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="138dp"
android:background="#drawable/btn_rounded"
android:fontFamily="#font/cabin"
android:text="Want Apartment ?"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
</RelativeLayout>
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="#drawable/background"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="#+id/mainView"
>
<ImageView
android:id="#+id/logo"
android:layout_width="162dp"
android:layout_height="196dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:adjustViewBounds="true"
android:maxHeight="64dp"
android:maxWidth="64dp"
app:srcCompat="#drawable/logo" />
<Button
android:id="#+id/logIn"
android:layout_width="218dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/userName"
android:layout_alignParentBottom="true"
android:layout_marginEnd="31dp"
android:layout_marginBottom="113dp"
android:background="#color/buttons"
android:clickable="true"
android:fontFamily="#font/catamaran"
android:onClick="logIN"
android:text="Login"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="22sp" />
<EditText
android:id="#+id/userName"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10"
android:fontFamily="#font/catamaran"
android:hint="UserName"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="#+id/password"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_centerHorizontal="true"
android:fontFamily="#font/catamaran"
android:hint="Password"
android:inputType="textPassword" />
<TextView
android:id="#+id/_new"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-35dp"
android:layout_marginBottom="0dp"
android:layout_toStartOf="#+id/sginUp"
android:fontFamily="#font/catamaran"
android:text="New ? Start Now By"
android:textAlignment="center"
android:textSize="18sp" />
<TextView
android:id="#+id/sginUp"
android:layout_width="81dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="17dp"
android:layout_marginBottom="0dp"
android:layout_centerHorizontal="true"
android:layout_toEndOf="#id/mError"
android:clickable="true"
android:fontFamily="#font/catamaran"
android:text="SginUp"
android:textAlignment="center"
android:textColor="#color/links"
android:textSize="18sp" />
<TextView
android:id="#+id/mError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:textColor="#android:color/holo_red_dark"
android:visibility="invisible"
android:text="mError" />
</RelativeLayout>
Activity.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_login =(Button)findViewById(R.id.logIn);
pref = getSharedPreferences("user_details", MODE_PRIVATE);
if(pref.contains("username")&&pref.contains("password"))
success();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.mainView, mFrag);
ft.commit();
imageView=(ImageView)findViewById(R.id.logo);
mDBHelper =new DBHelper(this);
mError=(TextView)findViewById(R.id.mError);
//Define the variables
userNameEdit=(EditText)findViewById(R.id.userName);
passwordEdit=(EditText)findViewById(R.id.password);
_signUp=(TextView)findViewById(R.id.sginUp);
//End
_signUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent=new Intent(MainActivity.this,Signup.class);
startActivity(intent);
}
});
}
I solve it by surround the button with LinearLayout
<LinearLayout
android:id="#+id/buttonContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/password"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<Button
android:id="#+id/logIn"
android:layout_width="186dp"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
android:background="#drawable/btn_rounded"
android:minWidth="200dp"
android:onClick="logIN"
android:text="#string/title_login"
android:textAllCaps="false"
android:textColor="#color/ms_white"
android:textSize="18sp" />
</LinearLayout>

Fragment is not overlapping buttons that are in the activity

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.

OnClickListener and fragments causing application crash

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.

ANDROID - How to move to other fragment from fragment

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);

Dialog using DialogFragment not showing up properly

I am using DialogFragment to display a dialog.But the dialog was displayed with the title.When i used no title my dialog was not being displayed properly.
Dialog with title
Dialog without title
Xml
<?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"
android:background="#drawable/share_background"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
</LinearLayout>
</LinearLayout>
DialogFragment Code
public class ShareDialogFragment extends DialogFragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.share_dialog, null, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Why this is happening????
Try this:
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
Hope this helps.
Fix layout height and width of your parent linear layout to a fixed value.
Example:
android:layout_width="200dp"
android:layout_height="wrap_content"
Try this:
inflater.inflate(R.layout.share_dialog, container, false);

Categories

Resources