Hepsini indir

package com.example.ali.myapplication;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.example.ali.myapplication.fragment1.form;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  RelativeLayout rl;
  LinearLayout ll;
  Fragment frg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PageCreate();
    }

    public void PageCreate()
    {
        rl=(RelativeLayout)findViewById(R.id.cerceve);//layoutu alıyoruz cünkü icersisine ekleme yapmak için
        ll=new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ll.setLayoutParams(lp);//genislik ve yükseklik ayarlandi
            Button btn=new Button(this);
            btn.setText("fragment'e geçis");
            int id=1;
            btn.setId(id);
            btn.setOnClickListener(this);
            ll.addView(btn);
            rl.addView(ll);
    }

    @Override
    public void onClick(View view) {
       frg=new form();
        FragmentManager fm=getSupportFragmentManager();//fragment yöneticisi
        FragmentTransaction ft=fm.beginTransaction();//transaction islemi basladı
        ft.replace(rl.getId(),frg);//layoutlar yer degistirdi
        ft.commit();//
    }

}
package com.example.ali.myapplication.fragment1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.ali.myapplication.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class form extends Fragment  {
  Button btn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      final Fragment fgm=this;
      View view=inflater.inflate(R.layout.fragment_form,container,false);//view 'e esitledik componentleri baglamak için
      btn=(Button)view.findViewById(R.id.btn);
      btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              getFragmentManager().beginTransaction().remove(fgm).commit();//fragment silindi ve onaylandı
          }
      });
      return view;
    }


}