-1

I want to made a lock screen Button in a Fragment Layout.
I searched some help and found the same, but made using an Activity.

The code is here: http://karanbalkar.com/2014/01/tutorial-71-implement-lock-screen-in-android/

I don't know how to change it to work in a Fragment?

Please help

My code:

public class Tab1fragment extends Fragment {

/**
 * @param args
 */
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.tab1_layout, container, false);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
PLorida
  • 103
  • 1
  • 2
  • 11
  • In the method `onCreateView`, you can implement your view. `R.layout.tab1_layout` is the layout. – yummy Apr 11 '15 at 08:16

1 Answers1

1

you have to read more about Fragment: http://developer.android.com/reference/android/app/Fragment.html

first you have to implement View.OnClickListener in your Fragment, then create a View inside onCreateView like :

View rootView = inflater.inflate(R.layout. tab1_layout, container, false);

so your fragment became :

public class Tab1fragment extends Fragment implements View.OnClickListener {

.
.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.tab1_layout, container, false);
    mDevicePolicyManager = ...
    mComponentName = ...
    Button btnEnableAdmin = (Button) rootView.findViewById(R.id.btnEnable);
    .
    .

    btnEnableAdmin.setOnClickListener(this);
    .
    .
    return rootView;

}

public void onClick(View v) {
    switch (v.getId()) {
         ...
    }
}

}

NB: I tested this code on my device

Mounir Elfassi
  • 2,242
  • 3
  • 23
  • 38
  • tks so much. I did it but I'm stucking with ComponentName. My code: mComponentName = new ComponentName(this,MyAdminReceiver.class); it's said the constructor Component name is undefined :( – PLorida Apr 11 '15 at 14:00
  • just replace 'this' by 'getactivity'.. so it became"mComponentName = new ComponentName(getActivity(), MyAdminReceiver.class);" – Mounir Elfassi Apr 11 '15 at 14:08
  • Plz help me this Error. when I started project http://stackoverflow.com/questions/29586229/please-help-me-error-when-starting-lockscreen-android-project – PLorida Apr 12 '15 at 06:38