Skip to content

Instantly share code, notes, and snippets.

@serhatleventyavas
Created November 18, 2020 12:09
Show Gist options
  • Save serhatleventyavas/ede8455d51bfdc8c4d81c6c0d8501a33 to your computer and use it in GitHub Desktop.
Save serhatleventyavas/ede8455d51bfdc8c4d81c6c0d8501a33 to your computer and use it in GitHub Desktop.
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.material.snackbar.Snackbar
class FirstFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
pxToDp(view.measuredWidth)
pxToDp(view.measuredHeight)
val configuration = requireActivity().resources.configuration
val orientation = configuration.orientation
val smallestScreenWidthDp: Int = configuration.smallestScreenWidthDp
if (smallestScreenWidthDp >= 600) {
// 7.inc
val snackbar = Snackbar.make(requireView(), "7 inc tablet ve yukarısı", Snackbar.LENGTH_SHORT)
snackbar.show()
} else if (smallestScreenWidthDp >= 720) {
// 10.inc
val snackbar = Snackbar.make(requireView(), "10 inc tablet ve yukarısı", Snackbar.LENGTH_SHORT)
snackbar.show()
} else if (smallestScreenWidthDp >= 720 && orientation == Configuration.ORIENTATION_PORTRAIT) {
// 10.inc - portrait
val snackbar = Snackbar.make(requireView(), "10 inc ve dikey tablet ve yukarısı", Snackbar.LENGTH_SHORT)
snackbar.show()
} else {
val snackbar = Snackbar.make(requireView(), "Normal Telefon", Snackbar.LENGTH_SHORT)
snackbar.show()
}
}
fun dpToPx(dp: Int): Int {
return (dp * Resources.getSystem().displayMetrics.density).toInt()
}
fun pxToDp(px: Int): Int {
return (px / Resources.getSystem().displayMetrics.density).toInt()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment