I am new to Android Studio. I am working on a ArCore sample project.
I want to add a button, when clicked the plane finding mode will be vertical otherwise horizontal.
Before voting negative, I have gone through these posts:
How can I write “if button clicked” in an If statement in android studio?
Checking if a button has been clicked
The following is the snippet of my code:
if(y > 0){
Config config = new Config(session);
//config.setPlaneFindingMode(Config.PlaneFindingMode.VERTICAL); If button clicked, this
config.setPlaneFindingMode(Config.PlaneFindingMode.HORIZONTAL); //otherwise this
session.configure(config);
Globals.notRecording = true;
}
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Just use this code in OnClickListener.
when you tap button call this
config.setPlaneFindingMode(Config.PlaneFindingMode.VERTICAL);
Is it possible when any users clicks on button which I created on Application, and code some numbers like *123#. When click happens then it automatically execute this without showing dialpad to User and show its responce in popup like we get.
May be it little bit confusing because I do not know how to clearly explain it. If any person know how to do that in Android Studio.
You can add this a method in your button listener dialToNumberStr("0123456789")
public void dialToNumberStr(String numberToDial){
//String numberToDial = "0123456789"
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:".concat(numberToDial)));
startActivity(dialIntent);
}
your listener should be (using Lambda Expression):
myButton.setOnClickListener(v->{
dialToNumberStr("0123456789");
});
your listener should be (or the old way):
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialToNumberStr("0123456789");
}
});
This is what is working correctly
>>The user swipes on a part of the main activity ui we will call the MainTitleBar.
>>This creates a dialog with stuff in it
=----------------------------------------------------------------------------=
Now I need to make it so when someone clicks on the buttons or imageviews within that dialog STUFF happens. I cannot figure out for the life of me how to handle this.
Code I have currently inside the maintitlebar.java class (which extends view) and is the view they are swiping on to create the dialog box.
--- bunch of stuff like ui stuff and swipe calculations etc
public void onDownSwipe() {
// custom dialog object created
Dialog ntd = new Dialog(getContext(),R.style.lightbox_dialog);
ntd.setContentView(R.layout.create_task_dialog);
ntd.show();
ImageView cancelbutton = (ImageView) ntd.findViewById(R.id.cancelbutton);
cancelbutton.setOnClickListener(new OnclickListener(){
public void onClick(View v) {
ntd.dismiss();
}
}
);
In the xml for the dialog, the create_task_dialog is the overall xml for the dialog.
the cancelbutton (id) is the id for the cancel button (which is properly added with + sign into resource file).
Should I be putting the onclicklistener and clicky stuff somewhere else in my code?
So confused as to how to make my dialog buttons/imageviews clickable.
THANKS!
I am following a youtube tutorial and I've got most of the works done, but I still got some problems.
I have my custom layout for my custom dialog, all I wanted to do is to set the custom dialog on a button. Once we click the button ,the dialog shows, that's it. I've already set the onclicklistener on the button, here's my code.
Credit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog credit = new Dialog(Main.this);
credit.setContentView(R.layout.creditdialog);
credit.setTitle(" ");
credit.show();
}
});
I followed all of this on a tutorial, but I don't know that the "MAIN" is about, I got an error there. Please tell me what to do. Sorry for my poor English.
new Dialog(Main.this);
The above line creates a new dialog object and associates it with the context of your Activity. SO you have to pass the context of your activity in the paranthesis..
Eg:
If you are calling the dialog from Activity "ActivityMain".. then use:
new Dialog(ActivityMain.this);
try this
Credit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog credit = new Dialog(getApplicationContext());
credit.setContentView(R.layout.creditdialog);
credit.setTitle(" ");
credit.show();
}
});
it is common question! but you can extend dialog box and can set custom layout for your dialog than on button click just call dialog like this:
new CustomDialog(activity).show();
also you can follow this tutorial on custom dialog to understand how to customise dialog and how to use it on button click.
http://www.shaikhhamadali.blogspot.com/2013/09/types-of-dialogbox-part-two-custom.html
Currently, I have a layout which contains a Button, a TextView and an EditText. When the layout is displayed, the focus will be automatically put on the EditText, which will trigger the keyboard to show up on Android phone. It is not what I want. Is there any way that I can set the focus on TextView or on nothing when a layout is displayed?
Set focus: The framework will handled
moving focus in response to user
input. To force focus to a specific
view, call requestFocus()
This works:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You should add this:
android:focusableInTouchMode="true"
To set focus, delay the requestFocus() using a Handler.
private Handler mHandler= new Handler();
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainVw = (LinearLayout) findViewById(R.id.main_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edit = new EditText(this);
edit.setLayoutParams(params);
mainVw.addView(edit);
TextView titleTv = new TextView(this);
titleTv.setText("test");
titleTv.setLayoutParams(params);
mainVw.addView(titleTv);
mHandler.post(
new Runnable()
{
public void run()
{
titleTv.requestFocus();
}
}
);
}
}
Set
android:focusable="true"
in your <EditText/>
You can try just hidding the keyboard. Something like this:
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Set these lines to OnResume as well and make sure if focusableInTouch is set to true while you initialize your controls
<controlName>.requestFocus();
<controlName>.requestFocusFromTouch();
Try
comp.requestFocusInWindow();
to change the focus make the textView in xml focusable
<TextView
**android:focusable="true"**
android:id="#+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
and in java in on create
textView.requestFocus();
or simply hide the keyboard
public void hideKeyBoard(Activity act) {
act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
}
None of the answers above works for me. The only (let's say) solution has been to change the first TextView in a disabled EditText that receives focus and then add
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
in the onCreate callback to prevent keyboard to be shown. Now my first EditText looks like a TextView but can get the initial focus, finally.
The last suggestion is the correct solution. Just to repeat, first set android:focusable="true" in the layout xml file, then requestFocus() on the view in your code.
i think a text view is not focusable. Try to set the focus on a button for example, or to set the property focusable to true.
you can add an edit text of size "0 dip" as the first control in ur xml, so, that will get the focus on render.(make sure its focusable and all...)
You can start by adding android:windowSoftInputMode to your activity in AndroidManifest.xml file.
<activity android:name="YourActivity"
android:windowSoftInputMode="stateHidden" />
This will make the keyboard to not show, but EditText is still got focus. To solve that, you can set android:focusableInTouchmode and android:focusable to true on your root view.
<LinearLayout android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
...
>
<EditText
...
/>
<TextView
...
/>
<Button
...
/>
</LinearLayout>
The code above will make sure that RelativeLayout is getting focus instead of EditText
Focus is for selecting UI components when you are using something besides touch (ie, a d-pad, a keyboard, etc.). Any view can receive focus, though some are not focusable by default. (You can make a view focusable with setFocusable(true) and force it to be focused with requestFocus().)
However, it is important to note that when you are in touch mode, focus is disabled. So if you are using your fingers, changing the focus programmatically doesn't do anything. The exception to this is for views that receive input from an input editor. An EditText is such an example. For this special situation setFocusableInTouchMode(true) is used to let the soft keyboard know where to send input. An EditText has this setting by default. The soft keyboard will automatically pop up.
If you don't want the soft keyboard popping up automatically then you can temporarily suppress it as #abeljus noted:
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
When a user clicks on the EditText, it should still show the keyboard, though.
Further reading:
Having Trouble Focusing? A Primer on Focus in Android
Android Developers Blog: Touch Mode
You can add
android:importantForAccessibility="yes"
android:focusable="true"
android:focusableInTouchMode="true"
to your Layout to force Talkback/accessibility to go there first.
You really only need the first line, however, the others reinforce to the OS what you want focused.
You can use the following Kotlin extension
fun View.focusAndShowKeyboard() {
/**
* This is to be called when the window already has focus.
*/
fun View.showTheKeyboardNow() {
if (isFocused) {
post {
// We still post the call, just in case we are being notified of the windows focus
// but InputMethodManager didn't get properly setup yet.
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
requestFocus()
if (hasWindowFocus()) {
// No need to wait for the window to get focus.
showTheKeyboardNow()
} else {
// We need to wait until the window gets focus.
viewTreeObserver.addOnWindowFocusChangeListener(
object : ViewTreeObserver.OnWindowFocusChangeListener {
override fun onWindowFocusChanged(hasFocus: Boolean) {
// This notification will arrive just before the InputMethodManager gets set up.
if (hasFocus) {
this#focusAndShowKeyboard.showTheKeyboardNow()
// It’s very important to remove this listener once we are done.
viewTreeObserver.removeOnWindowFocusChangeListener(this)
}
}
}
)
}
}
And just call your view.focusAndShowKeyboard() in override fun onViewCreated(..) or override fun OnCreate(..)
PS: For hiding Views use the following extension
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
Make sure the views are focusable before that using the following android XML attributes, u can also do it programmatically
android:focusableInTouchMode="true"
android:focusable="true"