package com.example.sergey.noxmllayout;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

// From:
//    https://stackoverflow.com/questions/11402418/adding-a-button-to-activity-without-any-layout

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);

        Log.d("CYCLE", "onCreate");

        Intent intent = getIntent();

        Log.d("INTENT", intent.toString());

        Button button= new Button (this);
        LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = 8;
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

        button.setText("dynamic Button");

        LayoutParams params1 = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        getWindow().addContentView(button, params);

        // Important: For performance reasons, view inflation relies heavily on pre-processing
        //   of XML files that is done at build time. Therefore, it is not currently possible
        //   to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

        // Uncomment this, and Button2 appears on top of the title bar, not under it!
        //   works before or after inflate, so long as it's before addContentView
        //ViewGroup vg = findViewById(android.R.id.content);  //
        ViewGroup vg = (ViewGroup) findViewById(R.id.action_bar);  //


        Log.d("WND", vg.toString());

        LayoutInflater inflater =
                (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflated = inflater.inflate(R.layout.button, vg /*vg or null*/, false);

        // true for attachToRoot above adds the button; addContentView then fails with
        //   the exception "the child already has a parent"
        // this.getWindow().addContentView(inflated, params1);

        vg.addView(inflated);

        // add inflated explicit XML?

        button.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.d("BUTTON", "CLICKED");
            }

        });
    }

    // onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy().

    @Override protected void onStart(){
        Log.d("CYCLE", "onStart");
        super.onStart();
    }

    @Override protected void onResume(){
        Log.d("CYCLE", "onResume");
        super.onResume();
    }

    @Override protected void onPause(){
        Log.d("CYCLE", "onPause");
        super.onPause();
    }

    @Override protected void onStop(){
        Log.d("CYCLE", "onStop");
        super.onStop();
    }

    @Override protected void onDestroy(){
        Log.d("CYCLE", "onDestroy");
        super.onDestroy();
    }
}
