ANDROID - How to move to other fragment from fragment - android

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

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>

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.

Why fragment layout cannot be displayed?

What have I missed out here? I wanted to open edit_information fragment from edit activity. It can display the Toast but not displaying the edit_information layout.
Edit
listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding listview_item_row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
String ID =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));
EditInformation fragment2 = new EditInformation();
Bundle bundle = new Bundle();
bundle.putString("ID", ID);
fragment2.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
EditInformation
public class EditInformation extends Fragment {
String ID;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_info = inflater.inflate(R.layout.edit_information, container, false);
Bundle bundle = this.getArguments();
if(getArguments()!=null)
{
ID=bundle.getString("ID");
}
Toast.makeText(getActivity(),"Edit_Information",Toast.LENGTH_LONG).show();
return edit_info;
}
}
edit
<?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:id="#+id/fragment1">
<RelativeLayout
android:layout_width="209dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/tomato"
android:paddingLeft="10sp"
android:textSize="20sp"
android:paddingTop="10sp"
android:hint="Long Press to delete"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/tomato"
android:paddingTop="40sp"
android:textSize="20sp"
android:paddingLeft="10sp"
android:hint="Hint to edit"/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listViewEdit"
android:paddingTop="10sp"/>
</LinearLayout>
edit_information
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10sp"
android:orientation="vertical"
android:background="#color/beige">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Name"
android:textColor="#color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Name"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="#+id/SpinnerName" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:id="#+id/Weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Weather"
android:textColor="#color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Weather"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="342dp"
android:layout_height="52dp"
android:id="#+id/SpinnerWeather">
</Spinner>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Date"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Date"
android:textColor="#color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Date"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<EditText
android:layout_width="311dp"
android:layout_height="52dp"
android:layout_margin="10sp"
android:background="#drawable/round_corner_square"
android:id="#+id/date"
android:paddingLeft="20dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Status"
android:background="#drawable/round_corner_up"
android:padding="10dp"
android:text="Status"
android:textColor="#color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Status"
android:layout_marginTop="-15dp"
android:background="#drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="#+id/SpinnerStatus">
</Spinner>
</LinearLayout>
</RelativeLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:orientation="horizontal">
<Button
android:layout_width="187dp"
android:layout_weight="0.83"
android:text="Add Work Force"
android:textSize="20sp"
android:drawableRight="#mipmap/add_workforce"
android:layout_height="75dp"
android:id="#+id/AddForce"
android:layout_x="12dp"
android:layout_y="11dp" />
<Button
android:layout_width="123dp"
android:layout_weight="0.83"
android:text="Next"
android:textSize="20sp"
android:drawableRight="#mipmap/next"
android:layout_height="71dp"
android:id="#+id/Next"
android:layout_x="211dp"
android:layout_y="14dp" />
</AbsoluteLayout>
</LinearLayout>
Try using FrameLayout as a container for fragment instead of LinearLayout. I believe because of LinearLayout your fragment is getting created out of screen. where as FrameLayout will overlap your fragment on other Views.

onbuttonclick display a hidden fragment

When I click on login button, it says that app is stopped working. But It should be changed to fragment register. Can anyone tell what I should change in following java file ? because I am new for Android, Please kindly help.
**AdminFragment.java**
public class AdminFragment extends Fragment implementsView.OnClickListener {
EditText etUsername,etPassword;
Button bLogin;
public View mRootView;
public AdminFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_admin, container, false);
etUsername=(EditText)mRootView.findViewById(R.id.etUsername);
etPassword=(EditText)mRootView.findViewById(R.id.etPassword);
bLogin=(Button)mRootView.findViewById(R.id.bLogin);
bLogin.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
Fragment fragment=null;
switch (v.getId()){
case R.id.bLogin:
fragment = new RegisterFragment();
replaceFragment(fragment);
break;
}
}
private void replaceFragment(Fragment AdminFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab,AdminFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
enter code here
my Admin Xml file
My xml file in admin fragment
<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:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.AdminFragment">
<TextView
android:id="#+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bLogin"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
My Register java file
public class RegisterFragment extends Fragment implementsView.OnClickListener {
Button bRegister;
EditText etHospital;
public View mRootView;
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView= inflater.inflate(R.layout.fragment_register, container, false);
bRegister=(Button)mRootView.findViewById(R.id.bRegister);
etHospital=(EditText)mRootView.findViewById(R.id.etHospital);
bRegister.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRegister:
break;
}
}
}
My register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

Using Sliding tabs in a fragment

I'm working on an app where I am supposed to fragment my MainActivity into 2 fragments. One on the right whitch going to hold some textEdits and buttons, and one on the left that should hold some sliding tabs.
I already did the fragmentation part, and added tabs on the left fragment, but the problem is I don't know how to make the tabs slide. Please I really need your help. Thanks in advance.
This is what I wrote so far:
MainActivity=>AppActivity.java:
package com.example.g514110.morphokitihm1;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AppActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_container_1, new MainTab())
.commit();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_container_2, new StaticFragment())
.commit();
}
}
public static class MainTab extends Fragment {
private FragmentTabHost mTabHost;
//Mandatory Constructor
public MainTab() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main_tab,container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmenta").setIndicator("Biometric operations"),
Tab1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("DataBase operations"),
Tab2.class, null);
return rootView;
}
}
public static class Tab1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
return rootView;
}
}
public static class Tab2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2, container, false);
return rootView;
}
}
public static class StaticFragment extends Fragment {
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.activity_static_frag, container, false);
return rootView;
}
}
}
activity_app.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
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">
<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
<FrameLayout
android:id="#+id/main_container_1"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="450dp"/>
<FrameLayout
android:id="#+id/main_container_2"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="200dp"/>
</LinearLayout>
</LinearLayout>
activity_static_frag.xml:
<?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">
<!--Acquisition pannel-->
<RelativeLayout
android:layout_width="170dp"
android:layout_height="fill_parent"
android:background="#drawable/textview_border"
android:orientation="vertical"
android:id="#+id/relativeLayout3"
android:layout_marginLeft="68dp"
android:layout_marginStart="71dp"
android:layout_below="#+id/APLabel">
<VideoView
android:layout_width="150dp"
android:layout_height="160dp"
android:id="#+id/videoView"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:text="#string/Sacqu"
android:id="#+id/stop"
android:background="#drawable/shutdown_design"
android:src="#drawable/shutdown1"
android:layout_column="4"
android:layout_marginTop="170dp"
android:layout_marginLeft="130dp" />
<TextView
android:layout_width="150dp"
android:layout_height="27dp"
android:text=" "
android:id="#+id/msg"
android:background="#DFDFDF"
android:layout_below="#+id/videoView"
android:layout_alignRight="#+id/videoView"
android:layout_alignEnd="#+id/videoView"
android:layout_marginTop="35dp"/>
<TextView
android:layout_width="150dp"
android:layout_height="27dp"
android:text=" "
android:background="#DFDFDF"
android:id="#+id/qty"
android:layout_below="#+id/msg"
android:layout_alignLeft="#+id/msg"
android:layout_alignStart="#+id/msg"
android:layout_marginTop="10dp"/>
<ProgressBar
android:id="#+id/qtyProgressbar"
android:layout_width="150dp"
android:layout_height="20dp"
android:background="#DFDFDF"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
android:layout_below="#+id/qty"
android:layout_alignLeft="#+id/qty"
android:layout_alignStart="#+id/qty"
android:layout_marginTop="20dp"/>
</RelativeLayout>
<TextView
android:id="#+id/APLabel"
android:layout_width="106dp"
android:layout_height="15dp"
android:background="#drawable/textview_border"
android:text="#string/AP"
android:textSize="11px"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginLeft="100dp"
android:layout_marginTop="10dp"/>
</RelativeLayout>
activity_main_tab.xml:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/item_detail_container">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/realtabcontent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
tab1.xml:
<?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:id="#+id/item_detail_container">
<!--Enrollement settings-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/textview_border"
android:orientation="vertical"
android:id="#+id/linearLayout4"
android:layout_marginTop="80dp"
android:layout_marginLeft="0dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="#string/consolidation"
android:id="#+id/cons_ch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="#string/SI"
android:id="#+id/save_img_ch"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="#string/SDB"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/FFOT"
android:id="#+id/frc_ch"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ST"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:id="#+id/save_temp_ch"/>
<Button
android:layout_width="110dp"
android:layout_height="35dp"
android:text="#string/enroll"
android:id="#+id/enroll"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/designed_buttons"/>
</LinearLayout>
<TextView
android:id="#+id/APLabel"
android:layout_width="106dp"
android:layout_height="15dp"
android:background="#drawable/textview_border"
android:text="#string/ES"
android:textSize="11px"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="65dp" />
<RelativeLayout
android:layout_width="300dp"
android:layout_height="110dp"
android:background="#drawable/textview_border"
android:orientation="horizontal"
android:id="#+id/linearLayout6"
android:layout_marginLeft="54dp"
android:layout_marginStart="54dp"
android:layout_alignTop="#+id/linearLayout4"
android:layout_toRightOf="#+id/linearLayout4"
android:layout_toEndOf="#+id/linearLayout4">
<Button
android:layout_width="110dp"
android:layout_height="35dp"
android:text="#string/verif"
android:id="#+id/verif"
android:onClick="verify"
android:layout_marginTop="10dp"
android:layout_marginLeft="180dp"
android:background="#drawable/designed_buttons"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="110dp"
android:layout_height="35dp"
android:text="#string/identify"
android:id="#+id/identify"
android:onClick="identify"
android:layout_marginTop="5dp"
android:background="#drawable/designed_buttons"
android:layout_gravity="center_horizontal"
android:layout_below="#+id/verif"
android:layout_alignLeft="#+id/verif"
android:layout_alignStart="#+id/verif" />
<ImageView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/matchingIcon"
android:background="#drawable/matching"
android:layout_marginLeft="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<TextView
android:id="#+id/MOASLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/textview_border"
android:text="#string/MOAS"
android:textSize="11px"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_above="#+id/linearLayout6"
android:layout_alignLeft="#+id/linearLayout6"
android:layout_alignStart="#+id/linearLayout6" />
</RelativeLayout>
tab2.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:id="#+id/item_detail_container">
<!--Database operations-->
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/storageIcon"
android:background="#drawable/stor1"
android:layout_marginTop="30dp"
android:layout_marginLeft="5dp"
android:layout_alignBottom="#+id/record"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/textview_border"
android:id="#+id/linearLayout5">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CreatMatch"
android:background="#drawable/designed_buttons"
android:id="#+id/matcher"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/NewRecord"
android:id="#+id/record"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="#string/RecordList"
android:id="#+id/record_list"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="#string/dbsize"
android:id="#+id/dbSize"
android:textColor="#000"
android:layout_span="2"
android:layout_above="#+id/db_size"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="#+id/db_size"
android:background="#DFDFDF"
android:layout_span="6"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>

Categories

Resources