when I am trying to click on an element on the reycler view it creates a new intent in the fragment and calls a different Activity but when it starts the new activity from the intent it just doesnt display anything or the breakpoint at SecondaryActivity shows up
this code is running from a fragment on the main activity and the new activity isnt displaying any layout
fun loadImages(){
recyclerView!!.setHasFixedSize(true)
recyclerView!!.layoutManager = GridLayoutManager(this.activity,4)
try {
this.imageList = ImageGallery.GetImagesList(this.context as Context )
}
catch (ex:Exception)
{
println(ex.message)
}
try { galleryAdapter = GalleryAdapter(this.context as Context,imageList,
object : IPhotoInterface {
override fun onPhotoClick(stringPath: String):Unit {
//process picture on click
imageIntent = android.content.Intent(context,SecondaryActivity::class.java)
imageIntent.putExtra("image_file",File(stringPath))
try{
startActivity(imageIntent)//here throws and exception
}catch(except:Exception)
{
println(except.message)
}
}
})
recyclerView?.adapter = galleryAdapter
galleryNumberText?.text = "${imageList.size} images"
}
catch(ex:Exception)
{
println(ex.message)
}
}
secondary_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="#layout/secondary_action_bar"></include>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gearicon"
app:flow_verticalAlign="center"
tools:ignore="MissingConstraints">
</ImageView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="407dp"
android:id="#+id/bottomNavBar"
android:layout_height="57dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="1.0"
android:background="#color/black"
app:menu="#menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
SecondaryActivity
kt
class SecondaryActivity: AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
val i = intent
val myParcelableObject: File? =
i.getParcelableExtra<Parcelable>("image_file") as File?
}
}
Related
FolderActivity.java
public class FolderActivity extends AppCompatActivity {
private MyListAdapter myListAdapter;
private HashMap<String, List<VideoModel>> videodata;
private HashMap<String,List<String>> latestvideo=new HashMap<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
RecyclerView folderrecycleview=findViewById(R.id.folderrecyclerview);
final GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
folderrecycleview.setLayoutManager(layoutManager);
myListAdapter=new MyListAdapter(null);
folderrecycleview.setAdapter(myListAdapter);
}
Fragment.kt
class VideoPlayer : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val intent = Intent(activity, FolderActivity::class.java)
startActivity(intent)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_video_player_activity, container, false)
}
}
fragmentvideoplayeractivity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".navigation.fragments.LocalVideoPlayerMain">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#color/blue"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mytitle"
android:textSize="18sp"
android:text="#string/app_name"
android:fontFamily="#font/lato_regular"
>
</TextView>
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/folderrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="display the background theme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/recentplayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="24dp"
android:layout_marginBottom="32dp"
android:clickable="true"
android:text="#string/play_recent"
android:src="#drawable/ic_play_btn"
android:focusable="true"
app:backgroundTint="#color/blue"
android:contentDescription="TODO" />
</RelativeLayout>
</FrameLayout>
MainActivity.kt this is the bottom navigation bar and were the app source starts first
class MainActivity : AppCompatActivity() {
private var doubleBackToExitPressedOnce = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.mainactivity)
val LocalVideoPlayerMain =LocalVideoPlayerMain()
val example1 = example2()
val example2=example3()
val example3=example4()
makeCurrentFragment(VideoPlayer)
bottomNavigationView.setOnNavigationItemSelectedListener{
when (it.itemId){
R.id.example1 -> makeCurrentFragment(VideoPlayer)
R.id.example2 -> makeCurrentFragment(example2)
R.id.example3 -> makeCurrentFragment(example3)
R.id.example4 -> makeCurrentFragment(example4)
}
true
}
}
private fun makeCurrentFragment(fragment: Fragment)=
supportFragmentManager.beginTransaction().apply{
replace(R.id.fl_wapper,fragment)
commit()
}
}
can any one please tell me how to make folderActivity work on fragment layout.
when i click the video player button on navigation bar, Folderactivity.java(it has same setcontentView(fragmentvideoplayeractivity.xml as fragment.kt) it just popup above the fragment layout and when i click back button it show the fragment.kt layout(which have a bottom navigation bar).the navigation bar also overlay by folderactivity.java
i need to show that overlayed recyclerview with the navigation bar
Overlayed Folder activity above the fragment(it automatically popup the activity it didn't open it on same navigation fragment layout)
i need to make this recycler into one complete activity like given below
if you know any solution please tell me
I have a RecyclerView with a list of names being pulled from a firebase realtime database, as well with phone numbers, emails and addresses. I have created a layout for the cards shown in RecyclerView. I want them to only display the name and image until clicked. Once clicked they expand and show the phone number, email and address. I have done all this but the final problem I am facing is with setVisibility for the RelativeLayout inside the layout.xml for RecyclerView, since it's a different xml from the one that belongs to the activity my app keeps crashing. I have tried multiple solutions for problems similar to mine but I could never find someone with the same problem that I was facing.
Now if I use this code:
mUserInfoLayout = (RelativeLayout) findViewById(R.id.user_additional_info_layout);
mUserInfoLayout.setVisibility(View.INVISIBLE);
I get this error
Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
However If I use the code provided below nothing happens the Layout stays visible.
My MainActivity.java OnCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
//Firebase
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mUserDatabase.keepSynced(true);
//mUserSingleMainLayout = (RelativeLayout) findViewById(R.id.user_single_main_layout);
mUsersList = (RecyclerView) findViewById(R.id.users_list);
mUsersList.setHasFixedSize(true);
mUsersList.setLayoutManager(new LinearLayoutManager(this));
View usersingleView = getLayoutInflater().inflate(R.layout.users_single_layout, null);
mUserInfoLayout = (RelativeLayout) usersingleView.findViewById(R.id.user_additional_info_layout);
mUserInfoLayout.setVisibility(View.INVISIBLE);
//Buttons
mUserAdd = (FloatingActionButton) findViewById(R.id.user_add_btn);
mUserAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent createuserIntent = new Intent(MainActivity.this, CreateUserActivity.class);
startActivity(createuserIntent);
}
});
}
My activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/user_add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
app:fabSize="normal"
app:rippleColor="#color/colorPrimary"
android:src="#drawable/ic_add_white_24dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/users_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"/>
</RelativeLayout>
My user_single_layout.xml the layout file used in RecyclerView
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_single_image"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:src="#drawable/account_circle" />
<TextView
android:id="#+id/user_single_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="100dp"
android:layout_marginTop="20dp"
android:text="#string/display_name_users"
android:textColor="#color/colorBlack"
android:textSize="18sp" />
<RelativeLayout
android:id="#+id/user_additional_info_layout"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/user_single_name"
android:layout_marginTop="10dp"
android:layout_alignParentStart="true"
android:layout_marginStart="100dp">
<ImageView
android:id="#+id/user_phone_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/user_single_phone"
android:src="#drawable/ic_phone_black_24dp"/>
<ImageView
android:id="#+id/user_email_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/user_single_email"
android:layout_below="#+id/user_phone_image"
android:src="#drawable/ic_email_black_24dp"/>
<ImageView
android:id="#+id/user_address_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/user_single_address"
android:src="#drawable/ic_map_marker_black_24dp" />
<TextView
android:id="#+id/user_single_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/user_phone_image"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:text="Phone" />
<TextView
android:id="#+id/user_single_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_below="#+id/user_single_phone"
android:layout_toRightOf="#+id/user_email_image"
android:layout_marginTop="10dp"
android:text="Email" />
<TextView
android:id="#+id/user_single_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/user_single_email"
android:layout_toRightOf="#id/user_address_image"
android:text="Address" />
</RelativeLayout>
</RelativeLayout>
In the user_single_layout.xml I want to make the "#+id/user_additional_info_layout" RelativeLayout invisible programatically inside MainActivity.java
Edit
My answer is focused on the Fragment lifecycle. The Activity lifecycle is different and does not contain the method I suggested.
The problem is the view that you are trying to access is null. The view is not yet inflated in the onCreate method and is therefore not yet available.
No view related logic should be executed within onCreate().
You need to use the onViewCreated() method to access your views which has the following signature:
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
}
Therefore I suggest you move all of your view related logic into this method.
First, move your code to recyclerView adapter.
Second, make relativeLayout visibility to gone and change it to linearLayout.
Third, use this code for expanding it.
private fun animateView(
view: View,
duration: Long,
initialHeight: Int,
targetHeight: Int,
onAnimationEnd: () -> Unit = {}
) {
view.apply {
val animator = ValueAnimator.ofInt(initialHeight, targetHeight)
animator.addUpdateListener {
layoutParams.height = it.animatedValue as Int
requestLayout()
if (it.animatedValue as Int > 2) {
//Make the view visible in java
//yourview.setVisibility(View.Visible)
show()
}
}
animator.addListener(object : Animator.AnimatorListener {
override fun onAnimationEnd(animation: Animator) {
layoutParams.height = targetHeight
onAnimationEnd.invoke()
}
override fun onAnimationStart(animation: Animator) {
}
override fun onAnimationCancel(animation: Animator) {}
override fun onAnimationRepeat(animation: Animator) {}
})
animator.interpolator = AccelerateDecelerateInterpolator()
animator.duration = duration
animator.start()
}
}
fun View.expandAnimation(duration: Long) {
measure(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
val initialHeight = 0
val targetHeight = measuredHeight
layoutParams.height = 0
animateView(this, duration, initialHeight, targetHeight)
}
fun View.collapseAnimation(duration: Long) {
val initialHeight = measuredHeight
val targetHeight = 0
animateView(this, duration, initialHeight, targetHeight) {
hide()
}
}
fun View.hide() {
this.apply {
visibility = View.GONE
}
}
fun View.show() {
this.apply {
visibility = View.VISIBLE
}
}
You can easily convert this code to java
sry I don't have time to convert it myself
The RecyclerView calling inside Alert Dialog, I have tried setting width MATCH_PARENT at run time in onCreateView in adapter class.
if (binding.root.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
binding.root.getLayoutParams ().width = parent.getWidth ()
Tried calling inside in onCreateView but leaving space vertically. I tried all possible combination which is present on SO but nothing is working.
private fun initDialog() {
if (mAlertDialog == null) {
val mBuilder = AlertDialog.Builder(this)
val view1 = layoutInflater.inflate(R.layout.select_itinerary_dialog, null, false)
val mRecyclerView = view1.findViewById<RecyclerView>(R.id.mRecyclerView)
mRecyclerView?.adapter = SelectItineraryAdapter(this#AddItineraryActivity)
val llm = LinearLayoutManager(this#AddItineraryActivity)
// llm.isAutoMeasureEnabled = false
mRecyclerView?.layoutManager = llm
//mRecyclerView?.layoutManager = LinearLayoutManager(this#AddItineraryActivity)
mCheckBoxSelectAll = view1.findViewById<CheckBox>(R.id.checkSelctAll)
mCheckBoxSelectAll?.setOnCheckedChangeListener { _, b ->
if (!fromBroadcastReceiver) {
for (item in mItinerarylistResponse?.data?.itinerary!!) {
item?.isSelected = b
}
mRecyclerView?.adapter!!.notifyDataSetChanged()
}
fromBroadcastReceiver = false
}
view1.findViewById<AppCompatButton>(R.id.clearItinerary).setOnClickListener {
mAlertDialog!!.dismiss()
}
view1.findViewById<AppCompatButton>(R.id.doneItinerary).setOnClickListener {
try {
for (item in mItinerarylistResponse?.data?.itinerary!!) {
viewContainer.visibility = View.VISIBLE
if (item!!.item == "Wake Up") {
if (item.isSelected) {
wakeUpTimeTV.visibility = View.VISIBLE
wakeUpTimeLay.visibility = View.VISIBLE
} else {
wakeUpTimeTV.visibility = View.GONE
wakeUpTimeLay.visibility = View.GONE }
}
}
} catch (e: Exception) {
}
mAlertDialog!!.dismiss()
}
mBuilder.setView(view1)
mAlertDialog = mBuilder.create()
mAlertDialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
// mAlertDialog?.setView(view1)
}
}
Adapter
class SelectItineraryAdapter(val contecx: AddItineraryActivity) : RecyclerView.Adapter<SelectItineraryAdapter.MyHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val binding = DataBindingUtil.inflate<RowItemItinararyBinding>(LayoutInflater.from(contecx), R.layout.row_item_itinarary, parent, false)
// val v= LayoutInflater.from(contecx).inflate(LayoutInflater.from(contecx), R.layout.row_item_itinarary, parent, false)
/* if (binding.root.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
binding.root.getLayoutParams ().width = parent.getWidth ()*/
val dd = MyHolder(binding)
return dd
}
override fun getItemCount(): Int {
return mItinerarylistResponse?.data?.itinerary!!.size
}
#SuppressLint("ClickableViewAccessibility")
override fun onBindViewHolder(holder: MyHolder, position: Int) {
holder.v.itinararyname.text = mItinerarylistResponse?.data?.itinerary!![position]?.item
holder.v.markReadCheck.setOnCheckedChangeListener { _, isChecked ->
mItinerarylistResponse?.data?.itinerary!![position]!!.isSelected = isChecked
val receiverIntentDetail = Intent()
receiverIntentDetail.action = isitinerarySelected
holder.v.markReadCheck.context.sendBroadcast(receiverIntentDetail)
}
holder.v.detailLat.setOnTouchListener { view, motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
com.socrpro.utils.error("ACTION_DOWN")
}
MotionEvent.ACTION_UP -> {
// if (itinerary[position].readStatus == 0) {
holder.v.markReadCheck.performClick()
// }
com.socrpro.utils.error("ACTION_UP")
}
MotionEvent.ACTION_MOVE -> {
com.socrpro.utils.error("ACTION_MOVE")
}
}
true
}
holder.v.markReadCheck.isChecked = mItinerarylistResponse?.data?.itinerary!![position]!!.isSelected
}
class MyHolder(val v: RowItemItinararyBinding) : RecyclerView.ViewHolder(v.root)
}
Row_item
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="#+id/detailLat" android:layout_width="match_parent" android:layout_height="wrap_content"
android:clickable="true" android:orientation="vertical">
<LinearLayout android:id="#+id/readornot" android:layout_width="match_parent"
android:layout_height="#dimen/_25sdp" android:background="#color/white"
android:gravity="center_vertical" android:orientation="horizontal"
android:paddingTop="#dimen/_5sdp" android:paddingBottom="#dimen/_5sdp">
<CheckBox android:id="#+id/markReadCheck" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:button="#drawable/custom_checkbox" android:layoutDirection="rtl"
android:text="" />
<TextView android:id="#+id/itinararyname" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/_2sdp" android:layout_marginEnd="#dimen/_5sdp"
android:layout_weight="1" android:singleLine="true"
android:text="dfgdgfdggfdg" android:textColor="#color/colorPrimary"
android:textSize="#dimen/_10sdp" />
</LinearLayout>
<View android:layout_width="match_parent" android:layout_height=".5dp"
android:layout_alignBottom="#+id/readornot" android:background="#color/linecolorcc" />
</RelativeLayout>
</layout>
Updated.. new option added..*
TL;DR
Replace your vertical LinearLayout (which surrounds the RecyclerView) with a RelativeLayout
Option1 (old answer)
Before surrounding the layout with ScrollView
After adding ScrollView as root
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/search"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search"
android:orientation="vertical">
<!--Header-->
<LinearLayout
android:id="#+id/static_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/HeaderStyle"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</layout>
The downside of adding ScrollView is that it causes all the surrounded Views to be Scrolled! So try the next option..
Option2 (better one)
Actually, I was not satisfied with Option1 because the header of the table should not disappear when user scrolls down the RecyclerView! So i've tried to replaced the LinearLayout (which surrounds the RecyclerView) with a RelativeLayout and it get the job done..
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/search"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search">
<!--Header-->
<LinearLayout
android:id="#+id/static_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#id/rv"
android:layout_alignEnd="#id/rv"
style="#style/HeaderStyle"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/static_header"/>
</RelativeLayout>
</RelativeLayout>
</layout>
My layout_xml code:
<?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="wrap_content"
android:minHeight="#dimen/_350sdp"
android:maxHeight="#dimen/_500sdp"
android:orientation="vertical"
android:id="#+id/commentProblemScreenLL">
<ListView
android:id="#+id/commentProblemLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/commentProblemNotFound"
android:visibility="gone"
android:id="#+id/commentProblemNotFoundTV"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_40sdp"
android:background="#drawable/side_nav_bar"
android:orientation="horizontal"
android:padding="#dimen/m1dp"
android:weightSum="1"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/commentEditTextET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:background="#null"
android:hint="#string/comment_EditText"
android:padding="5dip"
android:textColor="#color/white"
android:textColorHint="#color/hintcolor" />
<ImageButton
android:id="#+id/commentSendTextIB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:layout_alignParentEnd="true"
android:background="#drawable/side_nav_bar"
android:paddingBottom="#dimen/_4sdp"
android:src="#drawable/cm_chat_send"
android:layout_alignParentRight="true"
android:paddingRight="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"/>
</RelativeLayout>
</RelativeLayout>
Java code:-
private void showCommentDailog()
{
Log.d(TAG,"showCommentDailog enter()");
commentDialog = new BottomSheetDialog(getContext());
View view = ((ProblemsDetailActivity) getActivity()).getLayoutInflater().inflate(R.layout.commentproblemscreen, null);
commentDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
commentProblemLV = (ListView)view.findViewById(R.id.commentProblemLV);
TextView commentProblemNotFoundTV = (TextView)view.findViewById(R.id.commentProblemNotFoundTV);
EditText commentEditTextET = (EditText) view.findViewById(R.id.commentEditTextET);
ImageButton commentSendTextIB = (ImageButton)view.findViewById(R.id.commentSendTextIB);
if(commentDialog.getWindow() != null) {
commentDialog.getWindow().setGravity(Gravity.BOTTOM);
}
//call the function to show list of previous work
commentDialog.setContentView(view);
commentDialog.setCanceledOnTouchOutside(true);
commentDialog.show();
final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
commentDialog.dismiss();
}else{
if (newState != BottomSheetBehavior.STATE_EXPANDED) {
if(commentProblemLV != null && !listIsAtTop()){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
}
This code does not show the full layout in bottomsheetDailog. Please correct my code as it shows the full layout in bottomsheetDialog.
I want to open full layout in half screen with BottomSheetDialog.
here I should be given my java code also for bottomSheetDialog.
bottomsheetdailog will apear on clickof comment ImageView.
I have created 2 XML files where both of which have completely different layouts. Whenever I deploy my app to a phone it loads successfully. However when I do the same thing with a tablet, it doesn't work and therefore crashes. I know that it has something to do with a lack of ListView in the sw600dp layout file but what must be done to make sure that sw600dp devices don't keep checking for the ListView?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
sw600dp/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:stretchColumns="*"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TableRow
android:id="#+id/MainActivity_tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/MainActivity_btn0"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_circle"
android:onClick="btncircle_click"
android:text="#string/circle"
/>
<Button
android:id="#+id/MainActivity_btn1"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_star"
android:onClick="btnstar_click"
android:text="#string/star"
/>
</TableRow>
<TableRow
android:id="#+id/MainActivity_tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/MainActivity_btn2"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_square"
android:onClick="btnsquare_click"
android:text="#string/square"
/>
<Button
android:id="#+id/MainActivity_btn3"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_triangle"
android:onClick="btntriangle_click"
android:text="#string/triangle"
/>
</TableRow>
<TableRow
android:id="#+id/MainActivity_tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/MainActivity_btn4"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_heart"
android:onClick="btnheart_click"
android:text="#string/heart"
/>
<Button
android:id="#+id/MainActivity_btn5"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_crescent"
android:onClick="btncrescent_click"
android:text="#string/crescent"
/>
</TableRow>
</TableLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// array of images for ListView
int[] listviewImage = new int[]{
R.drawable.ic_circle,
R.drawable.ic_star,
R.drawable.ic_square,
R.drawable.ic_triangle,
R.drawable.ic_heart,
R.drawable.ic_crescent
};
// array of strings for ListView
String[] listviewTitle = new String[]{
getResources().getString(R.string.circle),
getResources().getString(R.string.star),
getResources().getString(R.string.square),
getResources().getString(R.string.triangle),
getResources().getString(R.string.heart),
getResources().getString(R.string.crescent)
};
List<HashMap<String, String>> aList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
HashMap<String, String> hm = new HashMap<>();
hm.put("listview_image", Integer.toString(listviewImage[i]));
hm.put("listview_title", listviewTitle[i]);
aList.add(hm);
}
String[] from = {"listview_image", "listview_title"};
int[] to = {R.id.mainitem_img, R.id.mainitem_title};
SimpleAdapter simpleAdapter = new SimpleAdapter(this.getBaseContext(), aList, R.layout.listitem_main, from, to);
ListView list_main = this.findViewById(R.id.listview_main);
list_main.setAdapter(simpleAdapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
//blah, blah, blah
}
if (position == 1) {
//blah, blah, blah
}
if (position == 2) {
//blah, blah, blah
}
if (position == 3) {
//blah, blah, blah
}
if (position == 4) {
//blah, blah, blah
}
if (position == 5) {
//blah, blah, blah
}
}
});
}
public void btncircle_click(View view) {
}
public void btnstar_click(View view) {
}
public void btnsquare_click(View view) {
}
public void btntriangle_click(View view) {
}
public void btnheart_click(View view) {
}
public void btncrescent_click(View view) {
}
}
Your problem is that you are trying to load your listview_main even though in a tablet where that View does not exist, so you are getting a NULL value.
This line is giving you a null value in a tablet:
ListView list_main = this.findViewById(R.id.listview_main);
Because that View is not defined in your tablet layout.
In order to prevent this, you need to control whether you are in a tablet or in a phone. I solved this, creating a resource file inside the res folder, and other in your sw600dp one.
In the main one you will have this:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<bool name="is_tablet">false</bool>
</resources>
And in the sw600dp one:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<bool name="is_tablet">true</bool>
</resources>
Then in your code you need to check whether you are in a tablet or not by doing this:
if (getResources().getBoolean(R.bool.is_tablet))
There you will know what to do according to the loaded layout.
Another option is to have 2 different activities, create a launcher activity that will check your isTablet value and then load your MainActivityPhone or MainActivityTablet.
Hope it helps