I am using bottom navigation view with navigation component. I used nested navhost in this app, the bottom navigation menu is selected but do not show their fragment. I follow some code but it doesn't work.
I tried this code
MainFragment.kt
class MainFragment : BaseFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
val navController = findNavController(fragmentBinding.fragment)
fragmentBinding.bottomNavigationView.setupWithNavController(navController)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
fragment_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainFragment">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="#navigation/bottom_nav_graph"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
bottom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/homeFragment"
android:title="Home"
android:icon="#drawable/ic_baseline_home_24"/>
<item
android:id="#+id/allUserFragment"
android:title="All User"
android:icon="#drawable/ic_baseline_people_24"/>
<item
android:id="#+id/profileFragment"
android:title="Profile"
android:icon="#drawable/ic_baseline_settings_24"/>
bottom_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_nav_graph"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:label="HomeFragment" />
<fragment
android:id="#+id/allUserFragment"
android:name="com.newroz.myapplication.view.AllUserFragment"
android:label="AllUserFragment" />
<fragment
android:id="#+id/profileFragment"
android:name="com.newroz.myapplication.view.ProfileFragment"
android:label="ProfileFragment" />
</navigation>
how can i show clicked fragment?
Thank you
Initialize BottomNavigationView and Navigation controller in your Activity and merge them.
Suppose Your Activity Name "SecondActivity".
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val bottomNaviagtionview: BottomNavigationView = findViewById(R.id.bottomNavigationView)
val navController: NavController = findNavController(R.id.fragment)
bottomNaviagtionview.setupWithNavController(navController)
}
}
Related
I am building an application that has a GridView that contains a dynamic amount of CardViews. I am having issues accessing the TextView elements in the individual items layout from the custom adapter that I am creating.
tvCastleName and tvShieldTime are unresolved references, and the application will not compile.
main.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">
<TextView
android:id="#+id/tvNickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Nickname"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tvNickname"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:numColumns="3"
android:id="#+id/gvCastles" />
items.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvCastleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:fontFamily="sans-serif-medium"
android:text="Castle Name"
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvCastleName"
android:text="15:00"
android:id="#+id/tvShieldTime"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
GridAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class GridAdapter(var nameList: ArrayList<Castle>, var context: Context?) : BaseAdapter() {
override fun getCount(): Int = nameList.size
override fun getItem(position: Int): Castle = nameList[position]
override fun getItemId(position: Int): Long {
TODO("Not yet implemented")
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val castleView = inflater.inflate(R.layout.items, null)
castleView.tvCastleName.text = nameList.name!!
castleView.tvShieldTime.text = "15:00"
return castleView
}
}
You must be getting Unresolved Reference as the method (Kotlin Synthetics) you are using to access view is now deprecated. You can use ViewBinding to access your views.
You've to make a few changes to your getItem method, to make it work with ViewBinding
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
val itemsBinding = ItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
itemsBinding.tvCastleName.text = nameList.name!!
itemsBinding.tvShieldTime.text = "15:00"
return itemsBinding.root
}
I'm trying to build a simple app with GoogleMap API and I have custom tool bar.
The problem: I want to remove the default toolbar from the fragment.
activity .xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.BillboardsMap"
android:orientation="vertical">
<include
android:id="#+id/tool_bar_back_billbo"
layout="#layout/tool_bar_back_billbo"
/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
onCreate function:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBillboardsMapBinding.inflate(layoutInflater)
setContentView(binding.root)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
Is it possible to remove the toolbar with the title "BillboardsMap"?
Thanks in advance.
Try this -
STEP 1 -In style.xml or theme.xml add this custom new style.
<style name="NoToolbar" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
</style>
STEP 2- In manifest.xml add this code for your activity.
<activity
....
android:theme="#style/NoToolbar" //ADD this line for your activity class
> </activity>
This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 1 year ago.
I'm relatively new to android Studio so i was just asking how can one send a variable lets say;
val Name = editName.text.toString() from one Fragment to another so you can use it in the Second Fragment??
I don't have the project its just a question i had.
Firstly i suggest you learn what is fragment if you do not know,and you can add some arguments in passing fragments this is the navigation link.
https://developer.android.com/guide/navigation
and i will explain a bit first do implementations of navigation from this link
https://developer.android.com/guide/navigation/navigation-getting-started
then,create a navigation (documentation will help you) finally you will create arrows between fragments in navigation and put extras into you navigation as arguments without any code it is very easy argument documentation is here:
https://developer.android.com/guide/navigation/navigation-pass-data
val bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)
and finally you can pass your data with these codes
Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
</RelativeLayout>
Step 3 − Create two FragmentActivity and add the codes which are given below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:hint="Enter your message"
android:textColorHint="#android:color/background_dark"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
android:padding="10dp"
android:text="Send Message"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
FirstFragment.kt −
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_one.view.*
class FirstFragment : Fragment() {
private lateinit var communicator: Communicator
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_one, container, false) as ViewGroup
communicator = activity as Communicator
rootView.btnSend.setOnClickListener {
communicator.passData(rootView.editText.text.toString())
}
return rootView
}
}
fragmentTwo.xml −
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/outPutTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#android:color/background_dark"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
FragmentTwo.kt −
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_two.view.*
class FragmentTwo : Fragment() {
var inputText: String? = ""
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_two, container, false)
inputText = arguments?.getString("inputText")
rootView.outPutTextView.text = inputText
return rootView
}
}
Step 5 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.kotlipapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /&g;
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter&g;
</activity>
</application>
</manifest>
I took example following tutorial
Do you want to do so while both Fragment are active on the same activity? or when a new fragment is replacing the previews one? Both are possible but require a different solution.
If both fragment are active at the same time you can create an interface and have all your fragment implement it. Here is an example with events sent from one fragment to the others:
abstract class ActionableFragment(#LayoutRes contentLayoutId: Int) : Fragment() {
init { Fragment(contentLayoutId) }
abstract fun processEvent(event: MyEventObject)
fun sendEvent(event: MyEventObject) {
val fragmentList = parentFragmentManager.fragments as MutableList<ActionableFragment>
for (fragment in fragmentList) {
fragment.processEvent(event)
}
}
Code in the fragment:
class FirstFragment : ActionableFragment(R.layout.fragment_one) {
override fun processEvent(event: MyEventObject) {
Util.log("FirstFragment", "${event.name}")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
// Inflate the layout for this fragment
sendEvent(MyEventObject("This is an event with an Int",1))
return inflater.inflate(R.layout.fragment_one, container, false)
}
}
i have an activity that contains 2 fragments
StepsFragment contains a Recyclerview but the Recyclerview's animation is not working
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="#B3D1DB"
tools:context=".StepsFragment">
<TextView
android:id="#+id/inputEntred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="invisible" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:layoutAnimation="#anim/layou_animation_fall_down"
android:paddingTop="10dp"
android:paddingBottom="30dp" />
</RelativeLayout>
layou-animation-fall-down
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/fall_down"
android:animationOrder="normal"
android:delay="15%">
fall-down
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="6000">
<translate
android:fromYDelta="-20%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toYDelta="0"
android:duration="1000" />
<alpha
android:fromAlpha="0"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toAlpha="3"
android:duration="1000"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="100%"
android:toYScale="100%" android:duration="1000"
/>
</set>
activity layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Sorting_activity">
<RelativeLayout
android:id="#+id/triLayout"
android:layout_width="0dp"
android:layout_height="300dp"
app:layout_constraintBottom_toTopOf="#+id/stepsLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/stepsLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/triLayout"></androidx.coordinatorlayout.widget.CoordinatorLayout>
StepsFragment
public class StepsFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private static TextView inputEntred ;
private static RecyclerView RV ;
private Handler mhandler = new Handler();
public StepsFragment() {
// Required empty public constructor
}
public void setTextView(String textentered){
inputEntred.setText(textentered);
String[] numberList = inputEntred.getText().toString().split(",");
final Integer[] numbers = new Integer[numberList.length];
SelectionSort m1 = new SelectionSort();
ArrayList<String> mystepsList = m1.steps(numbers);// returns the steps numbers
final StepListAdapter adapter = new StepListAdapter() ;
RV.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
RV.setLayoutManager(llm);
adapter.setList(mystepsList);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_steps, container, false);
inputEntred =view.findViewById(R.id.inputEntred);
RV =view.findViewById(R.id.RV);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
// mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}}
Could you try this? I have to remove unnecessary duration in the XML. Thanks
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="#android:anim/decelerate_interpolator"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/decelerate_interpolator"
/>
</set>
I seem to be stuck at the MainActivity.kt file and my app won't debug as it shows the mentioned errors.
This is for a music player app with a Navigation Drawer.
Edit : I have added the layout file after the comment as requested.
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
}
//as someone asked for the layout file as well here it is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_gradient" />
<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#drawable/echo_logo" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/navigation_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
There is no component called fab in your xml layout file. You still have the line of code that access a component called fab from your class. Remove
fab.setOnClickListener { view->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
It should get it to work.