I have a Navigation drawer that has four different fragments. On the Home Fragment Dashboard, I have eight different image buttons that I want to open a new activity I.E. when you click the Image Button TM's I want it to open the TM's activity. I'm using kotlin as well.
I've watched numerous videos and forums on YouTube and google and none have seemed to work for what I'm doing.
// Dashboard.kt is where I have my ImageButtons, where I want each button to open a new activity.
package com.example.militarymainenance
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class Dashboard : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dashboard, container, false)
}
}
// fragment_dashboard.xml is where I have my ImageButtons, where I want each button to open a new 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"
tools:context=".Dashboard">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="32dp"
android:layout_marginRight="20dp">
<ImageView
android:id="#+id/txtdashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/dashboard3"
android:textAlignment="center"
android:textColor="#FFF" />
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="3"
android:columnCount="2"
android:alignmentMode="alignMargins">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/material_on_surface_stroke"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<ImageButton
android:id="#+id/card1"
android:src="#drawable/folder3"
android:scaleType="centerCrop"
android:layout_width="130dp"
android:layout_height="130dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="#color/white"
android:text="#string/tm_s"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardBackgroundColor="#color/material_on_surface_stroke"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<ImageButton
android:id="#+id/card2"
android:src="#drawable/circuit3"
android:scaleType="centerCrop"
android:layout_width="130dp"
android:layout_height="130dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="#color/white"
android:text="#string/schematics"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardBackgroundColor="#color/material_on_surface_stroke"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<ImageButton
android:id="#+id/card3"
android:src="#drawable/parts3"
android:scaleType="centerCrop"
android:layout_width="130dp"
android:layout_height="130dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="#color/white"
android:text="#string/parts_cheat_sheet"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardBackgroundColor="#color/material_on_surface_stroke"
app:cardElevation="6dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<ImageButton
android:id="#+id/card4"
android:src="#drawable/load3"
android:scaleType="centerCrop"
android:layout_width="130dp"
android:layout_height="130dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="#color/white"
android:text="#string/generator_load_wiring"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
// MainActivity.kt which controls my navigation drawer.
package com.example.militarymainenance
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import com.google.android.material.navigation.NavigationView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close)
toggle.isDrawerIndicatorEnabled = true
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
nav_menu.setNavigationItemSelectedListener(this)
setToolbarTitle("Dashboard")
changeFragment(Dashboard())
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
drawerLayout.closeDrawer(GravityCompat.START)
when(item.itemId){
R.id.dashboard -> {
setToolbarTitle("Dashboard")
changeFragment(Dashboard())
}
R.id.equipment_tracker -> {
setToolbarTitle("Equipment Tracker")
changeFragment(Equipment_tracker())
}
R.id.service_tracker -> {
setToolbarTitle("Service Tracker")
changeFragment(Service_tracker())
}
R.id.faults_tracker -> {
setToolbarTitle("Faults Tracker")
changeFragment(Faults_tracker())
}
R.id.share -> {
setToolbarTitle("Service Tracker")
changeFragment(Service_tracker())
}
R.id.rate_us -> {
setToolbarTitle("Faults Tracker")
changeFragment(Faults_tracker())
}
}
return true
}
fun setToolbarTitle(title:String){
supportActionBar?.title = title
}
fun changeFragment(frag:Fragment){
val fragment = supportFragmentManager.beginTransaction()
fragment.replace(R.id.fragment_container,frag).commit()
}
}
// activity_main.xml which controls my navigation bar
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#515050"
tools:context=".MainActivity">
<include layout="#layout/content_main" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
You should create ImageButton view in your xml file, then bind onClickListener to it and call startActivity when onClick triggers.
just an example to understand:
public class TabFragment1 extends Fragment {
ImageButton imageButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_fragment1, container, false);
imageButton = (ImageButton)view.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(getActivity(),NewActivity.class);
}
});
return view;}
Related
I am trying to make a transition like this one that scales a grid layouts cardview child (which is inside a fragment) to another cardview inside the new fragment.
I have followed all the instructions on the android guide and have no progress. I don't know what I am doing wrong or what I have misunderstood.
Here are the relevant pieces of code.
fragment_small_ad_page.xml
<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"
android:orientation="vertical"
android:paddingHorizontal="15.7dp"
tools:context=".SmallAdPage"
android:animateLayoutChanges="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/smallAdRecyclerView"
android:layout_width="match_parent"
android:layout_height="530dp"
android:layout_marginTop="40dp"
android:layout_marginBottom="30dp"
tools:listitem="#layout/recyclerview_ad_image_item"
/>
<fragment
android:id="#+id/appFragment"
android:name="com.example.hubosandroid.AppsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/smallAdRecyclerView"/>
</RelativeLayout>
fragment_apps.xml
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".AppsFragment">
<GridLayout
android:id="#+id/apps_grid_layout"
android:layout_below="#id/smallAdRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:rowCount="8"
android:animateLayoutChanges="true">
<androidx.cardview.widget.CardView
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_rowWeight="2"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/yellow"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_rowWeight="2"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp"></androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="4"
android:layout_rowWeight="4"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/fourth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="4"
android:layout_rowWeight="4"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/fifth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_rowWeight="2"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/sixth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/seventh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_rowWeight="2"
android:layout_columnSpan="3"
android:layout_columnWeight="3"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/eighth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="1"
android:layout_rowWeight="1"
android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/ninth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_rowWeight="2"
android:layout_columnSpan="5"
android:layout_columnWeight="5"
android:layout_margin="5dp"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="10dp">
</androidx.cardview.widget.CardView>
</GridLayout>
</FrameLayout>
fragment_game_selection.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"
android:background="#color/teal_200"
tools:context=".ui.mainActivity.fragments.GameSelectionFragment">
<androidx.cardview.widget.CardView
android:id="#+id/appCard"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</androidx.cardview.widget.CardView>
</FrameLayout>
AppsFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentAppsBinding.inflate(layoutInflater)
val view = binding.root
binding.appsGridLayout.forEach {
ViewCompat.setTransitionName(it as CardView, it.id.toString())
it.setOnClickListener {
parentFragmentManager.commit {
val newScreen = GameSelectionFragment()
setReorderingAllowed(true)
addSharedElement(it as CardView, "scale_transition")
replace(R.id.appFragment, newScreen)
addToBackStack(null)
}
}
}
return view
}
GameSelectionFragment.kt
class GameSelectionFragment : Fragment() {
private lateinit var binding: FragmentGameSelectionBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = TransitionInflater.from(requireContext())
.inflateTransition(R.transition.scale_transition.xml)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
postponeEnterTransition()
binding = FragmentGameSelectionBinding.inflate(layoutInflater)
val view = binding.root
ViewCompat.setTransitionName(binding.appCard, "scale_transition")
startPostponedEnterTransition()
return view
}
}
scale_transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300">
<changeBounds/>
<changeTransform/>
</transitionSet>
Would appreciate any help. Thanks.
I am trying to implement view binding to one of my activity. The issue is I am not able to access the views and it shows unresolved reference.
Now when I am using tools:viewBindingIgnore="true"
then there are no issues with the views and I am able to access them but then I am getting unresolved symbol for the activity class.
activity_single_movie.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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
<TextView
android:id="#+id/text_error"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Connection gone!!"
android:textColor="#color/white"
android:visibility="gone" />
<ScrollView
app:layout_constraintTop_toTopOf= "parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/movie_poster"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/placeholder_image"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp" >
<TextView
android:id="#+id/movie_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="movie"
android:textStyle="bold"
android:textSize="18sp" />
<TextView
android:id="#+id/movie_tagline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub title"
android:textStyle="bold"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="movie info"
android:textStyle="bold"
android:textSize="14sp" />
//Layout for Release Date
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Release Date: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="2019"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Rating
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Rating: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="5/10"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Runtime
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Runtime: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_runtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="2hrs 13mins"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for Budget
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Budget: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_budget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="$550Million"
android:textSize="12sp"
style="bold" />
</LinearLayout>
//Layout for revenue
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="Budget: "
android:textSize="12sp"
style="bold" />
<TextView
android:id="#+id/movie_revenue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="$550Million"
android:textSize="12sp"
style="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
In SingleMovie.kt
package com.example.movieapp.ui.single_movie_details
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.example.movieapp.R
import com.example.movieapp.data.api.POSTER_BASE_URL
import com.example.movieapp.data.api.TheMovieDBClient
import com.example.movieapp.data.api.TheMovieDBInterface
import com.example.movieapp.data.repository.NetworkState
import com.example.movieapp.data.vo.MovieDetails
import com.example.movieapp.databinding.ActivitySingleMovieBinding
import java.text.NumberFormat
import java.util.*
class SingleMovie : AppCompatActivity() {
private lateinit var viewModel: SingleMovieViewModel
private lateinit var movieRepository: MovieDetailsRepository
private lateinit var binding: ActivitySingleMovieBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySingleMovieBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
val movieId: Int = intent.getIntExtra("id", 1)
val apiService: TheMovieDBInterface = TheMovieDBClient.getClient()
movieRepository = MovieDetailsRepository(apiService)
viewModel = getViewModel(movieId)
viewModel.movieDetails.observe(this, Observer {
bindUi(it)
})
viewModel.movieDetailsNetworkState.observe(this, Observer {
binding.progress_bar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
binding.text_error.setVisibility( if(it == NetworkState.ERROR) View.VISIBLE else View.GONE)
})
}
fun bindUi(it: MovieDetails){
binding.movie_title.setText(it.title)
binding.movie_tagline.setText(it.tagline)
binding.movie_release_date.setText(it.releaseDate)
binding.movie_rating.setText(it.voteAverage.toString())
binding.movie_runtime.setText(it.runtime.toString() + "minutes")
val formatCurrency: NumberFormat = NumberFormat.getCurrencyInstance(Locale.US)
binding.movie_budget.setText(formatCurrency.format(it.budget))
binding.movie_revenue.setText(formatCurrency.format(it.revenue))
//for poster of movie
val moviePosterURL: String = POSTER_BASE_URL + it.posterPath
Glide.with(this)
.load(moviePosterURL)
.into(binding.movie_poster)
}
private fun getViewModel(movieId: Int): SingleMovieViewModel {
return ViewModelProviders.of(this, object: ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return SingleMovieViewModel(movieRepository, movieId) as T
}
}) [SingleMovieViewModel::class.java]
}
}
Errors while using tools:viewBindingIgnore="true"
Unresolved reference: ActivitySingleMovieBinding
Errors without using tools:viewBindingIgnore="true"
Unresolved reference: progress_bar
and similar for all the views
View Binding converts underscores to camelCase, the correct syntax is binding.progressBar
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
binding.progressBar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
I am new to programming and want to ask for help because I am stuck on this task.
Clicking on the RecyclerView gets this error.
Below I will attach my code and hope for your help and solutions to the problem. Thank you
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skreep.worko, PID: 24044
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skreep.worko/com.skreep.worko.DetailActivity}: java.lang.NullPointerException: workout_title must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NullPointerException: workout_title must not be null
at com.skreep.worko.DetailActivity.onCreate(DetailActivity.kt:32)
at android.app.Activity.performCreate(Activity.java:7894)
at android.app.Activity.performCreate(Activity.java:7881)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Workout Adapter
package com.skreep.worko.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.skreep.worko.DetailActivity
import com.skreep.worko.R
import com.skreep.worko.databinding.ItemWorkoutBinding
import com.skreep.worko.model.WorkoutData
class WorkoutAdapter(var c:Context,var workoutList:ArrayList<WorkoutData>
):RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>()
{
inner class WorkoutViewHolder(var v:ItemWorkoutBinding): RecyclerView.ViewHolder(v.root){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WorkoutViewHolder {
val inflter = LayoutInflater.from(parent.context)
val v = DataBindingUtil.inflate<ItemWorkoutBinding>(
inflter, R.layout.item_workout,parent,
false)
return WorkoutViewHolder(v)
}
override fun onBindViewHolder(holder: WorkoutViewHolder, position: Int) {
val newList = workoutList[position]
holder.v.isWorkouts = workoutList[position]
holder.v.root.setOnClickListener {
val name = newList.name
val description = newList.description
val fullTime = newList.fullTime
val workoutDesc = newList.workoutDesc
val workoutName = newList.workoutName
val workoutTime = newList.workoutTime
val mIntent = Intent(c,DetailActivity::class.java)
mIntent.putExtra("description",description)
mIntent.putExtra("name",name)
mIntent.putExtra("fullTime",fullTime)
mIntent.putExtra("workoutDesc",workoutDesc)
mIntent.putExtra("workoutName",workoutName)
mIntent.putExtra("workoutTime",workoutTime)
c.startActivity(mIntent)
}
}
override fun getItemCount(): Int {
return workoutList.size
}
}
Detail Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.DatabaseReference
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.item_workout.*
import kotlinx.android.synthetic.main.item_workout_detail.*
class DetailActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val workoutIntent = intent
val name = workoutIntent.getStringExtra("name")
val description = workoutIntent.getStringExtra("description")
val workoutName = workoutIntent.getStringExtra("workoutName")
detail_name.text = name
detail_desc.text = description
workout_title.text = workoutName
}
}
Home Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.*
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity : BaseActivity() {
lateinit var mDataBase:DatabaseReference
private lateinit var workoutList:ArrayList<WorkoutData>
private lateinit var mAdapter:WorkoutAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
workoutList = ArrayList()
mAdapter = WorkoutAdapter(this, workoutList)
recyclerworkoutList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerworkoutList.setHasFixedSize(true)
recyclerworkoutList.adapter = mAdapter
getWorkoutData()
}
private fun getWorkoutData() {
mDataBase = FirebaseDatabase.getInstance().getReference("Workout")
mDataBase.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val workout = userSnapshot.getValue(WorkoutData::class.java)
workoutList.add(workout!!)
}
recyclerworkoutList.adapter = mAdapter
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(this#HomeActivity,
error.message, Toast.LENGTH_SHORT).show()
}
})
}
}
XML Activity Detail
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isData"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
tools:context=".DetailActivity">
<ImageView
android:id="#+id/back_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:padding="20dp"
android:src="#drawable/ic_back">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:padding="20dp"
android:text="#string/app_name"
android:textSize="20sp"></TextView>
<TextView
android:id="#+id/detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="120dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isData.name}"
android:textColor="#color/darkblue"
android:textSize="26sp"></TextView>
<TextView
android:id="#+id/detail_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/detail_name"
android:layout_alignLeft="#+id/detail_name"
android:fontFamily="#font/poppins"
android:text="#{isData.workoutDesc}"
android:textColor="#color/lightgray">
</TextView>
<TextView
android:id="#+id/tv_workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="310dp"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:text="Упражнения"
android:textColor="#color/darkblue"
android:textSize="20sp"></TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/workoutListDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_workout"
android:orientation="vertical"
android:padding="10dp"
tools:itemCount="1"
/>
</RelativeLayout>
</layout>
Xml activity Home
<?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">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkblue"
android:orientation="vertical"
tools:context=".HomeActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_name"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="30sp"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/welcome_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome_description"
android:textColor="#color/desc_color"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="#id/welcome" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerworkoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
android:paddingTop="40dp"
tools:itemCount="1"
tools:listitem="#layout/item_workout" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>
item_workout xml
<?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">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="350dp"
android:layout_margin="10dp"
android:backgroundTint="#color/gray"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/watch_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/ic_watch"
app:tint="#color/darkblue"></ImageView>
<TextView
android:id="#+id/tvfulltime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="26dp"
android:fontFamily="#font/poppins"
android:text="#{isWorkouts.fullTime}"
android:textSize="10sp"></TextView>
<TextView
android:id="#+id/tvtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="27dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkouts.name}"
android:textColor="#color/darkblue"
android:textSize="24sp">
</TextView>
<TextView
android:id="#+id/tvdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtitle"
android:layout_marginStart="10dp"
android:padding="20dp"
android:text="#{isWorkouts.description}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:src="#drawable/ic_next"
>
</ImageView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
item workout detail xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="isWorkout"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:backgroundTint="#color/gray">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/workout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/darkblue"
android:textSize="24sp"></TextView>
<TextView
android:id="#+id/workout_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/workout_title"
android:layout_alignLeft="#+id/workout_title"
android:fontFamily="#font/poppins"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<TextView
android:id="#+id/workout_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/workout_title"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:text="#{isWorkout.workoutTime}"
android:textColor="#color/darkblue"
android:textSize="12sp">
</TextView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</layout>
Check your activity_detail XML file is missing a Textview called workout_title
You have it in a different XML layout file workout_detail xml
I am using ConstraintLayout as a parent and put recyclerview in it to populate the list. Suppose I have to give item count 5 to populate the list and run the code then the list is showing perfectly but the last item of the recyclerview is showing half and not fully visible, and scroll stops at there. I found a solution for this is if am giving height match_parent to the recyclerview then it works fine but then all other view is hidden behind the recyclerview.
If I use another parent view like LinearLayout or RelativeLayout, then recyclerview with height wrap_content works fine and all the list item are fully visible.
I have tried by 2 ways that work for me but I found that wrong programming practice
I give height match_parent to recyclerview and give top_margin to the recylerview so all the other UI item show.
I give padding_bottom to the recyclerview until all the list item position visible
My XML file "activity_per_day_sale.xml" is below.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/btn_back"
android:padding="5dp"
android:onClick="OnClickPerDaySale"
android:background="?attr/selectableItemBackground"
android:src="#drawable/icon_back_white"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:layout_centerInParent="true"
android:id="#+id/home_title_text"
android:textColor="#color/black"
android:visibility="gone"
android:fontFamily="#font/seguisb"
android:textSize="18sp"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP"
android:padding="10dp"
android:layout_centerVertical="true"
android:visibility="gone"
android:id="#+id/skip"
android:textColor="#color/black"
android:fontFamily="#font/seguisb"
android:textSize="18sp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="10dp"
android:text="Per Day Sale Details"
android:textColor="#color/yellow_app_logo_color"
android:fontFamily="#font/segoeuib"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintStart_toStartOf="parent"
android:id="#+id/TV_signupScreenText"
android:layout_marginLeft="45dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rv_perday_sale"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="10dp"
app:layout_constraintTop_toBottomOf="#+id/TV_signupScreenText"/>
</android.support.constraint.ConstraintLayout>
Activity class is "PerDaySaleActivity.kt"
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import com.nq.NQManager.R
import com.nq.NQManager.utils.BaseActivity
import kotlinx.android.synthetic.main.activity_per_day_sale.*
class PerDaySaleActivity:BaseActivity(){
var adapterPerdaySale : AdapterPerdaySale? = null
companion object {
fun start(context: Context) {
val starter = Intent(context, PerDaySaleActivity::class.java)
context.startActivity(starter)
}
}
override fun getID(): Int {
return R.layout.activity_per_day_sale
}
override fun iniView(savedInstanceState: Bundle?) {
initViews()
}
fun OnClickPerDaySale(v: View){
when(v){
btn_back->{
finish()
}
}
}
fun initViews() {
adapterPerdaySale = AdapterPerdaySale( this)
rv_perday_sale.layoutManager = StaggeredGridLayoutManager(1, 1)
rv_perday_sale.adapter = adapterPerdaySale
}
private fun setUpRecyclerView() {
runOnUiThread { adapterPerdaySale!!.notifyDataSetChanged() }
}
My Adapter class "AdapterPerdaySale.kt"
mport android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.nq.NQManager.R
import kotlinx.android.synthetic.main.adapter_perday_sale.view.*
class AdapterPerdaySale(context: Context):
RecyclerView.Adapter<AdapterPerdaySale.MyViewHOlder>() {
private var ctx:Context?=context
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHOlder {
return MyViewHOlder(LayoutInflater.from(ctx).inflate(R.layout.adapter_perday_sale, p0, false))
}
override fun getItemCount(): Int {
return 5
}
override fun onBindViewHolder(holder: MyViewHOlder, position: Int) {
if (position==1){
holder.tv_date.text="26 June, 2019"
}else if(position==2){
holder.tv_date.text="27 June, 2019"
}
}
inner class MyViewHOlder(view: View) : RecyclerView.ViewHolder(view) {
val tv_date=view.tv_date
}
}
My Adapter xml file "adapter_perday_sale.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" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="5dp"
android:background="#color/black">
<TextView
android:id="#+id/tv_date"
android:layout_below="#+id/txt_today"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"
android:text="25 June, 2019"
android:textSize="23sp"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="8dp" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/tv_date">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:text="Orders"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="0dp"
android:text="50"
android:layout_centerHorizontal="true"
android:textColor="#color/order_history_txt_color"
android:fontFamily="#font/segoeui"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:text="Revenue"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:fontFamily="#font/segoeuib"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:layout_marginTop="0dp"
android:text="£550"
android:layout_centerHorizontal="true"
android:textColor="#color/order_history_txt_color"
android:fontFamily="#font/segoeui"/>
</LinearLayout>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#color/gray"
android:id="#+id/view_1"
app:layout_constraintTop_toBottomOf="#+id/linearLayout1"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>
Add this app:layout_constraintBottom_toBottomOf="parent" property to your recyclerview
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="#+id/btn_back"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?attr/selectableItemBackground"
android:onClick="OnClickPerDaySale"
android:padding="5dp"
android:src="#drawable/ic_background" />
<TextView
android:id="#+id/home_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Signup"
android:textColor="#000"
android:textSize="18sp"
android:visibility="gone" />
<TextView
android:id="#+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="SKIP"
android:textColor="#000"
android:textSize="18sp"
android:visibility="gone" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/TV_signupScreenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="10dp"
android:text="Per Day Sale Details"
android:textColor="#AD4E4E"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_perday_sale"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TV_signupScreenText" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have an app with MainActivity that contains a FrameLayout which would be replaced by a fragment based on which item is clicked in the bottom layout. I have a fragment named "bookings" that must appear in the body area when "Bookings" option is clicked. The "bookings" fragment contains a spinner and a button that are placed at the center of screen inside of RelativeLayout. When the fragment's layout is placed directly inside of body of MainActivity, the spinner and button is centered, just the way I want it to be.
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<!-- The FrameLayout goes here -->
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
But when I replace the FrameLayout with the fragment, the spinner disappears and the button moves at the top.
Code in activity_main.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_frame"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Code in fragment_bookings.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
Code of onCreateView of bookings.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_bookings, container, false);
Spinner loc = (Spinner)v.findViewById(R.id.city_spinner);
String[] cities = new String[]{
"Select a City",
"ABC",
"DEF",
"GHI",
"JKL",
"MNO"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this.getActivity(),android.R.layout.simple_spinner_item,cities
);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
loc.setAdapter(spinnerArrayAdapter);
loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
L1 = id;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
return v;
}
Code in MainActivity.java:
package com.example.user.temporary;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements
profile.OnFragmentInteractionListener,
bookings.OnFragmentInteractionListener,
games.OnFragmentInteractionListener {
private Activity myActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myActivity = this;
LinearLayout profilebtn = (LinearLayout)this.findViewById(R.id.profile_button);
LinearLayout bookingsbtn = (LinearLayout)this.findViewById(R.id.bookings_button);
LinearLayout gamesbtn = (LinearLayout)this.findViewById(R.id.games_button);
profilebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
profile profileFragment = new profile();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, profileFragment);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
bookingsbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bookings bookings_frame = new bookings();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, bookings_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
gamesbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
games games_frame = new games();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, games_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
}
#Override
public void onFragmentInteraction(Uri uri){
}
}
Change height of FrameLayout to match_parent from wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_frame"/>
</LinearLayout>
You have match_parent in the height for fragment layout. Try changing it to wrap_content and see if it works