I've trying to implement custom fragment with loading animation. Same code works fine with activity, but not works with fragment.
Here my fragment layout code:
<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"
tools:context="pw.osin.musicexpert.fragments.BusyFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/fragment_animation_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:background="#drawable/vinyl" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#color/colorPrimaryDark">
<TextView
android:id="#+id/fragment_animation_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="#string/message_data_is_loading"
android:textColor="#color/colorPrimaryYellow"
android:textSize="20dp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
My methods which I invokes in OnCreate method in fragment
private fun setAnimation() {
Log.i(TAG, "Установка анимации")
val drawable = AnimationUtils.loadAnimation(this.context, R.anim.rotate_anim)
mAnimationDescription?.setText(R.string.message_data_is_loading);
mAnimationImage?.startAnimation(drawable)
}
private fun initViewItems(view: View?) {
Log.i(TAG, "Поиск элементов View")
mAnimationDescription = view?.findView<TextView>(R.id.animation_description);
mAnimationImage = view?.findView<ImageView>(R.id.animation_logo);
}
My rotate animation code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360" />
</set>
Here I attach my animation fragment to activity in OnCreate method
private var mTransaction: FragmentTransaction? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
attachFragment()
}
private fun attachFragment() {
mTransaction = supportFragmentManager.beginTransaction()
mTransaction?.add(R.id.main_activity_container, BusyFragment.newInstance("Получаем дату"))
mTransaction?.commit()
}
Where I need to invoke my start animation method?
Thanks.
From the documentation of onResume:
Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.
You want to run the animation when it can be visible in the first place, hence I suggest starting the animation in your fragment's onResume or override the onFragmentsResumed method in the activity that this fragment is attached to.
Related
i have this toolbar
i want to remove the title of the fragment so i can display more items
is it from the xml file or the activity?
because i tried to add this to my main activity:
getActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setSupportActionBar(toolbar).setTitle("");
toolbar.setTitle(null)
but none of them worked
any suggestion?
this is MainActiviy code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
setupBottomNavMenu(navController)
setupSideNavigationMenu(navController)
setupActionBar(navController)
Since you are using a Navigation component you can use the addOnDestinationChangedListener to set your custom title after your setup method.
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.xxxx) {
//If in your setup you are using a Toolbar
toolbar.title = ""
//supportActionBar?.title = "" //If you are using setSupportActionBar()
} else {
//
}
}
You could create your own tool bar for each fragment and customize it as per your requirement. I have made a common toolbar xml file and reusing it in every fragment and customizing it. Look :
common_toolbar.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/const_item"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/margin_normal"
android:gravity="center"
android:padding="#dimen/padding_five"
android:text="Back"
android:textColor="#android:color/white"
android:textSize="#dimen/font_size_fifteen"
app:drawableStartCompat="#drawable/ic_action_chevron_left"
app:drawableTint="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/title_toolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="#string/menu"
android:textColor="#android:color/white"
android:textSize="#dimen/font_size_fifteen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
And then adding it to fragments's UI. Let's say, adding to fragment_user_details.xml
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar_common"
android:id="#+id/toolbar_user_details"/>
<!--
Other View widgets
-->
</androidx.constraintlayout.widget.ConstraintLayout>
As each fragment has its own tool bar, so I am getting it by it's id and customize as per my requirement. Make sure Activity has Theme.MaterialComponents.Light.NoActionBar theme.
Menu Items can also be added like below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
I created custom toolbar inside fragment,but i got view like this:
In themes.xml i disabled ActionBar:
<style name="Theme.Project" parent="Theme.MaterialComponents.DayNight.NoActionBar">
This is my custom toolbar layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white"
>
<TextView
android:id="#+id/toolbar_title"
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Home"
android:fontFamily="#font/google_sans"
android:textColor="#color/black"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/fragment_home" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And this is HomeFragment.java:
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.toolbar_in_home, container, false);
return root;
}
}
How to remove this white thing on top of screen?
UPD:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
</androidx.constraintlayout.widget.ConstraintLayout>
This is fragment home(just ConstraintLayout)
UPD2:i also was trying use toolbar in activity_main and it works:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_examination, R.id.navigation_profile)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(navView, navController);
}
But i can't do changes if i want to go on other fragment.
I need to set another custom toolbar layout with button like this:
So this is how I did it. My app is based on single activity-multi-fragment model.
I had set activity theme to AppTheme.NoActionBar, so it enabled me to set a custom toolbar.
Later for each fragment I had a separate layout and within which added a custom toolbar like below
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary">
....
....
</androidx.appcompat.widget.Toolbar>
Within this layout I had added menu items.
If you don't want to follow this, may be you should look at getActionBar().setCustomView(). Keep your activity theme set, so getActionBar() is not null.
You can control action bar layout by calling getActionBar().setCustomView() in fragment onResume() or probably when you are adding or removing a fragment in your activity.
You can use your custom toolbar inside the main layout replace it from com.google.android.material.appbar.AppBarLayout.
Here is an example code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/colorPrimary">
<ImageView
android:id="#+id/navi"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:contentDescription="#string/todo"
android:src="#drawable/navi" />
<ImageView
android:layout_width="180dp"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:contentDescription="#string/todo"
android:src="#drawable/header2" />
</RelativeLayout>
<include layout="#layout/fragment_home" />
</RelativeLayout>
I have a simple project with 3 fragments and i'm trying to add animations to it transitions.
It navigates when i click the buttons but now i'm trying to add animations to those transitions in the nav graph but for some reason the destination of the actions are not being registered.
This is the code i have already:
Main Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupActionBarWithNavController(findNavController(R.id.fragment))
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment)
return navController.navigateUp() || super.onSupportNavigateUp()
}
Fragment A:
class FragmentA : Fragment() {
private var _binding: FragmentABinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentABinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val navController : NavController = Navigation.findNavController(view)
binding.button1.setOnClickListener {
navController.navigate(R.id.fragmentB)
}
binding.button2.setOnClickListener {
navController.navigate(R.id.fragmentC)
}
}
}
Main activity 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<fragment
android:id="#+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment A xml:
<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="#android:color/holo_purple"
tools:context="com.example.FragmentA">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="#string/fragmentB"
android:textColor="#color/black" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="#string/fragmentC"
android:textColor="#color/black" />
</LinearLayout>
</FrameLayout>
And the other 2 fragments just have 2 buttons and nothing of logic aplicated yet.
So when i try to create an action for the navigation from Fragment A (Home fragment) to fragment B the desing window normally shows the id of the action and its destinantion but i don't know why it is not registering the destination when i create the action. It looks like this now:
I tried to add the destination manually but it doesn't works either because it just delete it and let it blank again.
Nav Graph xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
app:startDestination="#id/fragmentA">
<fragment
android:id="#+id/fragmentA"
android:name="com.example.FragmentA"
android:label="fragment_a"
tools:layout="#layout/fragment_a" >
<action
android:id="#+id/action_fragmentA_to_fragmentB2"
app:destination="#id/fragmentB" />
</fragment>
<fragment
android:id="#+id/fragmentB"
android:name="com.example.FragmentB"
android:label="fragment_b"
tools:layout="#layout/fragment_b" />
<fragment
android:id="#+id/fragmentC"
android:name="com.example.FragmentC"
android:label="fragment_c"
tools:layout="#layout/fragment_c" />
</navigation>
Animation xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%" android:toXDelta="0%" android:duration ="800"/>
</set>
I added this custom animation to enterAnim field but its not taking it.
I don't really know whats going on and i didn't find any answer to this issue.
I found my error and it was on the Fragmnet A code. It was about the click listener.
I had it like:
binding.button1.setOnClickListener {
navController.navigate(R.id.fragmentB)
}
but i need to add the action instead:
binding.button1.setOnClickListener {
findNavController().navigate(R.id.action_fragmentA_to_fragmentB)
}
The destination field is bugged i think, now everything works as it should.
This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Closed 5 years ago.
I want to know if I have a text like "hello world".
How can I set the preference of this text view that user can change the font size to medium, large and small like 20sp, 25sp, 30sp.
If you have an activity, which layout have a
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="20sp"
/>
so, you need an user input like buttons and in the activity you can changue the size of the textview element detecting an event relate to the user input, in this example the "onclick" method.
here is the code to archieve that.
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_height="match_parent">
<TextView
android:id="#+id/txt_hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HelloWorld"
android:textColor="#android:color/black"
android:textSize="20sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txt_hello_world"
android:orientation="horizontal"
>
<Button
android:id="#+id/btn_20sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="20sp"
/>
<Button
android:id="#+id/btn_30sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="30sp"
/>
<Button
android:id="#+id/btn_40sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="40sp"
/>
</LinearLayout>
Activity
class MainActivity : AppCompatActivity(), View.OnClickListener {
lateinit var helloText : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
helloText = findViewById(R.id.txt_hello_world)
var btn20sp: Button = findViewById(R.id.btn_20sp)
btn20sp.setOnClickListener(this)
var btn30sp: Button = findViewById(R.id.btn_30sp)
btn30sp.setOnClickListener(this)
var btn40sp: Button = findViewById(R.id.btn_40sp)
btn40sp.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id){
R.id.btn_20sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,20f)
R.id.btn_30sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,30f)
R.id.btn_40sp -> helloText.setTextSize(TypedValue.COMPLEX_UNIT_SP,40f)
}
}
}
The code is in kotlin but the logic is the samen in java
I have the following DialogFragment
public class DetailItemFragment extends AppCompatDialogFragment {
#BindView(R.id.task_detail_name)
AppCompatTextView taskDetailNameText;
#BindView(R.id.task_detail_description)
AppCompatTextView taskDetailDescriptionText;
#BindView(R.id.task_detail_priority)
AppCompatTextView taskDetailPriorityText;
#BindView(R.id.toolbar)
Toolbar toolbar;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true); //doesn't work
dialog.setCancelable(true); //doesn't work
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail_view, container, false);
ButterKnife.bind(this, view);
//Get the Json back and parse it as a Task
Bundle bundle = getArguments();
final String taskAsJson = bundle.getString("task");
Task task = new Gson().fromJson(taskAsJson, Task.class);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.close);
}
setHasOptionsMenu(false);
taskDetailNameText.setText(task.getName());
taskDetailDescriptionText.setText(task.getDescription());
taskDetailPriorityText.setText(task.getPriority().toString());
return view;
}
I perform the Fragment transaction like so
//Put the clicked item into a Bundle for the next fragment to consume
DialogFragment detailItemFragment = new DetailItemFragment();
Bundle args = new Bundle();
args.putString("task", new Gson().toJson(mTaskListAdapter.getItem(position)));
detailItemFragment.setArguments(args);
getActivity().getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.add(R.id.task_detail_fragment_container, detailItemFragment)
.addToBackStack(null)
.commit();
I'm trying to accomplish the following:
* On a tablet/large screen, this displays as a Dialog popup
* On a phone, it will display a fullscreen dialog (pretty much trying to replicate how Google Calendar handles opening event details)
This is what my xlarge/activity_main.xml looks like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/task_detail_fragment_container"
android:layout_width="800dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
android:elevation="16dp" />
<android.support.design.widget.AppBarLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolbarStyle"
android:background="#color/colorPrimary"
app:title="#string/app_name"
app:titleTextColor="#color/white" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/task_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="#dimen/fab_normal_size"
android:layout_height="#dimen/fab_normal_size"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="#dimen/fab_padding"
android:layout_marginEnd="#dimen/fab_padding"
android:src="#drawable/add_black"
app:elevation="#dimen/fab_elevation"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="#dimen/fab_pressed_elevation" />
</RelativeLayout>
You'll notice I have a FrameLayout that is centered on the screen for loading the DetailItemFragment into. Maybe this isn't the correct way to do this--but I want to bring it up in case that turns out to be the issue.
This is what the fragment_detail_view.xml layout looks like
<android.support.design.widget.CoordinatorLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/task_detail_background"
android:clickable="true"
android:elevation="8dp"
tools:context="io.havoc.todo.view.fragments.DetailItemFragment">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_tall_height"
android:background="?attr/colorPrimary">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_name"
style="#style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:gravity="start|bottom"
android:padding="8dp"
android:text="Task detail"
android:textColor="#color/white"
android:textSize="24sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginStart="86dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.AppCompatTextView
style="#style/TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_margin"
android:text="#string/hint_task_description"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Description text"
android:textColor="#color/text_primary"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatTextView
style="#style/TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_margin"
android:text="#string/hint_task_priority"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Priority"
android:textColor="#color/text_primary"
android:textSize="18sp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
The issue
When the DialogFragment is open on a tablet, the Activity in the background still accepts touches (i.e. I can interact with the Activity as if the dialog wasn't even present); furthermore, I have no idea how to get the DialogFragment to dismiss when touching outside of it. I've tried the following:
dialog.setCanceledOnTouchOutside(true); (in the onCreateDialog() method)
dialog.setCancelable(true); (in the onCreateDialog() method)
dialog.setCanceledOnTouchOutside(false); (in the onCreateDialog() method)
getDialog().setCanceledOnTouchOutside(true); setCancelable(true); (leads to NullPointerExceptions--performed in onCreateView() for the DialogFragment)
I've gone through all the other questions like this I could find and nothing has worked for me--so clearly I'm doing something wrong.
I appreciate any and all help.
Change to .dialog.setCancelable(false);.
If it's a dialog with OK and Cancel buttons, you can use:
.setCancelable(closeActivity == true ? false : true)
The FrameLayout (task_detail_fragment_container) you are adding your DetailItemFragment TO has a different size then the parent, which means it will not cover the entire layout:
...
android:layout_width="800dp"
android:layout_height="400dp"
...
To fix this, make task_detail_fragment_container the same size as the parent layout (match_parent) and cap the size of the dialog in the dialog layout by introducing another Layout after your container layout:
<android.support.design.widget.CoordinatorLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:clickable="true"
android:elevation="8dp"
tools:context="io.havoc.todo.view.fragments.DetailItemFragment">
<FrameLayout
andorid:width="800dp"
android:height="400dp"
android:layout_centerInParent="true">
<android.support.design.widget.AppBarLayout ...
//rest of your xlm here
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Change this
dialog.setCanceledOnTouchOutside(true);
with
dialog.setCanceledOnTouchOutside(false);