How to create message sending method in Tabs?

Asked

Viewed 66 times

0

I’m creating an app, in android, that has tabs. Each tab is a chat room. I took an example of a video about how to create app with Tabs, where the author, in addition to creating the app using a tabs template, he create a tab class for each of the tabs and an xml for each of them as well. In order to send a message, on a given tab, I would have to create the message sending method in the tab class of each of them, however, I am not able to make the proper "link" of the text field (Textview) in this class, because the command findoViewById() does not appear in the tab class!

package com.example.gustavo.vigilantescomunitarios;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabRua extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_rua, container, false);


    }

}
  • Tried to use the class Context?

  • I’m sorry, but what do you mean?

  • Take a look at this topical and see if it helps you.

  • I did not understand, very well what has to be done kkkkk sorry

2 answers

2


You don’t deny using the findviewById because you are not using the root inflated layout, when you extend to a Fragment things are a little different from when you extend to an Activity, you have to access the other layouts through the inflated layout:

public class TabRua extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_rua, container, false);
    Textview textView = rootView.findviewById(R.id.textView); //Apenas um exemplo
    return rootView;
}
  • Now I’m trying to create the method to send the message and put a Toast, but the method has as parameter (View view). I need to get the message typed in the textview

  • Hunmm you can do this transaction in several ways, from a read on the MVP architecture it may help you

  • Where do I find this MVP architecture? In the android documentation?

  • Where do I see it @Matheus?

0

You can use the class Bundle and pass an instance of that class to your Fragmentin the act of his Intent.

Example:

Bundle bundle = new Bundle();
bundle.putString("foo", "bar"); // chave e valor, respectivamente
fragment.setArguments(bundle);

And to get the data in your Fragment

Bundle bundle = this.getArguments();

if (bundle != null) {
    String foo = bundle.getInt("foo", "VALOR_PADRAO");
}

EDIT 1: As for the findViewById(), recommend you take a look at the library Butterknife to deal with view Binding.

  • Guy on the Bundle I know, the question is another.

  • Your question is somewhat explanatory. You want to exchange a message TextView from a tab to a TextView another tab?

  • No, you who did not understand... Each Tab is a chat channel and each Textview sends a message to its respective channel! It is a normal chat!

  • You want to make the call from your TextView ? Try this: TextView textView = (TextView) getView().findViewById(R.id.text_view);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.