How to use this function what 'View group' on android-studio - android

I don't have enough skills to code. But I have to make this program. so please help me...
I want to make function to slide the screen sideways in Android but it isn't working what I made. how can I do? please tell me.
PS: I want to use 'View group' and this code is on the MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sv = SlidingView(this)
val v1 = View.inflate(this, R.layout.t1, null)
val v2 = View.inflate(this, R.layout.t2, null)
sv.addView(v1)
sv.addView(v2)
setContentView(sv)
setContentView(R.layout.activity_main)
}
}

Seems like you are overriding your SlidingView with activity_main, you are calling setContentView twice and the latest call overrides the first one, so your view gets replaced with what's in layout activity_main, if you do not have anything in activity_main, do remove setContentView(R.layout.activity_main)
or
if you want SlidingView to be a part of activity_main, then add it to activity_main xml instead.

Related

Android can't change height of constraint layout in post

I am trying to change the height of the ContraintLayout inside the post block.
like so:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
changeHeight()
}
private fun changeHeight() {
val layoutView = findViewById<ConstraintLayout>(R.id.block1)
layoutView.post {
Log.d("222", "1===========")
layoutView.run {
Log.d("222", "2===========")
layoutParams.height = 1000
setBackgroundColor(Color.BLUE)
}
Log.d("222", "3===========")
}
}
}
It doesn't work at all.
And the logs are printed out and the color was changed too.
If it removed the layoutView.post then it works.
I want to figure out why this happened, anyone knows? thanks!
I tried to move the changeHeight() into a button listener to see if the view isn't attached or something relative view. But still the same.

How to create and display Fragments from inside an Android Library?

I'm relatively new to Android / Kotlin.
I wonder how Android libraries like AdMob manage to create and show a View (especially an Intersitial Ad) from inside a library without any layout preparation of the integrating app. I assume this View is some sort of Fragment.
Sample code to show an intersitial ad from AdMob:
I think it somehow has to do with the Activity passed as a parameter in the show method.
if (mInterstitialAd != null) {
mInterstitialAd?.show(this)
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.")
}
This Guide states, that to add a fragment programmatically, "the layout should include a FragmentContainerView". Additionally in the sample code from the same guide the id of said FragmentContainerView is used to add the fragment. This id is not known inside the library.
class ExampleActivity : AppCompatActivity(R.layout.example_activity) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
supportFragmentManager.commit {
setReorderingAllowed(true)
add<ExampleFragment>(R.id.fragment_container_view)
}
}
}
}
How does such a library achieve this?
I wonder how Android libraries like AdMob manage to create and show a View (especially an Intersitial Ad) from inside a library without any layout preparation of the integrating app.
What "layout preparations" would the integrating app need to do? Given an Activity, which you pass to the method, any code can use Activity.startActivity to launch it's own Activity which can be styled / themed in any way with any layout the library chooses (such as showing an interstitial ad).
I assume this View is some sort of Fragment.
Why would you assume that? It could be a Fragment, but it would be contained within an Activity, which could be launched as I've indicated above.
This Guide states, that to add a fragment programmatically, "the layout should include a FragmentContainerView". Additionally in the sample code from the same guide the id of said FragmentContainerView is used to add the fragment. This id is not known inside the library.
Right. But that again assumes tha the library is only using a Fragment and trying to shove it into your heirarchy. That's highly unlikely. It's more likely starting a brand new Activity that it knows about and has full control over.
I managed to achieve it.
A working demo can be found here: https://github.com/eat-your-broccoli/add-fragment-from-library-demo
Library:
class FragmentManager {
companion object {
fun showFragment(activity: AppCompatActivity) {
(activity.supportFragmentManager.findFragmentById(android.R.id.content) == null) {
activity.supportFragmentManager.beginTransaction()
.add(android.R.id.content, DemoFragment())
.addToBackStack(null)
.commit()
}
}
}
}
class DemoFragment : Fragment() {
private lateinit var btnBack: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_demo, container, false)
// enable back button
btnBack = view.findViewById(R.id.btn_back)
btnBack.setOnClickListener {
this.activity?.supportFragmentManager?.popBackStack()
}
return view
}
}
And in my activity:
class MainActivity : AppCompatActivity() {
lateinit var btnSwitch: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnSwitch = findViewById(R.id.btn_switch)
btnSwitch.setOnClickListener {
FragmentManager.showFragment(this)
}
}
}
A problem I had was that using R.id.content instead of android.R.id.content caused an execption and crashed the app.

Navigating to fragment doesn't Work - Kotlin

I was trying to open some fragment however the app keep crashing
I am wondering if it was possible to fix the issue that I got
class HomePage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_page)
val addMed = findViewById<Button>(R.id.addMedButton)
val fragManager: FragmentManager = supportFragmentManager
fragManager.beginTransaction().add(R.id.frameLayout, addMed).commit()
}
}
You have to add a fragment, not a button.
Besides, if the button is found by id, it is already present in the activity.

Can't call findViewById() while working with Fragments

While studying Kotlin, I got trouble with fragment sessions.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Log.d("life_cycle", "F onViewCreated")
super.onViewCreated(view, savedInstanceState)
val pass: Button = findViewById(R.id.pass)
pass.setOnClickListener {
dataPassListener.onDataPass("Good bye")
}
}
I want to write val pass: Button = findViewById(R.id.pass), but I can't use findViewById().
I think the problem is that androidx.appcompat.app.AppCompatActivity is not imported.
Guessing from what you've written, this snippet is from one of your classes that extends some variant of the Fragment class.
If this is the case, what you want to do is:
val pass: Button = view.findViewById(R.id.pass)
The difference here is that findViewById() is invoked against the view instance passed in the lifecycle callback.
(This assumes that the R.id.pass resource is defined in the view inflated by this Fragment).
you have to use view.findViewById
val pass: Button = view.findViewById(R.id.pass)

Issue with OnClickListener

I got an issue with SetOnClickListener in mine mobile kotlin app on AndroidStudio.
I created menu with this tutorial https://www.youtube.com/watch?v=sZWMPYIkNd8
Its ok! works fine on HAXM emulator but i can't manage to make mine button interactive..Tutorial guide tells me to initialize Button with SetOnClickListener
Android studio keeps asking me for more arguments and editors displays not-enough-information-to-infer-parameter-t-with-kotlin-and-android which makes me stuck.
I'm learning this language but resolving this problem is kinda out of mine range...what i need to know to properly implement OnClickListener?
Guy on youtube video doesnt put any extra phrase in brackets. so what i should do?
Make sure that you use braces {} instead of brackets after "view.setOnClickListener"
You can implement OnClickListeners a few different ways.
For a simple implementation you can do the following:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
button.setOnClickListener {
// do something when the user clicks the button
}
}
Or you can have your activity handle it like so:
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
<yourButton>.setOnClickListener(this)
}
override fun onClick(v: View?) {
when (v) {
(<yourButton>) -> {
// do something when the user clicks the button
}
else -> return
}
}
}
Here is a decent source if you want to read more.
https://antonioleiva.com/lambdas-kotlin-android/

Categories

Resources