I implemented the ankushsachdeva emojicon project to display emojicons in my chat app. When i click on a specific chat i start my ChatActivity. If i then immediately click the emoji-imageButton which i made, without expanding the keyboard first, it looks like the left screenshot here
Afterwords, the emojicon-overlay is always displayed correctly. (right screenshot)
I want the overlay to always be like in the right screenshot. Any ideas? (thanks in advance)
ChatActivity:
private ListView listView; //contains the chatmessages and has a customAdapter
private EmojiconsPopup popUp; //emojicon-popUp
private EditText editText; //editText to capture text and emojicons
private InputMethodManager inputManager;
#Override
protected void onCreate(Bundle savedInstanceState){
listView = (ListView) findViewById(R.id.listView);
//...//
inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
popUp = new EmojiconsPopup(listView, getApplicationContext());
popUp.setSizeForSoftKeyboard();
popUp.setOnEmojiconClickedListener(new OnEmojiconClickedListener(){
#Override
public void onEmojiconClicked(Emojicon emojicon){
editText.append(emojicon.getEmoji());
}
});
popUp.setOnEmojiconBackspaceClickedListener(new OnEmojiconBackspaceClickedListener(){
#Override
public void onEmojiconBackspaceClicked(View v){
KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
editText.dispatchKeyEvent(event);
}
});
popUp.setOnSoftKeyboardOpenCloseListener(new OnSoftKeyboardOpenCloseListener(){
#Override
public void onKeyboardOpen(int keyBoardHeight){
}
#Override
public void onKeyboardClose(){
if (popUp.isShowing())
popUp.dismiss();
}
});
}
//called when the emojicon button is clicked
public void onEmojiButtonClicked(View view){
if (!popUp.isShowing()){
inputManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
popUp.showAtBottom(); //show popUp with emojicons
}else if(popUp.isShowing()){
popUp.dismiss(); // hide popUp with emojicons
}
}
Since, there is no reliable way to know the soft keyboard height, the library calculates it by opening the keyboard and seeing how much the top most layout of the view heirarchy shrinks.
I have added a new function into the library showAtBottomPending() which should solve your problem.
Make the following two changes to your code.
Change your onEmojiButtonClicked function to
public void onEmojiButtonClicked(View view){
if (!popUp.isShowing()){
popUp.showAtBottomPending(); //show popUp with emojicons after the keyboard is visible
inputManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}else if(popUp.isShowing()){
popUp.dismiss(); // hide popUp with emojicons
}
}
Instead of passing listView in the EmojiconsPopup constructor,
pass the top most layout of your view hierarchy.
use like this its working.
public void onEmojiButtonClicked(View view){
if (!popUp.isShowing()){
popUp.showAtBottomPending(); //show popUp with emojicons after the keyboard is visible
showKeyboard(ettext);
}else if(popUp.isShowing()){
popUp.dismiss(); // hide popUp with emojicons
}
}
public void showKeyboard(final EmojiconEditText ettext){
ettext.requestFocus();
ettext.postDelayed(new Runnable(){
#Override public void run(){
InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext,0);
}
}
,200);
}
Related
I am creating a word game application , I have created a grid view consisting of buttons. When I click a button in the grid view a popup windows opens which contains all the English alphabets.Now when I click any letter in my pop up window , I want that letter to appear in my grid that is the string in my pop window's button must appear in my grid view's button. , how do I do it?
This is my button's code:
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View container = layoutInflater.inflate(R.layout.activity_popup,null);
popupWindow = new PopupWindow(container,800,1100,true);
popupWindow.showAtLocation(constraintLayout, Gravity.NO_GRAVITY,150,400);
b1=(Button) findViewById(R.id.b1);
String s1 = b1.getText().toString();
button1.setText(s1);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionevent){
popupWindow.dismiss();
return true;
}
});
}
});
My popup window's code:
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String s1 = "A";
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
});
Screenshots of the app
This is my grid layout where the user must enter the letters
When I click any button on the grid this is the pop up window which is displayed.
If I run this , the app is getting stopped.
It might help us if you would share the exact exception that is thrown by your application.
If I unterstand you correctly, then b1 is a button contained in your activity_popup- layout. So you might want to try b1 = (Button) container.findViewById(R.id.b1) instead. If thats not working, post your exception!
I want to show a button when the virtual keyboard is open and hide this button if the virtual keyboard visibility is off.But I could not find any listeners to perform this activity.
Anybody knows how to do this?
As found here, you'll need to instantiate the SoftkeyBoard and add a listener.
/*
Somewhere else in your code
*/
RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use your root layout
InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);
/*
Instantiate and pass a callback
*/
SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{
#Override
public void onSoftKeyboardHide()
{
// Code here
}
#Override
public void onSoftKeyboardShow()
{
// Code here
}
});
/*
Open or close the soft keyboard programatically
*/
softKeyboard.openSoftKeyboard();
softKeyboard.closeSoftKeyboard();
/*
SoftKeyboard can catch keyboard events when an EditText gains focus and keyboard appears
*/
/* Prevent memory leaks:
*/
#Override
public void onDestroy()
{
super.onDestroy();
softKeyboard.unRegisterSoftKeyboardCallback();
}
In his post, you will also find more information about bug fixes and possible problems.
add onGlobalLayoutListener to your parent view of activity/fragment and make your button visibility accordingly
final View parentView= findViewById(R.id.myrootview);
parentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = root.getRootView().getHeight() - root.getHeight();
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
if(heightDiff <= contentViewTop){
//Soft KeyBoard Hidden---button visible
}else{
//Soft KeyBoard Shown---button hide
}
}
});
There is no direct event for keyboard open and close. but you can create observer on your full layout and then display buttons or whatever you want to do.
For Observer code check this - Hide part of activity_main.xml if keyboard is open (Android)
I am adding this https://github.com/rockerhieu/emojicon lib to my app, Now this lib requires me to add a fragment view to my layout
<fragment
android:id="#+id/emojicons"
android:layout_width="match_parent"
android:layout_height="220dp"
class="com.rockerhieu.emojicon.EmojiconsFragment"/>
after adding this my app crashes, can you please help me solve this?
here is the logat
Caused by: android.view.InflateException: Binary XML file line #90:
Error inflating class fragment
You should use this for Edit Text
<github.ankushsachdeva.emojicon.EmojiconEditText
android:id="#+id/emojicon_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="8"
emojicon:emojiconSize="28sp" />
and then in class file after initialising emojitext, you will have to use these listeners
final EmojiconsPopup popup = new EmojiconsPopup(rootView, this);
//Will automatically set size according to the soft keyboard size
popup.setSizeForSoftKeyboard();
/*--------------------------------------------------------------------*/
popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
changeEmojiKeyboardIcon(emojiButton, R.mipmap.smiley);
}
});
//If the text keyboard closes, also dismiss the emoji popup
popup.setOnSoftKeyboardOpenCloseListener(new EmojiconsPopup.OnSoftKeyboardOpenCloseListener() {
#Override
public void onKeyboardOpen(int keyBoardHeight) {
}
#Override
public void onKeyboardClose() {
if (popup.isShowing())
popup.dismiss();
}
});
/*On emoji clicked, add it to edittext*/
popup.setOnEmojiconClickedListener(new EmojiconGridView.OnEmojiconClickedListener() {
#Override
public void onEmojiconClicked(Emojicon emojicon) {
if (emojiconEditText == null || emojicon == null) {
return;
}
int start = emojiconEditText.getSelectionStart();
int end = emojiconEditText.getSelectionEnd();
if (start < 0) {
emojiconEditText.append(emojicon.getEmoji());
} else {
emojiconEditText.getText().replace(Math.min(start, end),
Math.max(start, end), emojicon.getEmoji(), 0,
emojicon.getEmoji().length());
}
}
});
//On backspace clicked, emulate the KEYCODE_DEL key event
popup.setOnEmojiconBackspaceClickedListener(new EmojiconsPopup.OnEmojiconBackspaceClickedListener() {
#Override
public void onEmojiconBackspaceClicked(View v) {
KeyEvent event = new KeyEvent(
0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
emojiconEditText.dispatchKeyEvent(event);
}
});
// To toggle between text keyboard and emoji keyboard keyboard(Popup)
emojiButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//If popup is not showing => emoji keyboard is not visible, we need to show it
if (!popup.isShowing()) {
//If keyboard is visible, simply show the emoji popup
if (popup.isKeyBoardOpen()) {
popup.showAtBottom();
changeEmojiKeyboardIcon(emojiButton, R.mipmap.ic_action_keyboard);
}
//else, open the text keyboard first and immediately after that show the emoji popup
else {
emojiconEditText.setFocusableInTouchMode(true);
emojiconEditText.requestFocus();
popup.showAtBottomPending();
final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(emojiconEditText, InputMethodManager.SHOW_IMPLICIT);
changeEmojiKeyboardIcon(emojiButton, R.mipmap.ic_action_keyboard);
}
}
//If popup is showing, simply dismiss it to show the undelying text keyboard
else {
popup.dismiss();
}
}
});
Hope this will help you :)
I have always had trouble using the <fragment> tag in my xml. I would suggest switching to a <FrameLayout> and using the following method in your code:
FragmentManager manager = getSupportFragmentManager(); // or getFragmentManager() if you are not using android.support.v4.app.Fragment
manager.beginTransaction().replace(R.id.frame_layout, new EmojiconsFragment()).commit();
Your activity must implement this interfaces:
public class MainActivity extends Activity implements EmojiconGridFragment.OnEmojiconClickedListener, OnEmojiconBackspaceClickedListener{
and implement method (in your activity):
#Override
public void onEmojiconClicked(Emojicon emojicon) {
}
#Override
public void onEmojiconBackspaceClicked(View v) {
}
It is very strange why that was not written in library readme
How to show my layout in front off soft keyboard Android like whatsapp or facebook?
like this
thanks before
this my code, but when emoji layout show, soft keyboard close.
btn_pop_emot.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (listEmot.getVisibility() == View.VISIBLE) {
listEmot.setVisibility(View.GONE);
imm.toggleSoftInputFromWindow(
edt_pop_komen.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
btn_pop_emot.setBackgroundResource(R.drawable.emot_anim);
} else {
listEmot.setVisibility(View.VISIBLE);
imm.hideSoftInputFromWindow(edt_pop_komen.getWindowToken(),
0);
btn_pop_emot.setBackgroundResource(R.drawable.keyboard);
}
}
});
You can use this library, it also contains sample code in README to get you started
https://github.com/ankushsachdeva/emojicon
I want to show my list view when the user clicks on a button and hide it again when they click on a button. This is the onClick listener for the button in question:
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(open){
mDbAdapter.close();
connectBtn.setText("Open Database");
open = false;
hideUI();
}else{
mDbAdapter = new ContactsDbAdapter(v.getContext());
mDbAdapter.open();
connectBtn.setText("Close Database");
open = true;
showUI();
//retrieve data
fillData();
}
}
});
This is the showUI() method:
protected void showUI() {
fName.setVisibility(View.VISIBLE);
lName.setVisibility(View.VISIBLE);
fNameBox.setVisibility(View.VISIBLE);
lNameBox.setVisibility(View.VISIBLE);
createBtn.setVisibility(View.VISIBLE);
this.setVisible(true);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbAdapter.createContact(fNameBox.getText().toString(), lNameBox.getText().toString());
fillData();
}
});
}
and the hideUI() method:
protected void hideUI() {
fName.setVisibility(View.INVISIBLE);
lName.setVisibility(View.INVISIBLE);
fNameBox.setVisibility(View.INVISIBLE);
fNameBox.clearComposingText();
lNameBox.setVisibility(View.INVISIBLE);
lNameBox.clearComposingText();
createBtn.setVisibility(View.INVISIBLE);
this.setVisible(false);
}
It works fine when I set the visibility to true. However when I set it to false I get a black screen but no crash or error. Any idea?
NOTE: this.setVisible(false);. My class extends ListActivity.
setVisibility(View.INVISIBLE);
Just makes you view invisible but the space taken by view will be their itself
use setVisibility(View.GONE); so that the size of view will be lapsed
Use this and let me know if it is helpful
ListActivity is hold list view
if u do this.setVissiblity(false);
it hide the list view and its contents so u r seeing background color in ur case it is black.
Good way is take Listview in xml and get id make vissible nad invissible of that view u feel very confortable with this apprch
http://www.vogella.com/articles/AndroidListView/article.html read this u will get clear idea. and make changes accordingly