Button Click Open The Fragment
java Activity code
package com.example.reBLISS.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.example.reBLISS.R;
import com.example.reBLISS.fragment.FragmentCheckIn;
import com.example.reBLISS.fragment.FragmentCheckOut;
public class ActivitySelfies2 extends AppCompatActivity {
private Button btncheckin,btncheckOut;
private ImageView backbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selfies2);
btncheckin=findViewById(R.id.checkin);
btncheckOut=findViewById(R.id.checkout);
backbtn=findViewById(R.id.back_btn);
FragmentManager fm = getSupportFragmentManager();
FragmentCheckIn fragment = new FragmentCheckIn();
fm.beginTransaction().add(R.id.fragmentcontainer,fragment).commit();
btncheckin.setEnabled(false);
btncheckOut.setEnabled(true);
btncheckin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
FragmentCheckIn fragment = new FragmentCheckIn();
fm.beginTransaction().add(R.id.fragmentcontainer,fragment).commit();
btncheckin.setEnabled(false);
btncheckOut.setEnabled(true);
}
});
btncheckOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
FragmentCheckOut fragment = new FragmentCheckOut();
fm.beginTransaction().add(R.id.fragmentcontainer,fragment).commit();
btncheckOut.setEnabled(false);
btncheckin.setEnabled(true);
}
});
backbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
Xml Activity Code
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".view.ActivitySelfies2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/line"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
app:titleTextColor="@color/white"
android:background="@drawable/toolbar_gradiant">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/back_btn"
android:src="@drawable/arrow_back_24"
>
</ImageView>
<TextView
android:layout_width="match_parent"
android:text="Selfies"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginEnd="33dp"
android:textSize="21dp"
android:textColor="@color/white"
android:textStyle="bold"
android:layout_height="wrap_content">
</TextView>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="2"
android:background="@color/white"
android:layout_height="60dp">
<Button
android:layout_width="wrap_content"
android:id="@+id/checkin"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/appcolor"
android:text="Check In"
android:layout_weight="1"
android:layout_height="wrap_content">
</Button>
<Button
android:layout_width="wrap_content"
android:text="Check Out"
android:layout_weight="1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/appcolor"
android:id="@+id/checkout"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/fragmentcontainer"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
Fragment java Code
package com.example.reBLISS.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.reBLISS.R;
import com.example.reBLISS.adepters.SelfiesAdepaterIn;
import com.example.reBLISS.helper.MySingleton;
import com.example.reBLISS.modal.SelfieResponse;
import com.example.reBLISS.retrofit.ApiClient;
import com.example.reBLISS.retrofit.ApiInterface;
import com.kaopiz.kprogresshud.KProgressHUD;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* A simple {@link Fragment} subclass.
* Use the {@link FragmentCheckIn#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentCheckIn extends Fragment {
public MySingleton mySingleton;
public RecyclerView recyclerViewIn;
public SelfieResponse selfieResponse;
private KProgressHUD kProgressHUD;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public FragmentCheckIn() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentCheckIn.
*/
// TODO: Rename and change types and number of parameters
public static FragmentCheckIn newInstance(String param1, String param2) {
FragmentCheckIn fragment = new FragmentCheckIn();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
selfieResponse =new SelfieResponse();
mySingleton=new MySingleton(getContext());
View view=inflater.inflate(R.layout.fragment_check_in, container, false);
recyclerViewIn =view.findViewById(R.id.recycheckoutIN);
try {
kProgressHUD = KProgressHUD.create(getContext())
.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
.setCancellable(false)
.setAnimationSpeed(2)
.setDimAmount(0.5f)
.show();
} catch (Exception e) {
e.printStackTrace();
}
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("uid",mySingleton.getData("userUid"));
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<SelfieResponse> call=apiInterface.getSelfies(hashMap);
call.enqueue(new Callback<SelfieResponse>() {
@Override
public void onResponse(Call<SelfieResponse> call, Response<SelfieResponse> response) {
selfieResponse=response.body();
// Toast.makeText(getApplicationContext(),""+response.body().getData().get(0).getCheckinlocation(),Toast.LENGTH_LONG).show();
LinearLayoutManager linearLayoutManager= new LinearLayoutManager(getContext());
SelfiesAdepaterIn selfiesAdepater =new SelfiesAdepaterIn(selfieResponse);
recyclerViewIn.setLayoutManager(linearLayoutManager);
recyclerViewIn.setAdapter(selfiesAdepater);
if (kProgressHUD.isShowing())
{
kProgressHUD.dismiss();
}
}
@Override
public void onFailure(Call<SelfieResponse> call, Throwable t) {
Log.d("yes","ok"+t);
if (kProgressHUD.isShowing())
{
kProgressHUD.dismiss();
}
}
});
return view;
}
}
Fragment Xml Code
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".fragment.FragmentCheckIn">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycheckoutIN"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Comments
Post a Comment