Change the behaviour of "imeOptions" (Android) - java

This is my problem right now, guys.
I have the next TextView and EditText
activity_profile.xml
<TextView
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/done"
android:id="#+id/done_profile_btn"
android:textColor="#color/white"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:id="#+id/input_profile_swift"
android:hint="#string/swift"
android:inputType="textCapCharacters"
android:textColor="#color/colorPrimaryDark"
android:layout_marginBottom="22dp"
android:maxLines="1"
android:imeOptions="actionDone"/>
And I would like to change the action that press the 'Done' button on the keyboard does for the EditText in order to do the same as the TextView, which does the next:
ProfileActivity.java
done_btn = (TextView) findViewById(R.id.done_profile_btn);
swift = (EditText)findViewById(R.id.input_profile_swift);
done_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
//saveChanges();
//finish();
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
});
...
So... I want that both (press 'Done' on my keyboard and press the TextView) do the same.
Any idea of how to do that? Thank you very much.

You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:
swift.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
//saveChanges();
//finish();
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
return false;
}
});
Hope it will help !

try this
EditText swift = (EditText)findViewById(R.id.input_profile_swift);
swift.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
}
});
You have to implements OnEditorActionListener interface and set it to your EditText. using setOnEditorActionListener(OnEditorActionListener implimentation.)

Use TextView.OnEditorActionListener.
#Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
KeyboardHelper.hideSoftKeyboard(getActivity());
doSomeWoorrkThatYouNeed();
return true;
}

You should add this android:clickable="true" to your xml for the TextView.
Also you could set an EditorActionListener using setOnEditorActionListener on your EditText in similar way that you did with setOnClickListener for your TextView.

Related

How to disable onTouch of other items in Recyclerview? [duplicate]

I am using Floating Action Button. I want to disable Recyclerview Items from Clicking when i press FAB button. I tried this method but not working setClickable(true);
My Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#fff"
tools:context="com.hartwintech.socialchat.activity.IconTabsActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
>
</android.support.v7.widget.RecyclerView>
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/floatmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="60dp"
android:layout_marginRight="16dp"
fab:fab_showAnimation="#anim/show_from_bottom"
fab:fab_hideAnimation="#anim/hide_to_bottom"
fab:menu_labels_style="#style/MenuLabelsStyle"
fab:menu_shadowColor="#444"
fab:menu_colorNormal="#FFB805"
fab:menu_colorPressed="#F2AB00"
fab:menu_colorRipple="#D99200"/>
</RelativeLayout>
Java Class
floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
#Override
public void onMenuToggle(boolean opened) {
if (opened) {
final int color = R.color.transp;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mrecyclerview.setClickable(false);
mrecyclerview.setEnabled(false);
mrecyclerview.setForeground(new ColorDrawable(ContextCompat.getColor(getContext(), color)));
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mrecyclerview.setClickable(true);
mrecyclerview.setEnabled(true);
mrecyclerview.setForeground(null);
}
}
}
});
You can add a simple boolean to your adapter like this:
public boolean isClickable = true;
and set it in your fab-click:
mAdapter.isClickable = true/false;
And within your OnClickListener in the Adapter, only act when it is clickable:
public void onClick(View view) {
if(!isClickable)
return;
// do your click stuff
}
To disable RecyclerView, follow below steps:
1. Add following view into your layout file,
<View
android:id="#+id/viewDisableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#40000000"
android:clickable="true"
android:focusable="true"
android:visibility="gone"/>
2. Set View Visibility `View.VISIBLE when you want to disable RecyclerView else
You can simply use recursion to disable/enable clicks on view
public static void setClickable(View view, boolean clickable) {
if (view != null) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
setClickable(viewGroup.getChildAt(i), clickable);
}
}
view.setClickable(clickable);
}
}
Björn Kechel's answer helps me. As he said I just added Boolean. When i click the fab menu the boolean is activated. Then have to write the condition on mrecyclerview.addOnItemTouchListenerJava Class
public Boolean fabClick = false;
floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
#Override
public void onMenuToggle(boolean opened) {
if (opened) {
final int color = R.color.transp;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fabClick = true;
mrecyclerview.setClickable(false);
mrecyclerview.setEnabled(false);
mrecyclerview.setForeground(new ColorDrawable(ContextCompat.getColor(getContext(), color)));
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fabClick = false;
mrecyclerview.setClickable(true);
mrecyclerview.setEnabled(true);
mrecyclerview.setForeground(null);
}
}
}
});
mrecyclerview.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mrecyclerview, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
if(!fabClick) {
android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fragment_anim_start, R.anim.fragment_anim_stop);
Intent i = new Intent(getActivity(), Group_Chat_Screen.class);
startActivity(i);
}
}
You need to set the click listener to every FloatingActionButton.
see this issue on library
Working solution with RecyclerView.OnItemTouchListener:
#SuppressLint("ClickableViewAccessibility")
#BindingAdapter("itemsClickable")
fun setRecyclerViewClickable(view: RecyclerView, clickable: Boolean) {
view.isEnabled = clickable
if (!clickable) {
val itemTouchListener = object : RecyclerView.OnItemTouchListener {
override fun onTouchEvent(rv: RecyclerView?, e: MotionEvent?) {
}
override fun onInterceptTouchEvent(rv: RecyclerView?, e: MotionEvent?): Boolean {
return rv?.isEnabled == false
}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
}
}
view.addOnItemTouchListener(itemTouchListener)
view.tag = itemTouchListener
} else {
(view.tag as? RecyclerView.OnItemTouchListener)?.let {
view.requestDisallowInterceptTouchEvent(true)
view.removeOnItemTouchListener(it)
}
}
}
Java
recyclerView.setOnTouchListener((view1, motionEvent) -> true );
Kotlin
reyclerView.setOnTouchListener { v, event -> true }
this solution disables all touch events
You can disable the touch of recyclerview
recyclerView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
In the xml file, set the layout_width and layout_height for FloatingActionMenu as match_parent and set clickable as false :
android:layout_width="match_parent "
android:layout_height="match_parent "
android:clickable="false"
In your java class,
floatMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
#Override
public void onMenuToggle(boolean opened) {
if (opened) {
floatMenu.setClickable(true);
} else {
floatMenu.setClickable(false);
}
}
});
This should work.
I solved this problem with very simple logic. This will prevent double click on single item and multiple items of RecyclerView as well.
Declare a Variable in your Activity.
private long mLastClickTime = 0;
Then use in any OnClickListener.
#Override
public void onClick(View v) {
if(SystemClock.elapsedRealtime() - mLastClickTime < 1000){//You can reclick after 1 second
return;//Before 1 seconds from first click this onclick will return from here
}
mLastClickTime = SystemClock.elapsedRealtime();
//Do stuff here
}
Actually I found this solution in stackover flow when I was searching for preventing double click on Button. I'm writing this line to acknowledge that actual answer is posted by someone ( Unfortunatly I'm unable to find that answer to link his/her answer here.)
Hope this will solve your problem.:)

setOnEditorActionListener() method not working with inputtype textMultiline along with enter button in keyboard

I am stuck into a problem that when iam using inputtype as textmultiline in xml and imeiOptions as KEYCODE_ENTER in edittext.Then my setOnEditorActionListener is not working but if i change my inputtype to text then its working fine.Iam not able to understand that why this is not working with textMultiline.
My Edittext defined in xml is-:
<EditText
android:id="#+id/etremarks"
android:layout_width="match_parent"
android:layout_height="#dimen/_200sdp"
android:background="#drawable/shape_edittext_pink_border"
android:fontFamily="#font/share"
android:gravity="top"
android:hint="Enter Message"
android:inputType="textMultiLine"
android:padding="5dp"
android:textColor="#000000" />
and my code for edittext in java file is-:
etremarks.addTextChangedListener(mTextEditorWatcher);
etremarks.setImeOptions(KeyEvent.KEYCODE_ENTER);
etremarks.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
boolean handled = false;
if (actionId == KeyEvent.KEYCODE_ENTER)
{
// Handle pressing "Enter" key here
Toast.makeText(SendBulkSmsToClients.this, etremarks.getText(), Toast.LENGTH_SHORT).show();
handled = true;
}
return handled;
}
});
Please share your valuable opinions for this problem
thanks
Use
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
and in XML:
android:inputType="textMultiLine"
Source : Multi-line EditText with Done action button
add this line android:imeOptions="actionSend" and in the code , handle like this below
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}

Listening For Touch In Padding Area Between Elements of An AlertDialog

I have onTouchListeners for the (custom) title area and for the message area of my AlertDialog.
I'm trying to have my dialog set up in such a way that the user can mindlessly tap in the top right 1/4th of the AlertDialog to toggle the whether background music plays or not since just having the speaker as the tappable area would result in too small of a hit box/area.
My problem is: the area in between the message and title marked in red isn't handling ontouchlistener events
Most people would suggest creating a custom dialog, but the thing is I really like the way this dialog looks (it has a very stock material design aesthetic) and already jumped through a lot of hoops to get it to look exactly the way I like (drawing leaderboard over an invisible neutral button, custom title area). I don't want to make a custom dialog unless I can make it look absolutely identical to what I have now (so hard to mimic the look of stock material dialogs, trust me i've tried and did a lot of research/wasted a lot of time trying that).
I'm assuming the on touch events for the custom title area and message area don't encompass or account for the margins or padding in between.
Pardon the disgusting code!! I'm just trying to hack everything together and tidy it up later.
Thanks in advance!
The linear layout for my custom title area of the alertdialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0px"
android:paddingTop="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingBottom="0px"
android:orientation="horizontal"
android:id="#+id/st"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:paddingTop="20dp"
android:paddingRight="5dp"
android:src="#drawable/ic_pause"/>
<TextView
android:id="#+id/leaderboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800080"
android:textSize="22sp"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.DialogWindowTitle"
android:layout_gravity="center"
android:paddingTop="18dp"
android:layout_weight="1"
android:text="Paused"
/>
<ImageView
android:id="#+id/soundtoggle"
style="?android:attr/panelTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:paddingTop="20dp"
android:paddingRight="?android:dialogPreferredPadding"
android:gravity="right"/>
</LinearLayout>
My android code
AlertDialog.Builder ad = new AlertDialog.Builder(new ContextThemeWrapper(AndroidLauncher.this, android.R.style.Theme_Material_Light_Dialog))
.setMessage(msg)
.setCustomTitle(myLayout)
.setCancelable(false)
.setNegativeButton("End Game", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pauseInterface.end();
dialog.cancel();
}
})
.setNeutralButton(" ", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//startSignInIntent();
showLeaderboard();
dialog.cancel();
}
})
.setPositiveButton("Resume", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pauseInterface.resume();
dialog.cancel();
}
});
alertDialog = ad.create();
int titleId = getResources().getIdentifier("alertTitle", "id", "android");
if (titleId > 0) {
TextView dialogTitle = (TextView) alertDialog.findViewById(titleId);
}
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
Drawable drawable = getResources().getDrawable(R.drawable.ic_leaderboard);
drawable.setBounds((int) 0,
0, (int) (drawable.getIntrinsicWidth() * 1),
drawable.getIntrinsicHeight());
button.setCompoundDrawables(drawable, null, null, null);
}
});
alertDialog.show();
final ImageView soundToggle = (ImageView) alertDialog.findViewById(R.id.soundtoggle);
if (tetrisgame.getMusicState()) {
if (tetrisgame.getMusicState()) {
soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_on, getApplicationContext().getTheme()));
} else {
soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, getApplicationContext().getTheme()));
}
TextView tv = (TextView) alertDialog.findViewById(android.R.id.message);
tv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent m) {
if (m.getAction() == MotionEvent.ACTION_DOWN && (m.getX() >= (v.getRight() - (v.getWidth() / 4)))) {
Log.println(Log.ERROR, "Ken", "Popular Pothead");
if (tetrisgame.toggleMusic()) {
String uri = "#drawable/music_on"; // where myresource (without the extension) is the file
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Drawable res = getResources().getDrawable(imageResource);
soundToggle.setImageDrawable(res);
// soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, alertDialog.findViewById(android.R.id.message).getTheme()));
} else {
soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, null));
}
}
return true;
}
});
final LinearLayout as = (LinearLayout) alertDialog.findViewById(R.id.st);
as.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent m) {
if (m.getAction() == MotionEvent.ACTION_DOWN) {
Log.println(Log.ERROR, "Ken", "Tweed");
if (tetrisgame.toggleMusic()) {
String uri = "#drawable/music_on"; // where myresource (without the extension) is the file
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Drawable res = getResources().getDrawable(imageResource);
soundToggle.setImageDrawable(res);
// soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, alertDialog.findViewById(android.R.id.message).getTheme()));
} else {
soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, null));
}
}
return true;
}
});

How to Add multiple choice checkboxes in alert Dialog in Android?

I have created menu "Sync" in android app. when we click on "Sync" alert open a 4 checkboxes layout. what I want is to have them in function like when I click on 15 minutes then other option unclicked automatically.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_settings:
alertDialog = new AlertDialog.Builder(HomePage.this).create(); //Read Update
LayoutInflater adbInflater = this.getLayoutInflater();
View checkboxLayout = adbInflater.inflate(R.layout.sync_layout, null);
defaultchkbox = (CheckBox)checkboxLayout.findViewById(R.id.defaultchkbox);
after15mint = (CheckBox)checkboxLayout.findViewById(R.id.after15mint);
after30mint = (CheckBox)checkboxLayout.findViewById(R.id.after30mint);
after45mint = (CheckBox)checkboxLayout.findViewById(R.id.after45mint);
alertDialog.setView(checkboxLayout);
alertDialog.setTitle("Synchronization");
alertDialog.setMessage("Choose");
alertDialog.setButton(Dialog.BUTTON_POSITIVE,"Save changes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
boolean checkBoxResult = false;
if(after15mint.isChecked())
{
Toast.makeText(getApplicationContext(), "15 Minute checked", Toast.LENGTH_LONG).show();
checkBoxResult = true;
}
else if(after30mint.isChecked())
{
Toast.makeText(getApplicationContext(), "30 Minute checked", Toast.LENGTH_LONG).show();
checkBoxResult = true;
}
else if(after45mint.isChecked())
{
Toast.makeText(getApplicationContext(), "45 Minute checked", Toast.LENGTH_LONG).show();
checkBoxResult = true;
}
else{
Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show();
}
}
});
alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.dismiss();
}
});
alertDialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But I am little bit confused over working of check boxes in alert. Suggestions will be great help. Thank you. :)
It looks like you need to work with Radio Buttons which are nested within a RadioGroup. It will then only allow you to select one option at a time.
For more information on RadioGroup look at:
http://developer.android.com/reference/android/widget/RadioGroup.html
For more information on creating Radio Buttons look at:
http://developer.android.com/reference/android/widget/RadioButton.html
in particular to your code you will have to define RadioButtons within a RadioGroup in your R.layout.sync_layout like for example
<RadioGroup
android:id="#+id/syncGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/defualt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "Default"
android:checked="true" />
<RadioButton
android:id="#+id/minute15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "15 Minute" />
<RadioButton
android:id="#+id/minute30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "30 Minute" />
<RadioButton
android:id="#+id/minute45"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "45 Minute" />
</RadioGroup>
See this link.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color);
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}

search a website in android

I am in a development of an android that will search for a particular word in a website..for that I developed some code...but it is not complete...is anyone able to help..it will be a help for me...thank you so much for reading
public class MainActivity extends Activity {
EditText et = new EditText(this);
public void onClick(View v){
Editable searchText = et.getText();
Intent intent = new Intent(this, com.example.app.MainActivity.class);
intent.putExtra("searchQuery", searchText);
startActivity(intent);
}
}
Try it....
<EditText
android:id="#+id/edt_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:ellipsize="end"
android:hint="Enter Your Text"
android:imeOptions="actionGo"
android:singleLine="true"
android:textSize="14sp" />
EditText edt_search;
edt_search = (EditText) findViewById(R.id.edt_search_book);
edt_search.setImeOptions(EditorInfo.IME_ACTION_GO);
edt_search.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// Log.i("KeyBoard" ,"Inside the Edit Text");
if (actionId == EditorInfo.IME_ACTION_GO) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, edt_search.getText().toString()); // query contains
startActivity(intent); // search string
}
return false;
}
});

Categories