Week 3(b): Design an application that is used to make Dialogs that float on
Activity
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="100dp"
android:text="open fragment">
</Button>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.dialogfragment;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDialogFragment one=new MyDialogFragment();
one.show(getSupportFragmentManager(),"MyFragment");
}
});
}
}
dialogfrag.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyDialogFragment">
<TextView
android:layout_width="317dp"
android:layout_height="400dp"
android:background="#FFEE99"
android:gravity="center"
android:text="You are in Dialog Fragment"
android:textSize="50dp" />
</LinearLayout>
MyDialogFragment.java
package com.example.dialogfragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
public class MyDialogFragment extends DialogFragment {
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.dialogfrag, container, false);
}
}