I am trying to go to another activity, but my setting activity is preventing to go there. How do I fix that?
class MainActivity : AppCompatActivity() {
private var gettext: EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
gettext = findViewById(R.id.text2)
// val intent = intent
val action = intent.action
val type = intent.type
val button = findViewById<Button>(R.id.button_submit)
//this part is what I want to go to another activity, while still have setting activity
button.setOnClickListener {
val intent = Intent(this,com.example.testing1.DecryptVerify::class.java)
startActivity(intent)
}
if (Intent.ACTION_SEND == action && type != null) {
handleSendText(intent)
}
}
private fun handleSendText(intent: Intent) {
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
if (sharedText != null) {
// showing the text in edittext
gettext?.setText(sharedText)
}
}
//I want to stay, while when I run it and click button on the top one
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_settings -> {
val intent = Intent(this, com.example.testing1.SettingsActivity::class.java
)
startActivity(intent)
}
}
return super.onOptionsItemSelected(item)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.main_menu, menu)
return super.onCreateOptionsMenu(menu)
}
}
Do I need to some form if-else?
Related
It was working perfectly, I don't know where I messed up...
I have 5 items in my bottom navigation menu and it is always marked/set/highlighted/selected on the home activity (0), whenever I click on let's say ProfileActivity (4), it set it as selected just for a second and then selection goes back to the home activity (0), but the screen remains on the activity which I wanted.
Here is the bottom navigation:
AppBottomNavigation.kt
class AppBottomNavigation(private val bnv: BottomNavigationViewEx,
private val navNumber: Int,
activity: Activity): LifecycleObserver {
#OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
bnv.menu.getItem(navNumber).isChecked = true
}
init {
bnv.setIconSize(29f, 29f)
bnv.setTextVisibility(false)
bnv.enableItemShiftingMode(false)
bnv.enableShiftingMode(false)
bnv.enableAnimation(false)
for (i in 0 until bnv.menu.size()) {
bnv.setIconTintList(i, null)
}
bnv.setOnNavigationItemSelectedListener {
val nextActivity =
when (it.itemId) {
R.id.nav_item_home -> MainActivity::class.java
R.id.nav_item_search -> SearchActivity::class.java
R.id.nav_item_share -> ShareActivity::class.java
R.id.nav_item_likes -> LikesActivity::class.java
R.id.nav_item_profile -> ProfileActivity::class.java
else -> {
Log.e(BaseActivity.TAG, "unknown nav item clicked $it")
null
}
}
if (nextActivity != null) {
val intent = Intent(activity, nextActivity)
intent.flags = Intent.FLAG_ACTIVITY_NO_ANIMATION
activity.startActivity(intent)
activity.overridePendingTransition(0, 0)
true
} else {
false
}
}
}
}
fun BaseActivity.setupBottomNavigation(navNumber: Int){
AppBottomNavigation(bottom_navigation_view, navNumber,this)
}
here is a MainActivity.kt just as an example of how I called bottom navigation in activities:
class MainActivity : BaseActivity(), FeedAdapter.Listener {
private lateinit var mAdapter: FeedAdapter
private val TAG = "MainActivity"
private lateinit var mFirebase: FirebaseHelper
private var mLikesListeners: Map<String, ValueEventListener> = emptyMap()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
Log.d(TAG, "onCreate")
setupBottomNavigation(0)
First create BottomNavigationViewHolder class like
public class BottomNavigationViewHolder {
public static void setupBottomNavigationView(BottomNavigationViewEx bottomNavigationViewEx) {
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
}
public static void enableNavigation(final Context context, BottomNavigationView view) {
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch(menuItem.getItemId()) {
case R.id.activity_0:
Intent intent0 = new Intent(context, PostsActivity.class);
intent0.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent0);
break;
case R.id.activity_1:
Intent intent1 = new Intent(context, GuessHomeActivity.class);
context.startActivity(intent1);
break;
case R.id.activity_2:
Intent intent4 = new Intent(context, SendReceivedActivity.class);
context.startActivity(intent4);
break;
case R.id.activity_3:
Intent intent3 = new Intent(context, ProfileActivity.class);
context.startActivity(intent3);
break;
}
return true;
}
});
}
}
Then setup this navigation every activity
And number your activities like private Static final int ACTIVITY_NUM = 0 in first activity and so on up to last activity
private void setupBottomNavigationView() {
BottomNavigationViewEx bottomNavigationView = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHolder.setupBottomNavigationView(bottomNavigationView);
BottomNavigationViewHolder.enableNavigation(this,bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
I want to switch Fragments by button click inside the fragment itself. I have have created viewpager + tablayout, everything worked until i made an interface to communicate with my activity where i can set different Page for Viewpager. I am pretty new to Kotlin and I do not understand why I am catching NullPointer :(
I probably initialized my button poorly too, but I am not sure
Here is my MainActivity code
class MainActivity : AppCompatActivity()
, MyFragment1.buttonClick {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment1))
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment_2))
tablayout.addTab(tablayout.newTab().setIcon(R.drawable.ic_fragment3))
tablayout.tabGravity = TabLayout.GRAVITY_FILL
val adapter = MyAdapter(this, supportFragmentManager, tablayout.tabCount)
viewpager!!.adapter = adapter
viewpager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tablayout))
val tabStrip = tablayout.getChildAt(0) as LinearLayout
for (i in 0 until tabStrip.childCount) {
tabStrip.getChildAt(i).setOnTouchListener { v, event -> true }
}
}
override fun buttonClicked(view: View) {
viewpager.currentItem = 2
}
fun selectIndex(index: Int) {
viewpager.currentItem = index
}
override fun onBackPressed() {
val currentPos = viewpager.currentItem
if (currentPos != 0) {
viewpager.currentItem = 0
} else {
super.onBackPressed()
}
}
}
**And a code for my Fragment**
class MyFragment1 : Fragment() {
private var click: buttonClick? = null
interface buttonClick {
fun buttonClicked(view: View)
}
var currentPage: Int = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_my_fragment1, container, false)
val btn: Button = view?.findViewById(R.id.btn_next)
btn.setOnClickListener {
Toast.makeText(context, "test", Toast.LENGTH_LONG).show()
click?.buttonClicked(it)
}
return view
}
}
Have you initialized your click variable?
private var click: buttonClick? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
click = context as? buttonClick
}
override fun onDetach() {
super.onDetach()
click = null
}
I have tried to add an setOnClickListener in my Fragment. But both methods i tried didnt work. There is no readction to my button press in the App.
class LoginFragment: Fragment(), View.OnClickListener {
//AuthVariable for global use
private lateinit var myAuth: FirebaseAuth
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var view = inflater.inflate(R.layout.content_login, container, false)
view?.login_button?.setOnClickListener(this)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onClick(p0: View?) {
Log.i("BUTTON123","TEST")
loginUser()
}
}
Activity:
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
//AuthVariable for global use
private lateinit var myAuth : FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
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)
//Initializing FirebaseAuth Instance
myAuth = FirebaseAuth.getInstance()
var user = myAuth.currentUser
if(user != null){
updateUI(user)
}else{
val test = supportFragmentManager
val frag = LoginFragment()
test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()
}
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_addShop -> {
//Activity to Add your Shop
val addshopIntent = Intent(this,AddShopActivity::class.java)
startActivity(addshopIntent)
}
R.id.nav_searchShop -> {
//SearchFragment
val test = supportFragmentManager
val frag = test()
test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()
}
R.id.nav_shopMap -> {
//MapActivity
val mapintent = Intent(this, ShopMap::class.java)
startActivity(mapintent)
}
R.id.nav_manage -> {
}
R.id.nav_share -> {
}
R.id.nav_logout -> {
myAuth.signOut()
updateUI(myAuth.currentUser)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
}
Did you forget the onClick() method in LoginFragment?
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.content_login, container, false)
view.login_button.setOnClickListener {
Log.i("BUTTON123","TEST")
loginUser()
}
return view
}
I need to open a context menu on MenuItem click. How can i do this? I've tried to do this on BottomNavigationView.OnNavigationItemSelectedListener:
registerForContextMenu(menuItem.actionView)
openContextMenu(menuItem.actionView)
but the actionView in null.
Thanks a lot.
Main Activity:
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
navigation.selectedItemId = R.id.navigation_dashboard
}
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
...
R.id.navigation_add -> {
registerForContextMenu(menuItem.actionView)
openContextMenu(item.actionView)
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
navigation.selectedItemId = R.id.navigation_dashboard
}
override fun onCreateContextMenu(menu: ContextMenu, v: View,
menuInfo: ContextMenuInfo) {
...
}
override fun onContextItemSelected(item: MenuItem): Boolean {
...
}
i'm stuck in my application because i have to do a fragment with the navigation drawer in kotlin.. can anyone help? I have search a lot of things in web but i didn't find anything yet...
Down here is two items that i have do in main activity and i want that is present in the fragment too.
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.home -> {
}
R.id.subjects -> {
val intent = Intent(this, SubjectsActivity::class.java)
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
You can use this to replace your frame with HomeFragment()
mainDisplayFrame should be replace by your FrameID
var fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.mainDisplayFrame,HomeFragment()).commit()
mDrawerLayout!!.closeDrawers()
You can download the source link from here(Navigation Drawer In Android Kotlin)
MainActivity.kt:
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init() {
tv_home.setText("Home Tab")
tv_title.setText("Home")
ll_about.setOnClickListener(this)
ll_home.setOnClickListener(this)
ll_bookmark.setOnClickListener(this)
ll_help.setOnClickListener(this)
rl_menu.setOnClickListener(this)
rl_header.setOnClickListener(this)
tv_link.setLinkTextColor(Color.parseColor("#000000"));
Linkify.addLinks(tv_link, Linkify.ALL)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.ll_home -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Home Tab")
tv_title.setText("Home")
}
R.id.ll_about -> {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://deepshikhapuri.blogspot.in/"))
startActivity(browserIntent)
}
R.id.ll_help -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Help Tab")
tv_title.setText("Help")
}
R.id.ll_bookmark -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Bookmark Tab")
tv_title.setText("Bookmark")
}
R.id.rl_header -> {
}
R.id.rl_menu -> {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
drawer_layout.openDrawer(GravityCompat.START)
}
}
}
}
Thanks!
Is something like this? #NongthonbamTonthoi
class NavViewFragment : AppCompatActivity(),
NavigationView.OnNavigationItemSelectedListener {
fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int){
supportFragmentManager.inTransaction { add(frameId, fragment) }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_view_fragment)
setSupportActionBar(toolbar)
nav_view.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.nav_view, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.home -> {
}
R.id.subjects -> {
val intent = Intent(this, SubjectsActivity::class.java)
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
val fragmentTransaction = beginTransaction()
fragmentTransaction.func()
fragmentTransaction.commit()
}
}