How to retrieve data from firebase into a recycler view using Kotlin (including images)? - firebase

I'm retrieving data from firebase into a recycler view using Kotlin. I'm able to load the text easily but unable to load the images. I have used Picasso library for the same. There is no error in the code, can anyone suggest something?
Following is my code:
1) This is the main activity class
class MainActivity : AppCompatActivity()
{
lateinit var recyclerView: RecyclerView
lateinit var ref:DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView=findViewById(R.id.rvFirebase)
ref= FirebaseDatabase.getInstance().reference.child("Restaurants")
recyclerView.layoutManager=LinearLayoutManager(this)
val option=FirebaseRecyclerOptions.Builder<ResModel>().setQuery(ref,ResModel::class.java).build()
val firebaseRecyclerAdapter= object :FirebaseRecyclerAdapter<ResModel,RViewHolder>(option){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RViewHolder {
val itemView= LayoutInflater.from(this#MainActivity).inflate(R.layout.single_row,parent,false)
return RViewHolder(itemView)
}
override fun onBindViewHolder(holder: RViewHolder, position: Int, model: ResModel) {
val refId = getRef(position).key.toString()
ref.child(refId).addValueEventListener(object:ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
holder.rCost.text = model.resCost
holder.rName.text = model.resName
Picasso.get().load(model.resImage).into(holder.rImage)
holder.rRating.text=model.resRating
}
})
}
}
recyclerView.adapter=firebaseRecyclerAdapter
firebaseRecyclerAdapter.startListening()
}
class RViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
var rImage:ImageView=itemView.findViewById(R.id.imgRestaurant)
var rName:TextView=itemView.findViewById(R.id.txtRestaurantName)
var rCost:TextView=itemView.findViewById(R.id.txtCost)
var rRating:TextView=itemView.findViewById(R.id.txtResRating)
}
}
2) This is the model class
class ResModel
{
var resCost: String =" "
var resImage: String=" "
var resName: String=" "
var resRating: String=" "
constructor():this("","","",""){
}
constructor(resCost:String,resImage: String,resName:String,resRating:String){
this.resCost=resCost
this.resImage=resImage
this.resName=resName
this.resRating=resRating
}
}
3)This is the main activity xml file
<?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="wrap_content"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="#+id/rvFirebase"/>
</RelativeLayout>
4) This is the card view xml file which is being used in recycler view
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"
app:cardCornerRadius="4dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/carViewXml">
<LinearLayout
android:id="#+id/llContent"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:background="#ffffff"
android:weightSum="6">
<ImageView
android:layout_marginTop="6dp"
android:layout_weight="1.7"
android:id="#+id/imgRestaurant"
android:layout_width="0dp"
android:layout_height="90dp"
android:src="#drawable/ic_food"
android:padding="5dp"
android:layout_marginStart="10dp"
android:scaleType="centerCrop"
android:fadingEdge="vertical"
android:background="#drawable/rounded_image"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_weight="3.3"
android:layout_marginStart="5dp">
<TextView
android:id="#+id/txtRestaurantName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="name_of_the_restaurant"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtCost"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/txtRestaurantName"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:padding="8dp"
android:text="Rs. 299"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="9dp"
android:layout_weight="1"
android:layout_marginStart="3dp">
<ImageView
android:id="#+id/imgFavResIcon"
android:layout_width="match_parent"
android:layout_height="23dp"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:src="#drawable/ic_favorite_res" />
<TextView
android:id="#+id/txtResRating"
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_below="#id/imgFavResIcon"
android:layout_marginTop="22dp"
android:padding="4dp"
android:text="4.5"
android:textAlignment="center"
android:textColor="#FB8C00"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
4) This is the image of how I stored data on firebase database and storage
Firebase Databse
Firebase storage

Not sure how Picasso can handle Firebase ui storage but with glide you do something like this:
implementation 'com.firebaseui:firebase-ui-storage:6.3.0' add firebase UI Storage
Then:
val storageReference: StorageReference = model.resImage //not sure about the reference here - eg FirebaseStorage.getInstance().getReference().child("yourImageReferencePath")
Glide.with(this /* context */)
.using(FirebaseImageLoader())
.load(storageReference)
.into(holder.rImage)
Also please take a look at other resources

Related

androidx.spinner onItemSelectedListener not working

Am trying to use a spinner in a layout. I can add and select items to the spinner but I cannot retrieve the selected item. This is the main activity:
class pageNewPurchaseOrder : AppCompatActivity() {
lateinit var spinnerVendorX: androidx.appcompat.widget.AppCompatSpinner
var spinnerArray: java.util.ArrayList<CharSequence> = ArrayList<CharSequence>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_page_new_purchase_order)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowTitleEnabled(false)
spinnerVendorX = findViewById(R.id.spinnerVendorX)
spinnerVendorX.adapter = ArrayAdapter(this#pageNewPurchaseOrder, android.R.layout.simple_spinner_item, spinnerArray)
spinnerVendorX.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
Log.d("aaa", "This code is not running!")
}
}
}
}
This is the layout activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:background="?attr/colorPrimary"
android:fillViewport="true"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".pageNewPurchaseOrder"
tools:showIn="#layout/activity_page_new_purchase_order">
<androidx.appcompat.widget.AppCompatSpinner
android:padding="10dp"
android:background="#drawable/bgselect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lbl2"
android:id="#+id/spinnerVendorX" />
</androidx.core.widget.NestedScrollView>
Am getting result this on the logcat when I select an item: E/ViewRootImpl(31835): sendUserActionEvent() mView == null.I get tj=he same results even when I use <Spinner instead of <androidx.appcompat.widget.AppCompatSpinner
User must have to add data in the ArrayList. Here, you are initialising arrayList and set it but not adding data in ArrayList. Please add data in ArrayList then try it. Thank you.

Button onclicklistener not fired in Fragment

I'm using Kotlin 1.3.61, kotlin extensions, androidx.appcompat:appcompat:1.1.0, androidx.constraintlayout:constraintlayout:1.1.3, androidx.core:core-ktx:1.1.0, org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61
and access a button defined in my xml in the fragment, like so:
class MyFragment : Fragment(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(TAG, "In onCreate()")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.i(TAG, "In onCreateView()")
return inflater.inflate(R.layout.fragment_map_bottom_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
whereBtn.setOnClickListener {
Log.i(TAG, "where clicked but first click doesn't register")
}
but the first click of the button is never fired but the second and subsequent clicks do fire.
I tried #ContainerOptions(CacheImplementation.SPARSE_ARRAY) for the fragment but no luck.
Here is the fragment layout file:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="#color/white">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/headerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:text="#string/navHeaderTitle"
android:textAppearance="#style/ChipTextCaptionFont"
android:textColor="#color/black"
app:layout_constraintBottom_toTopOf="#+id/whereBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/whereBtn"
style="#style/DrawerMenuItemsFont"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_below="#+id/headerTitle"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="32dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:drawablePadding="10dp"
android:drawableStart="#drawable/ic_search_24px"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:hint="#string/where_desc"
android:clickable="true"
android:focusable="true"
android:textAppearance="#style/DrawerProfileCaptionFont"
android:textSize="16sp"
android:textColorHint="#color/black"
android:background="#drawable/rect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.285"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/headerTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
it seems that whereBtn is focusable.
There is no event to perform click at first time because focus change already has consumed the event.

Trying to do Automatic image slider in a fragment in kotlin, However runouithread is not working.Any possible solutions?

I'm trying to do Automatic image slider in fragment in kotlin.
I am trying to convert an activity to fragment. However runouithread is not working in fragments.Any possible solutions??
I tried couple of other things also. But not a possible solution, I can do image slide inside the fragment but not to dod the auomatic sliding....
My mainactivity()
package com.example.sliderimagemovie
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="409dp"
android:layout_height="290dp"
android:name="com.example.sliderimagemovie.HomeFragment"
android:id="#+id/fragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="441dp"/>
</android.support.constraint.ConstraintLayout>
slide_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="275dp">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp" app:srcCompat="#drawable/ic_launcher_background"
android:id="#+id/slide_image"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" android:scaleType="centerCrop"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" app:srcCompat="#drawable/ic_play_button"
android:id="#+id/glide_play_button"
app:layout_constraintTop_toTopOf="#+id/slide_image" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="#+id/slide_image"
app:layout_constraintEnd_toEndOf="#+id/slide_image"
app:layout_constraintStart_toStartOf="#+id/slide_image" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" app:backgroundTint="#color/colorAccent"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp" app:srcCompat="#drawable/gradient_bg"
android:id="#+id/imageView3" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/movie_title" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/glide_play_button"
app:layout_constraintBottom_toBottomOf="#+id/imageView3" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="24dp" android:layout_marginRight="24dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="24dp"
android:layout_marginStart="24dp" android:textSize="24sp" app:layout_constraintHorizontal_bias="0.0"
android:textColor="#android:color/holo_red_light"/>
</android.support.constraint.ConstraintLayout>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".HomeFragment">
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="0dp"
android:layout_height="275dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:id="#+id/view_pager_glide"/>
<android.support.design.widget.TabLayout
app:tabBackground="#drawable/indicator_item"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
android:layout_width="395dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/view_pager_glide" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:id="#+id/indicator">
</android.support.design.widget.TabLayout>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
slide.kt
package com.example.sliderimagemovie.Model
class Slide(var image:Int, var title:String)
slidePagerAdapater.kt
package com.example.sliderimagemovie.Adapter
import android.content.Context
import android.support.v4.view.PagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.sliderimagemovie.Model.Slide
import com.example.sliderimagemovie.R
class SlidePagerAdapter:PagerAdapter {
var mContext: Context?=null
var mList:List<Slide>?=null
constructor(context: Context, list:List<Slide>)
{
mContext=context
mList=list
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
var inflater:LayoutInflater= mContext!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var slideLayout:View =inflater.inflate(R.layout.slide_item,null)
var slideImage:ImageView =slideLayout.findViewById(R.id.slide_image)
var slideText:TextView=slideLayout.findViewById(R.id.movie_title)
slideImage.setImageResource(mList!!.get(position).image)
slideText.setText(mList!!.get(position).title)
container.addView(slideLayout)
//return super.instantiateItem(container, position)
return slideLayout
}
override fun isViewFromObject(p0: View, p1: Any): Boolean {
return p0 == p1
}
override fun getCount(): Int {
return mList!!.count()
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//super.destroyItem(container, position, `object`)
container.removeView(`object` as View)
}
}
HomeFragment.kt
package com.example.sliderimagemovie
import android.app.Application
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sliderimagemovie.Adapter.SlidePagerAdapter
import com.example.sliderimagemovie.Model.Slide
import kotlinx.android.synthetic.main.fragment_home.*
import kotlin.concurrent.fixedRateTimer
class HomeFragment : Fragment() {
var lstSlides:MutableList<Slide> =ArrayList()
var sliderPage: ViewPager?=null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
sliderPage=view.findViewById(R.id.view_pager_glide)
lstSlides.add(Slide(R.drawable.spiderman,"Slide1 Spiderman"))
lstSlides.add(Slide(R.drawable.avenger,"Slide2 Avenger"))
lstSlides.add(Slide(R.drawable.frozen2,"Slide3 Frozen2"))
lstSlides.add(Slide(R.drawable.fastfurious,"Slide4 Fastfurious"))
lstSlides.add(Slide(R.drawable.joker,"Slide5 Joker"))
lstSlides.add(Slide(R.drawable.jumanji,"Slide6 Jumanji"))
lstSlides.add(Slide(R.drawable.terminator,"Slide7 Terminator"))
var adapter: SlidePagerAdapter = SlidePagerAdapter(context!!,this.lstSlides)
sliderPage!!.adapter=adapter
indicator.setupWithViewPager(sliderPage,true)
fixedRateTimer(name = "timer",initialDelay = 4000,period = 6000){
// I'm getting error over here. Since runOnUiThread wont work with fragment , I tried to run with //activity. but its not working. I tried couple of other things.
getactivity().runOnUiThread
{
public override fun run() {
if (sliderPage!!.currentItem < lstSlides.count() - 1) {
sliderPage!!.currentItem = sliderPage!!.currentItem + 1
} else {
sliderPage!!.currentItem = 0
}
}
})
}
return view
}
}

Xamarin Android (PCL) Layout not correctly render in dependency service

I am trying to display a dialog on Android using Dependency Service. However, I could not get the design I create in android layout designer to display, the result comes out is different.
This is the screenshot of my previewer:
Expected design
This is the screenshot of my result:
Unexpected result
My axml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialogWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="#drawable/backgroundstyle"
android:layout_marginRight="16dp"
android:stateListAnimator="#null">
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="16dp"
android:textAlignment="center"
android:textColor="#000"
android:textSize="19sp"
android:text="TitleText"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:paddingBottom="16dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="4dp"
android:textAlignment="center"
android:text="subtitle test"
android:textColor="#000"
android:textSize="15sp" />
<RelativeLayout
android:id="#+id/RL_editTextWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/subtitle"
android:padding="15dp"
>
<EditText
android:id="#+id/inputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundedentry"
android:paddingBottom="8dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="8dp"
android:textAlignment="center"
android:textColor="#000"
android:text="inputText test"
android:textSize="15sp" />
</RelativeLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/RL_editTextWrapper"
android:background="#cdced2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/line"
android:orientation="horizontal">
<TextView
android:id="#+id/dialogButtonNO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:stateListAnimator="#null"
android:textAlignment="center"
android:textColor="#007aff"
android:text="Cancel"
android:textSize="19sp" />
<View
android:id="#+id/separator"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#cdced2" />
<TextView
android:id="#+id/dialogButtonOK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:stateListAnimator="#null"
android:textAlignment="center"
android:textColor="#007aff"
android:textSize="19sp"
android:text="Ok"
android:textStyle="bold" />
</LinearLayout>
The #drawable/backgroundstyle code:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#android:color/white"/>
<corners
android:radius="16dp" />
This is the code for exporting dependency service in android:
namespace FFF.Droid.Native {
public class IOSDialogStyleDroid : IIOSDialogStyle {
public void ShowPopup(EntryPopup entryPopup) {
var context = CrossCurrentActivity.Current.Activity as FormsAppCompatActivity;
CustomDialog customDialog = new CustomDialog(context) {
entryPopup = entryPopup
};
customDialog.Show();
}
}
public class CustomDialog : Dialog {
public Context context { get; set; }
public EntryPopup entryPopup { get; set; }
public CustomDialog(Context context) : base(context) {
this.context = context;
}
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
RequestWindowFeature((int)WindowFeatures.NoTitle);
SetContentView(Resource.Layout.iOSStyleDialog);
EditText editText = FindViewById(Resource.Id.inputText) as EditText;
(FindViewById(Resource.Id.title) as TextView).Text = entryPopup.Title;
(FindViewById(Resource.Id.subtitle) as TextView).Text = entryPopup.Message;
var btnOk = FindViewById(Resource.Id.dialogButtonOK) as TextView;
btnOk.Text = entryPopup.okButton;
btnOk.Click += (object sender, EventArgs e) => {
entryPopup.OnPopupClosed(new EntryPopupClosedArgs {
Button = entryPopup.okButton,
Text = editText.Text
});
Dismiss();
};
var btnNo = FindViewById(Resource.Id.dialogButtonNO) as TextView;
btnNo.Text = entryPopup.cancelButton;
btnNo.Click += (object sender, EventArgs e) => {
entryPopup.OnPopupClosed(new EntryPopupClosedArgs {
Button = entryPopup.cancelButton,
Text = editText.Text
});
Dismiss();
};
}
}
I found out that textalignment seems broken, I use gravity instead.

Android popup won't dismiss

I am trying to implement a popup in my application but it won't dismiss. The Popup is supposed to open when I click on a Plus button, and this works. However, it is supposed to close when I click on the Cancel button in popup layout but it doesn't. why?
Popup layout (tempo_popup.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:id="#+id/confirmtempo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Confirm" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:id="#+id/canceltempo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Cancel" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Java code:
public class MetronomeActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_metronome);
final Button plus = (Button) findViewById(R.id.tempop);
final Button minus = (Button) findViewById(R.id.tempom);
final Button confirm_tempo = (Button) findViewById(R.id.confirmtempo);
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pop = inflater.inflate(R.layout.tempo_popup, null, false);
final PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.tempo_popup, null, false),
250, 150, true);
// The code below assumes that the root container has an id called 'main'
pw.showAtLocation(plus, Gravity.CENTER, 0, 0);
Button cancel_tempo = (Button) pop.findViewById(R.id.canceltempo);
cancel_tempo.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Begin1", "POPUP CANCEL");
pw.dismiss();
}
});
}
});
}
}
Damn it! Replace single line of code, it will work like charm.
Instead:
PopupWindow popUpWindow = new PopupWindow(inflater.inflate(R.layout.tempo_popup, null, false),250, 150, true);
Use:
View view = inflater.inflate(R.layout.tempo_popup, null, false);
PopupWindow popUpWindow = new PopupWindow(view, 250, 150, true);
Does the Popup cancel log get printed? Otherwise this might help:
Android popup window dismissal

Resources