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>
Related
Hi guys I can't get my dropdown menu to show up in my action bar.
In the menu.item.xml, the menu bar appears in the menu split view, however upon loading it on the emulator the menu disappears.
I can't find any solutions in the existing forums on StackOverflow.
Menu.item.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/miProfile"
android:icon="#drawable/ic_action_name"
app:showAsAction="ifRoom"
android:title="Profile"
android:iconTint="#color/white"
>
<menu>
<item
android:id="#+id/menuSortNewest"
android:title="Sort by newest" />
<item
android:id="#+id/menuSortRating"
android:title="Sort by rating" />
</menu>
</item>
</menu>
Main activity implementation of the onOptionsItemSelected
override fun onOptionsItemSelected(item: MenuItem): Boolean{
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
val navController = navHostFragment.navController
return when (item.itemId) {
R.id.menuSortNewest -> {
navController.navigate(R.id.action_home2_to_hamster)
true
}
R.id.menuSortRating -> {
navController.navigate(R.id.action_home2_to_dog)
true
}
else -> super.onOptionsItemSelected(item)
}
}
Toolbar in the activitymain.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="90dp"
app:titleTextColor="#android:color/white"
android:background="#color/black"
android:gravity="center"
>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
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)
}
}
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 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.
This is the Android equivalent of this iOS question.
Trying to create a view that contains a MapView at about 20% of the screen (under an ActionBar...) and the rest of the screen is a ScrollView that when scrolling down, overlaps on top of the MapView and hides it. In short like FourSquare's Android app.
Any ideas?
I've made an implementation based on AndroidSlidingUpPanel (many thanks to this project).
Details http://android.amberfog.com/?p=915
Source code with example: https://github.com/dlukashev/AndroidSlidingUpPanel-foursquare-map-demo
LAST UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map_fragment"
android:name="com.myapp.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
<fragment
android:id="#id/list_fragment"
android:name="com.myapp.ListFragment"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
Then I add an invisible header to the list like so:
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.listview, container, false);
SwipeListView listView = (SwipeListView) view.findViewById(R.id.venue_list);
// An invisible view added as a header to the list and clicking it leads to the mapfragment
TextView invisibleView = new TextView(inflater.getContext());
invisibleView.setBackgroundColor(Color.TRANSPARENT);
invisibleView.setHeight(300);
invisibleView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
listener.moveToMapFragment();
}
});
listView.addHeaderView(invisibleView);
This is hardly ideal, but it works. I hope it helps someone..
The easiest solution is to add a margin to your slider content (second parameter of the SlidingUpPanel) and then remove the fading background. All done from the XML.
You can just declare in your xml file a ScrollView that embeds a LinearLayout, that embeds a MapView :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...... Your list and other stuff .......
</LinearLayout>
</LinearLayout>
</ScrollView>
Then you can set the size for each element by specifying the layout_weight attribute
EDIT
IvanDanDo, following our discussion I found this link that may do what you want (not sure though, I didn't try it) : Android: Have an arbitrary View slide under another View like the software keyboard does