I referred
https://github.com/vanniktech/Emoji
but iam getting one error.I did everything what he had said
final EmojiPopup emojiPopup = EmojiPopup.Builder.fromRootView(rootView).build(emojiEditText);
(it is below " To open the EmojiPopup execute the code above: " line)
cannot resolve rootView
I'm unable to understand what is rootview here.
thanks a lot in advance
The rootView is the rootView of your layout xml file which will be used for calculating the height of the keyboard. emojiEditText is the EmojiEditText that you declared in your layout xml file.
This will be a View subclass that you instantiate through inflating an xml layout.
The layout he uses in the example is this one which is inflated as follows:
rootView = (ViewGroup) findViewById(R.id.main_activity_root_view);
Extracted from his own code samples. This would be the rootView in your above example.
I had the same problem...
First install the emoji installer in your splash activity or application class - basically it has to be executed before your layout is inflated in onCreate in your activity. Otherwise the layout will not recognize this <com.vanniktech.emoji.EmojiEditText />.
// Init Emoji
EmojiManager.install(new IosEmojiProvider());
You also need a rootview as explained above by jugutier.
In your xml file declare the view widget like this (this is for constraint layout)
<View
android:id="#+id/a_chat_v_keyboard"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintBottom_toTopOf="#+id/view3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
You can use the following in your activity class.
Declaration
private View vKeyboard;
Binding
vKeyboard = findViewById(R.id.a_chat_v_keyboard);
Then in your activity you can get the keyboard to pop up like this
final EmojiPopup emojiPopup = EmojiPopup.Builder.fromRootView(vKeyboard)
.build(etTextMessage);
ivEmoticonToggle.setTag(1);
ivEmoticonToggle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
emojiPopup.toggle();
if (ivEmoticonToggle.getTag().equals(1)) {
ivEmoticonToggle.setImageResource(R.drawable.keyboard);
ivEmoticonToggle.setTag(2);
} else {
ivEmoticonToggle.setImageResource(R.drawable.emoticon);
ivEmoticonToggle.setTag(1);
}
}
});
If you are using EmojiEditText in any Activity other than FirstActivity then install Emoji in the first activity using
EmojiManager.install(new IosEmojiProvider());
or if you are using this in the FirstActivity then Create a new class extended by the Application class and put this line in the onCreate method
EmojiManager.install(new IosEmojiProvider());
and put this line inside the application tag in your manifest file
android:name=".YourClassName"
Related
I am creating an application that uses a navigation drawer. However, the navigation drawer layout or design that I am using is from a different XML file, not on my MainActivity. I have an image in the layout that I am using that I want to apply as SetOnClickListener on my MainActivity. But I have no idea how I can define the image that is in a separate XML file in my MainActivity.
in your case you have 2 different XML file(layout files)
main Activity
navigation drawer
each layout file must have an own java class to get access to view objects.
but if you don't have another java class for navigation drawer, use an LayoutInflater to inflate the XML layout to an view, then you can access an set the Listener to any of views you want
in main activity add:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View rootView = li.inflate(R.layout.my_navigation_drawer_layout,null,false);
note* replace your layout file name with my_navigation_drawer_layout
now you can declare an image view and use findViewById from rootView we create earlier
final ImageView img = (ImageView) rootView.findViewById(R.id.myImageViewName)
now you can set listener to img :
img.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// write your code here!!!
}
});
What you want to do, is easy using <include> tag over the XML of the MainActivity class
<include
android:layout = "YOUR LAYOUT OF THE NAVIGATION"/>
I created a class that extends ActionBarActivity and displays a custom XML. That class is extended by almost all my activities.
I want to access an element of that custom XML from one of my activities. Let's say I want to change the background of item2 when I'm in Activity2.
In my activity's onCreate method, after setContentView, I tried:
View cView = getLayoutInflater().inflate(R.layout.custom_menu, null);
ImageButton rewards_link = (ImageButton) cView.findViewById(R.id.rewards_link);
rewards_link.setVisibility(View.GONE); // For test purpose
Even if the button id seems correct, the changes doesn't apply. Any ideas ?
If you are setting the custom view via getActionBar().setCustomView(R.layout.custom_menu); (or getSupportActionBar() for v21 of AppCompat), then you can access those views by using findViewById() directly as the view is part of your view hierarchy just like views added via setContentView():
ImageButton rewards_link = (ImageButton) findViewById(R.id.rewards_link);
rewards_link.setVisibility(View.GONE); // For test purpose
Use #getCustomView
View view =getSupportActionBar().getCustomView(); ImageButton imageButton =
(ImageButton)view.findViewById(R.id.action_bar_back); imageButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v){
// Your code here ...
}
});
after I asked if I should use XML or a View class for my project you told me, that I should do everything possible in XML and use a class for the rest. You told me, that animating Sprites isn't possible with XML so I wanted to make a View Class. I got the tip to google "LayoutInflater" for this and I did.
There aren't many Informations about inflaters so I visited android's developers database and tried to find out how this works.
As far as I know now, you have to put something into the onCreate method of your main game activity (the setContentView has to be the mainXML).
So now I created a LinearLayout in my mainXML and called it "container" and made this being a ViewGroup called "parent".
Now I have created a global variable "private View view" and wrote this line:
view = LayoutInflater.from(getBaseContext()).inflate(new ViewClass(this),
null);
Thw Problem now is that u can't inflate a class like this and I think I'm doing this whole inflating thing wrong.
Do you have any tips and tricks for me for making it work to have a LinearLayout in my mainXML and being able to make the content from my View Class appear in it?
EDIT:
Got it to work without errors, but nothing happens if I start my game now.
Here is the code pls answer if u have any solutions:
super.onCreate(savedInstanceState);
// inflate mainXML->
View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
// find container->
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
// initialize your custom view->
view = new GameLayout(this);
// add your custom view to container->
container.addView(view);
setContentView(R.layout.activity_game);
And my GameLayout:
public GameLayout(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.BLACK);
}
There are two ways of going about this. I'll show you one of them. Do the following in your onCreate(Bundle) before calling setContentView(...):
// inflate mainXML
View mainView = getLayoutInflater().inflate(R.layout.mainXML, null);
// find container
LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
// initialize your custom view
view = new ViewClass(this);
// add your custom view to container
container.addView(view);
Finally:
setContentView(mainView);
Alternatively, you can place your custom view inside mainXML:
<your.package.name.ViewClass
android:id="#+id/myCustomView"
android:layout_width="match_parent"
android:layout_height="match_parent"
.... />
I have a normal class (not an activity). Inside that class, I have a reference to an activity.
Now I want to access a view (to add a child) contained in the layout xml of that activity.
I don't know the name of the layout file of that activity. I only know the ID of the view, which I want to access (for example: R.id.my_view).
How can I do that?
Regarding the NullPointerException (which you should add to the question), always make sure you've called setContentView() in your Activity before trying to access a View defined in XML. Example usage:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
...
}
Then, somewhere,
ViewGroup group = (ViewGroup) context.findViewById(R.id.group); // In your example, R.id.my_view
The reason you need to have called setContentView() is that before it's called, your View(Group) doesn't exist. Because findViewById() is unable to find something that doesn't exist, it returns null.
As simple as that!
View view = activity.findViewById(R.id.my_view);
In case of the Layout:
LinearLayout layout = (LinearLayout) activity.findViewById(R.id.my_layoutId);
And to add the Views:
layout.addView(view);
You could make your method accept an Activity parameter and then use it to find the view by id.
Ex:
public class MyClass{
public void doSomething(Activity context){
TextView text=(TextView)context.findViewById(R.id.my_textview);
}
}
Then in your activity:
obj.doSomething(YourActivity.this);
I have a main activity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//HOW TO: dynamically add or remove <com.my.custom.MyCustomLayout>
}
}
The content of the above main Activity is:
main.xml
<FrameLayout ...>
<LinearLayout ...>
<com.my.custom.MyCustomLayout
android:id="#+id/custom">
<FrameLayout>
As you see above, I have a custom layout element, which is a Java class extends LinearLayout like following:
public class MyCustomLayout extends LinearLayout{
...
}
In my activity java code, I would like to dynamically add or remove the custom layout element<com.my.custom.MyCustomLayout> in main.xml layout.
How to do it in My activity Java code?
Create a different layout file with your custom view like:
difflayout.xml:
<com.my.custom.MyCustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom">
Take a reference of your container LinearLayout on your code section. Use LayoutInflater and addView() and removeAllViews() of the ViewGroup class(here your LinearLayout)
First consider if you really want to remove the view from the layout or just completely hide it. You need to have a very good reason to go for the latter.
To just hide the view you would do in your activity
findViewById(R.id.custom).setVisibility(View.GONE) // to hide
// or
findViewById(R.id.custom).setVisibility(View.VISIBLE) // to show
If you really want to completely remove the view from the layout, you can
View customView = findViewById(R.id.custom);
ViewGroup parentView = (ViewGroup) customView.getParent();
parentView.removeView(customView)
you must have defined , id for the custom layout. use this id in java code
example : customLayout = (LinearLayout)findViewById(id);
now with customLayout call the layout