How to navigate from bottomsheet to activity - java

I have a bottomsheet implemented in my app. I used inflater to show the bottomsheet dialog, here is my code in NewSignUpActivity to show the bottomsheet dialog.
class NewSignUpActivity : AppCompatActivity() {
private lateinit var btnBottomSheet: RelativeLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_rider_or_bike)
btnBottomSheet = findViewById(R.id.riderBtn)
val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback(){
override fun onStateChanged(bottomSheet: View, newState: Int) {}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
}
val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(bottomSheetView)
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView.parent as View)
bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback)
btnBottomSheet.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.show()
}
}
}
Here's how it looks like in view.
As you can see, there's button on the bottomsheet I want to navigate to another layout activity once I clicked the YES button, how can I do that? I am very new to android development using Kotlin.
EDITED: Added my XML file for bottomsheet dialog please see below._ name of file is layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutBtmSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/rectangle"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginRight="#dimen/_15sdp"
android:layout_marginBottom="#dimen/_30sdp"
android:src="#drawable/close_black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:background="#drawable/bgwhite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/ll_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginRight="#dimen/_10sdp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/outline_sports_motorsports_black_48"
tools:layout_editor_absoluteX="3dp"
tools:layout_editor_absoluteY="3dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#+id/ll_one"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_there_partner_rider"
android:textSize="18sp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/_5sdp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_100sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3"
app:layout_constraintVertical_bias="0.678">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/noBtn"
android:layout_width="169dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/bgbtn1"
android:radius="#dimen/_50sdp"
android:text="No" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/yellowBtn"
android:layout_width="169dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#color/white"
android:background="#drawable/bgbtno"
android:text="#string/yes" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#color/reply_black_800"
android:text="#string/are_you_using_your_own_motorcycle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.074" />
</androidx.constraintlayout.widget.ConstraintLayout>

Already the BottomSheetView layout is inflated with:
val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
And this layout hosts the YES button, so you can just use findViewById(). Assuming that the id of the YES button is btn_yes:
val btnYes = bottomSheetView.findViewById<Button>(R.id.btn_yes)
btnYes.setOnClickListener {
startActivity(myIntent) // Add your activity intent
}
UPDATE:
As you provided the id of the YES button which is yellowBtn, then you can use it:
val btnYes = bottomSheetView.findViewById<Button>(R.id.yellowBtn)
btnYes.setOnClickListener {
startActivity(myIntent) // Add your activity intent
}

Related

Not able to access action bar of app when bottom sheet is opened

I have a bookmark button in the action bar which is not clickable when I open a bottom sheet by clicking a button (the button is in fragment).
Here is the code :
activity_learn.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=".learn.LearnActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
mcq_fragment.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"
android:background="#color/light_cyan">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ic_close"
style="#style/ic_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_close"
tools:targetApi="lollipop" />
<ProgressBar
android:id="#+id/determinateBar"
style="#style/progressBar"
app:layout_constraintEnd_toStartOf="#+id/cardNumber"
app:layout_constraintStart_toEndOf="#+id/ic_close"
app:layout_constraintTop_toTopOf="parent" />
......more code ......
<com.adithya.memoneet.utils.FButton
android:id="#+id/check_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:fontFamily="#font/quando"
android:text="Check"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"
app:buttonColor="#color/light_gray"
app:cornerRadius="30dp"
app:layout_constraintBottom_toTopOf="#+id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear_layout"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
correct_wrong_bottom_sheet_dialog.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"
app:layout_behavior="#string/bottom_sheet_behavior"
app:behavior_hideable= "true"
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:fontFamily="#font/fredoka_one"
android:lineSpacingExtra="1sp"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Oops! Correct Answer is: \nHeart" />
<ozaydin.serkan.com.image_zoom_view.ImageViewZoom
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_memoneet"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview" />
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:visibility="gone"
app:autoPlay="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image"
app:showFullScreenButton="true"
app:showYouTubeButton="false" />
<com.adithya.memoneet.utils.FButton
android:id="#+id/continue_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="30dp"
android:fontFamily="#font/quando"
android:text="Continue"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"
app:buttonColor="#color/light_gray"
app:cornerRadius="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/youtube_player_view"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
McqFragment.java
//below function is called when the user clicks the button in MCQ fragment
public static void showCorrectWrongBottomSheetDialog(final FragmentActivity activity, String answer, String explanation, boolean correct, String subjectType, final CallBackListener callBackListener) {
try {
final BottomSheetDialog dialog = new BottomSheetDialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setDimAmount(0.01f);
dialog.setContentView(R.layout.correct_wrong_bottom_sheet_dialog);
dialog.setCancelable(false);
ConstraintLayout bottom_layout = dialog.findViewById(R.id.bottom_layout);
FButton continue_button = dialog.findViewById(R.id.continue_button);
TextView textview = dialog.findViewById(R.id.textview);
String finalExplanation;
AppCompatImageView imageView = dialog.findViewById(R.id.image);
..more code..
}
when the bottom sheet is opened in mcq fragment I am not able to click the bookmark button in action bar

I can't scroll the recyclerview even though it display all of the items

Recyclerview shows all the items, but I can't scroll it. Also in a way that I can't understand, once I click and open the searchView, I'm able to scroll just a bit (still not fully). How can I get the recyclerview to scroll?
Here is the Constraint Layout that includes Recycler View
<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"
android:background="#color/colorPrimaryDark"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:paddingStart="#dimen/_10sdp"
android:paddingTop="#dimen/_10sdp"
android:text="#string/notes"
android:textColor="#color/ColorWhite"
android:textSize="#dimen/_15sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SearchView
android:queryHint="#string/search"
android:theme="#style/ThemeOverlay.search"
android:iconifiedByDefault="false"
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
android:background="#drawable/background"
android:searchIcon="#drawable/ic_search"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv1" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:overScrollMode="never"
app:layout_constraintTop_toBottomOf="#id/search_view" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:orientation="horizontal"
android:padding="#dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tick"
app:tint="#color/ColorWhite" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_10sdp"
android:src="#drawable/ic_baseline_image_24"
app:tint="#color/ColorWhite" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_baseline_insert_link_24"
app:tint="#color/ColorWhite" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabCreateNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_baseline_add_24"
android:backgroundTint="#color/colorAccent"
android:tint="#color/ColorWhite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="#dimen/_20sdp"
android:layout_marginBottom="#dimen/_20sdp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the Recyclerview Adapter
class NotesAdapter() :
RecyclerView.Adapter<NotesAdapter.NotesViewHolder>() {
var listener: OnItemClickListener? = null
var arrList = ArrayList<Notes>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotesViewHolder {
return NotesViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_rv_notes, parent, false)
)
}
override fun getItemCount(): Int {
return arrList.size
}
fun setData(arrNotesList: List<Notes>) {
arrList = arrNotesList as ArrayList<Notes>
}
fun setOnClickListener(listener1: OnItemClickListener) {
listener = listener1
}
override fun onBindViewHolder(holder: NotesViewHolder, position: Int) {
holder.itemView.tvTitle.text = arrList[position].title
holder.itemView.tvDesc.text = arrList[position].noteText
holder.itemView.tvDateTime.text = arrList[position].dateTime
if (arrList[position].color != null){
holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(arrList[position].color))
}else{
holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(R.color.ColorLightBlack.toString()))
}
if (arrList[position].pathImage != null) {
holder.itemView.imgNote.setImageBitmap(BitmapFactory.decodeFile(arrList[position].pathImage))
holder.itemView.imgNote.visibility = View.VISIBLE
} else {
holder.itemView.imgNote.visibility = View.GONE
}
if (arrList[position].webLink != "") {
holder.itemView.tvWebLink.text = arrList[position].webLink
holder.itemView.tvWebLink.visibility = View.VISIBLE
} else {
holder.itemView.tvWebLink.visibility = View.GONE
}
holder.itemView.cardView.setOnClickListener {
listener!!.onClicked(arrList[position].pld!!)
}
}
class NotesViewHolder(view: View) : RecyclerView.ViewHolder(view){
}
interface OnItemClickListener {
fun onClicked(noteId: Int)
}
}```
Try placing the .xml content inside the Drawer layout
<androidx.drawerlayout.widget.DrawerLayout 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="#color/colorPrimaryDark"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:paddingStart="#dimen/_10sdp"
android:paddingTop="#dimen/_10sdp"
android:text="#string/notes"
android:textColor="#color/ColorWhite"
android:textSize="#dimen/_15sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SearchView
android:queryHint="#string/search"
android:theme="#style/ThemeOverlay.search"
android:iconifiedByDefault="false"
android:id="#+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
android:background="#drawable/background"
android:searchIcon="#drawable/ic_search"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv1" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:nestedScrollingEnabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:overScrollMode="never"
app:layout_constraintTop_toBottomOf="#id/search_view" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:orientation="horizontal"
android:padding="#dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tick"
app:tint="#color/ColorWhite" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_10sdp"
android:src="#drawable/ic_baseline_image_24"
app:tint="#color/ColorWhite" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_baseline_insert_link_24"
app:tint="#color/ColorWhite" />
</LinearLayout>
</FrameLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabCreateNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_baseline_add_24"
android:backgroundTint="#color/colorAccent"
android:tint="#color/ColorWhite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="#dimen/_20sdp"
android:layout_marginBottom="#dimen/_20sdp"/>
</androidx.drawerlayout.widget.DrawerLayout>
Actually the problem isn't in the scrolling itself, but that the rest of the RecyclerView items can't be seen as they are behind the l1 LinearLayout.
To fix this you can adjust the ConstraintLayout constraints to attach a RecyclerView bottom constraints to the top of the l1, and make the height as 0dp to match constraints:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="#dimen/_10sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/l1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/search_view" />

AlertDialog with custom view disappears behind Soft Keyboard after entering a long text

I have a simple AlertDialog with this custom View:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_alignParentTop="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a comment"
android:linksClickable="true"
android:background="#android:color/transparent"
android:inputType="textMultiLine"
android:scrollbars="vertical"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:gravity="center"
>
<ImageView
android:id="#+id/imageview_bold"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#drawable/bold_b" />
<ImageView
android:id="#+id/imageview_italic"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#drawable/italic_i" />
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/imageview_underlined"
android:layout_margin="10dp"
android:src="#drawable/underlined_u"/>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/add_link"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_link"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Problem
A long text causes the AlertDialog to hide behind Soft Keyboard.
And the FormattingBar (B I U Link) is also hidden and only visible with a short text.
The AlertDialog should always stay over the Soft Keyboard and if the text is too long, the EditText has to be scrollable, but the FormattingBar and the AlertDialog buttons should always be visible.
How can I do this?
I already tried stuff like
android:fitsSystemWindows="true"
android:windowSoftInputMode="adjustResize"
android:windowSoftInputMode="adjustPan"
but nothing worked.
Using maxLines causes problems with different devices. The number of maxlines varies.
You need to use setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) to your AlertDialog
SAMPLE CODE
Activity
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showAlertDialog(this)
}
private fun showAlertDialog(
context: Context
) {
val dialogBuilder = AlertDialog.Builder(context)
dialogBuilder.setView(R.layout.aa)
val dialog = dialogBuilder.create()
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
dialog.show()
}
}
dialog layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical"
android:scrollbars="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:hint="Enter a comment"
android:inputType="textMultiLine"
android:linksClickable="true"
android:scrollbars="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageview_bold"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
<ImageView
android:id="#+id/imageview_italic"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
<ImageView
android:id="#+id/imageview_underlined"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
<ImageView
android:id="#+id/add_link"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:src="#mipmap/ic_launcher_round" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Your issue is inputType
Remove that line and try this code instead
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a comment"
android:background="#android:color/transparent"
android:maxHeight="90dp"/>
Update :
edittext.setScroller(new Scroller(myContext));
edittext.setMaxLines(5);
edittext.setVerticalScrollBarEnabled(true);
edittext.setMovementMethod(new ScrollingMovementMethod());
By using this method you can get the keyboard dynamic height and you can set the edittext static height % based on the total height of the screen
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int heightDiff = root.getRootView().getHeight()- root.getHeight();
// IF height diff is more then 150, consider keyboard as visible.
}
});
this only works when android:windowSoftInputMode="adjustResize"
Try this solution:
Get the height of the device by adding the following method to your activity class
public int getHeight(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
return height;
}
Set the MaxHeight according to the device height so it won't cause any problem on bigger devices like tablets by adding this block of code to your onCreate() method.
EditText editText = findViewById(R.id.editText); //The EditText in the Layout
editText.setMaxHeight(getHeight()/3); //
The XML is ok but don't forget to add the id to the editText

NullPointerException on defined TextView

I am working on an app using a CardView menu & I am running into some troubles.
The CardView menu presents different tools & among those, there is "location" which uses a third-party library that I used very recently to retrieve the longitude and latitude.
When the user clicks into this card "location", a popup shows up & after pressing the button "update", I set two textviews in that popup to the retrieved longitude and latitude.
However, I seem to run into a NullPointerException when I try:
longitudeTv.setText("Longitude: " + deviceLongitude);
Here are the main parts of my code:
The Menu Class - the launcher activity:
public class Menu extends AppCompatActivity {
GridLayout menuGrid;
SimpleLocation myLocation;
public static double deviceLongitude;
public static double deviceLatitude;
public static Dialog infoPopupDialog;
static TextView messageTv;
public static Dialog locationPopupDialog;
public static TextView longitudeTv;
public static TextView latitudeTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
menuGrid = (GridLayout) findViewById(R.id.menuGrid);
myLocation = new SimpleLocation(this);
infoPopupDialog = new Dialog(this);
messageTv = (TextView) findViewById(R.id.messageTv);
locationPopupDialog = new Dialog(this);
longitudeTv = (TextView) findViewById(R.id.longitudeTv);
latitudeTv = (TextView) findViewById(R.id.latitudeTv);
// if we can't access the location yet
if (!myLocation.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(this);
}
CardView dataCard = (CardView) findViewById(R.id.dataCard);
CardView locationCard = (CardView) findViewById(R.id.locationCard);
CardView timeCard = (CardView) findViewById(R.id.timeCard);
CardView websiteCard = (CardView) findViewById(R.id.websiteCard);
CardView emailCard = (CardView) findViewById(R.id.emailCard);
CardView infoCard = (CardView) findViewById(R.id.infoCard);
infoCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.showInfoPopup();
}
});
dataCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent dataActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dataActivity);
}
});
locationCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final double longit = roundCoordinates(myLocation.getLongitude());
final double latit = roundCoordinates(myLocation.getLatitude());
deviceLongitude = longit;
deviceLatitude = latit;
MainActivity.showLocationPopup();
}
});
timeCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Menu.this, "Next update in 11 mns", Toast.LENGTH_SHORT).show();
}
});
websiteCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openWebsite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://159.203.78.94/rpilog/weatherstation.txt")); //Insert website.
startActivity(openWebsite);
}
});
emailCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Weather Station Data Report");
intent.putExtra(Intent.EXTRA_TEXT, MainActivity.dataFromURL); //Add string variable holding entire data here.
intent.setData(Uri.parse("mailto:RHellstrom#bridgew.edu"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
}
});
}
//function to round to 2 decimal places our GPS coordinates.
public static double roundCoordinates(double coordinate){
String result = String.format("%.2f", coordinate);
double roundedValue = Double.parseDouble(result);
return roundedValue;
}
public void closePopup(View v){ //ONCLICK OF CLOSE ICON
infoPopupDialog.dismiss();
locationPopupDialog.dismiss();
}
public void updateCoordinates(View v){ //ONCLICK BTN "UPDATE"
Menu.latitudeTv.setText("Latitude: " + Menu.deviceLatitude);
Menu.longitudeTv.setText("Longitude: " + Menu.deviceLongitude);
}
}
Menu.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapps.toualbiamine.weathertracker.Menu"
android:background="#drawable/bg"
android:weightSum="10"
android:orientation="vertical"
android:backgroundTintMode="multiply"
android:backgroundTint="#color/background"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weather Tracker"
android:textSize="34sp"
android:textColor="#color/titleColor"
android:layout_centerInParent="true"/>
</RelativeLayout>
<GridLayout
android:id="#+id/menuGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3"
>
<!-- Row 1 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/dataCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/data"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="32dp"
android:text="Data"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/locationCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="29dp"
android:text="Location"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/timeCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/time"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Update Time"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/emailCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/email"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="35dp"
android:text="Email"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Column 1 -->
<android.support.v7.widget.CardView
android:id="#+id/websiteCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/web"
android:layout_gravity="center_horizontal"
android:layout_marginRight="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="30dp"
android:text="Website"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Column 2 -->
<android.support.v7.widget.CardView
android:id="#+id/infoCard"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/info"
android:layout_gravity="center_horizontal"
android:layout_marginRight="17dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="34dp"
android:text="Info"
android:textColor="#color/textColor"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
Popup_location.xml:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:gravity="center">
<ImageView
android:id="#+id/closePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_alignParentRight="true"
android:elevation="5dp"
android:layout_marginTop="7dp"
android:layout_marginRight="7dp"
android:onClick="closePopup"/>
<android.support.v7.widget.CardView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp"
app:cardBackgroundColor="#color/background"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/location_popup"
/>
<TextView
android:id="#+id/longitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Longitude: "
/>
<TextView
android:id="#+id/latitudeTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:textSize="18dp"
android:text="Latitude: "
/>
<Button
android:id="#+id/btn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="UPDATE"
android:textColor="#color/colorWhite"
android:background="#drawable/update_btn_circle"
android:layout_gravity="center_horizontal"
android:onClick="updateCoordinates"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapps.toualbiamine.weathertracker, PID: 24053
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.
onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.
run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$
DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller
.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at
com.myapps.weathertracker.Menu.updateCoordinates(Menu.java:149)
So, the problematics textviews are defined in the popup.xml.
Please, help. I tried, I searched. I can't find anything.
Community, you're my last hope.
PSA: I also have 3 other fragment classes, a viewpager and a a class retrieving data from a URL using the Volley library by Google but they don't seem to coincide with the problem here. Let me know if you need them.
If the views are in your dialog you have to call findViewById on the dialog, as in:
longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);
They can't be found in the menu activity and are null.
You can't call this right away though, it has to be after you have set the layout of the dialog. However, I don't see where you actually set the layout of the dialog to popup_location.xml in the code you posted. Presumably that's in the showLocationPopup method.
The longitudeTv is childview of locationPopupDialog,you need set the layout of the dialog (popup_location.xml). then init with longitudeTv = (TextView) locationPopupDialog.findViewById(R.id.longitudeTv);

Darken/Dim background elements during loading animation - Android

In have a progress bar spinner that appears once the login button is clicked, I would like the elements behind the spinner to dim or darken while the spinner is visible on the screen. My layout file looks like this currently:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="apps.kusasa.biz.MainActivity" >
<EditText
android:id="#+id/EditText01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2"
android:ems="10"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/loginBtn"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/EditText01"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/EditText01"
android:layout_marginBottom="67dp"
android:text="Login" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/EditText01"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/pics_logo" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
</RelativeLayout>
My button click method:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginBtn:
spinner.setVisibility(View.VISIBLE);
// etc
}
}
How do I achieve this? Thanks!
This will be tough when your spinner is in the same layout as the elements that you would like to dim. I would move the spinner to a separately (parent) layout and then apply a background color with an alpha channel to the relativelayout.
For example #99000000
What I did was creating a custom dialog, which has only the progress bar (instead of a complete dialog as view):
class ProgressBarDialog(context: Context) : Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.progress_bar_dialog)
setCancelable(false)
this.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
override fun onBackPressed() {
// do nothing
}
}
The view (progress_bar_dialog.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
That's it. The dimmed background is given (by the Dialog() it extends) and by setting setCancelable(false) and overriding the onBackPressed() method the user can't dismiss it until waiting is over.
You just need to use it in your activity:
var mMyProgressBarDialog = ProgressBarDialog()
and then call mMyProgressBarDialog.show() when you need it to appear and mMyProgressBarDialog.dismiss() when you need it to disappear.
Hope this helps.

Categories

Resources