After button click to mouse scroll to be activted,then scroll up to value increment and scroll down to value decrement.Button inside code here not working, my sample code here,please analysis code to help me.
enter code here
public class MainActivity extends Activity {
Button button;
int x,f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnTouchListener(new OnTouchListener() {
#SuppressLint("NewApi") #Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
//selectNext();
{
x=Integer.parseInt(button.getText().toString());
f=x+5;
button.setText(""+f);
}
else
{
x=Integer.parseInt(button.getText().toString());
f=x-5;
button.setText(""+f);
return true;
}
}
return false;
}
});
}
You can implement a ScrollView as your layout in your UI.
With this layout you can program your button to handle the click and use the ScrollView.scrollBy(x,y) function to do the necessary.
Related
This is the sample code from Here API. But the problem I have is that the initial location where the map is loaded is wrong. What I need is for the map to be displayed where you are.
What is the method that modifies the position of the map?
private MapViewLite mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a MapViewLite instance from the layout.
mapView = findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
}
private void loadMapScene() {
// Load a scene from the SDK to render the map with a map style.
mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new MapScene.LoadSceneCallback() {
#Override
public void onLoadScene(#Nullable MapScene.ErrorCode errorCode) {
if (errorCode == null) {
mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915));
mapView.getCamera().setZoomLevel(14);
} else {
Log.d(TAG, "onLoadScene failed: " + errorCode.toString());
}
}
});
}
This is the piece of code that hard codes the position:
mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915))
Instead, set the coordinates to be the user's click. In order to do so, you will need to surround that piece of code with an onTouch() listener and check the action of the user. I'll use an ImageView as an example. So something like this:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//whatever you want to do
//to fetch the coordinates of the user's click use event.getX() and event.getY()
}
return true;
}
});
I've created a method that is called when my EditText is clicked (or touched, to be specific), which simply takes the current time and displays it exactly as it has been passed by System.currentTimeMillis() in a TextView below. The method is this one:
public void captureTime(View view) {
currentTime = System.currentTimeMillis();
String currentTimeStr = currentTime+ "";
TextView textView = findViewById(R.id.textView);
textView.setText(currentTimeStr);
}
To do that, I've had to add the code below in the onCreate method. It uses setOnTouchListener instead of setOnClickListener since otherwise the captureTime method wasn't always called. Now, this method does what I wanted, but now when I click or touch the EditText, although it does this as required, the keyboard doesn't show up anymore. I have looked into other questions in which the keyboard doesn't show up either but they're not related to my case, although I have tried them. So, what is that is making the keyboard not show up and how can I fix this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_time = (EditText)findViewById(R.id.editText);
add_time.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
captureTime(add_time);
return true;
}
return false;
}
});
}
Try removing return true (in the if block). In this case, the "captureTime" method will work and the keyboard will open.
I write an android app
I set an editText.setOnClickListener(...)
but i see that when the user clicks, the soft-keyboard is opened and only
when the user clicks on a keyboard key - the onClick() is called.
how to catch the click before the soft keyboard is opened?
I want to avoid the keyboard opening.
here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_login);
initMembers();
setOnClickListeners();
initFieldsTexts();
setKeyboardVisibilityListener();
}
private void setOnClickListeners() {
mPhoneNumberField.setInputTextOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
and:
public class PhoneLoginFillInField extends LinearLayout {
..
public void setInputTextOnClickListener(OnClickListener onClickListener)
{
mInputText.setOnClickListener(onClickListener);
}
during debugging i see this line is called twice
mPhoneNumberField.setInputTextOnClickListener(new OnClickListener() {
though it's called only from onCreate and there setOnClickListeners(); is called once
You mean EditText should not gain focus; it needs to respond only to click events. Do this:
editText.setFocusableInTouchMode(false);
And then carry on:
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Your code...
}
}
Set an onTouchListener to the EditView. Remember to return true to avoid the event propagation.
I just don't know how to do it. The Code is so confusing for me. Can anyone please show me the code with explanation?
What I want is to assign enter keyCode to the btn button, so when user touches Enter SoftKey, the toast will show up just like clicking the button!
Here is The Simple App to use the code :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Please Explain it to me don't Just Write it , I'm New to Android , thanks for your Time
Well, you need to set OnKeyListener for your button similar way you already set OnClickListener:
b.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// your custom implementation
if (KeyEvent.KEYCODE_ENTER == keyCode) { // match ENTER key {
// show the toast
Toast.makeText(MainActivity.this, "Hello World",
Toast.LENGTH_SHORT).show();
return true; // indicate that we handled event, won't propagate it
}
return false; // when we don't handle other keys, propagate event further
}
});
Additional explanations are in the comments. Hope that helps
i have 4 tab with activity group..all tab contain list of item and on press of any item its discriptioo will be displayed in new activity..
i m using activitygroup to embedded child activity in tab.and i m using replace contentview to change the activitygroup view.
when i press back button i call finish() from child and i immediately get out of application..is there any way to return back to parent activity using activity group...???
i m using following code to chang activitygroup view..bt dont know how to come back to this activity..
public void replaceContentView(String id, Intent newIntent)
{
View mview = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)).getDecorView();
this.setContentView(mview);
}
I was also stuck with this problem but solved it have a look a t below code hope will help you also
Your activityGroup should be something like this
public class ABCGroup extends ActivityGroup{
public static ABCGroup group;
private ArrayList<View> history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
View view = getLocalActivityManager().startActivity
("ParentActivity",
new Intent(this, ParentActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
if(history.size()<=0){
finish();
}else{
setContentView(history.get(history.size()-1));
}
}else {
finish();
}
}
#Override
public void onBackPressed() {
ABCGroup.group.back();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK){
ABCGroup.group.back();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
In your parent activity
View mview = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)).getDecorView();
ABCGroup.group.replaceView(v);
In your child activity use
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK){
ABCGroup.group.back();
return true;
}
return super.onKeyDown(keyCode, event);
}