Android Studio: scrollTo in Horizontal function isn't working - java

I'm making this converter app and have spent some time trying to figure out how to work this scrollTo() function to scroll to the button I desire it to be focused in a Horizontal Scroll View.
I've implemented the following to my onCreate:
final HorizontalScrollView HscrollView1 = (HorizontalScrollView)findViewById(R.id.hsView1);
final HorizontalScrollView HscrollView2 = (HorizontalScrollView)findViewById(R.id.hsView2);
final Button cmBtn = (Button)findViewById(R.id.cm_id);
final Button KmBtn = (Button)findViewById(R.id.km_id);
final Button mmBtn = (Button)findViewById(R.id.mm_id);
HscrollView1.scrollTo((int) mmBtn.getX(), 0);
HscrollView2.scrollTo((int) cmBtn.getX(), 0);
After running APK, it won't scroll to mm and cm button when the app starts (unlike what I wrote in the code)
It seems so frustrating to me with this scrollview and it would be awesome if someone can help me here at 5am (est)
Thank you!

replace
HscrollView1.scrollTo((int) mmBtn.getX(), 0);
HscrollView2.scrollTo((int) cmBtn.getX(), 0);
with
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
HscrollView1.scrollTo((int) mmBtn.getX(), 0);
HscrollView2.scrollTo((int) cmBtn.getX(), 0);
}
}, 500);
and run your code.

Related

Crash when using Handler/addView in Android Studio

so basically i have 2 problems.. 1st problem is the handler and runnable..
i have an image and i want to make it move..
so i wrote this
ImageView a = findViewById(R.id.os);
final Handler handler = new Handler();
final Runnable run = new Runnable() {
#Override
public void run() {
float y = a.getY();
if(y > layout.getHeight())
y=0;
else
y+=7;
handler.postDelayed(this,3000);
a.setY(y);
}
};
handler.post(run);
when i run it, the app crashes...my layout is ConstraintLayout.
and i wrote this code in a method "onClick" so after clicking the button the image starts moving.
..
2nd Problem : i want to add alot of objects (enemies etc to make a game) but the problem if I do that using XML it will take a long time. imagine adding 200 objects and what if I wanted to repeat adding them after they get removed or changing their attributes ? i will need to define an id for all them and create objects in reference to their id's all of them ...so I needed to figure out how to add objects programmatically to the ConstraintLayout..so i Wrote this:
ArrayList<ImageView> bosses=new ArrayList<>();
ConstraintLayout d = findViewById(R.id.layout);
for(int i =0;i<200;i++) {
bosses.add(new ImageView(this));
ConstraintLayout.LayoutParams lay =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lay.startToStart= d.getId(); lay.endToStart= d.getId();
lay.topToTop = d.getId(); lay.bottomToTop=d.getId();
bosses.get(i).setLayoutParams(lay);
d.addView(bosses.get(i));
}
and it crashes too....I'm trying to learn how to move objects and how to add them programmatically so I can create games but i keep facing these crashes. if anyone could help I'll be thankful.

display activity countdowntimer in fragment

I am just starting to learn Android, Java and need help.
I have an activity with the countdowntimer, which works fine. However, I want it to be displayed in the fragment. What is the best way to do it?
I tried calling Timer.getCountdowntimer, I tried calling Timer.getUserTime (userTime is the user selected time for the countdowntimer), but the textview in my fragment doesn't display the timer.
thanks in advance!
If you are coding in Java purely, and want to use the android SDK to do it, I would recommend:-
//Create a handler that runs on main loop so we can update UX
final android.os.Handler handler = new android.os.Handler(Looper.getMainLooper());
//Get a callback in 1 second
handler.postDelayed(new Runnable() {
int timer;
#Override
public void run() {
timer += 1;
myTextView.setText(String.valueOf(timer));
//Recursively get another callback in a second
handler.postDelayed(this, 1000);
}
}, 1000);
Make sure you add some logic to stop the timer when you want, and also onPause/onResume

Method to capture the whole Screen from a Floating Window Application

I'm trying to do implement a floating widget button(like the Facebook Messenger) which is displayed over the other app. The button should be able to interfere with the displayed view of the other app and capture the whole smartphone screen underneath.
The floating button is already implemented and works fine on every app. Getting the root view and printing out the bitmap of this floating view will just return the floating button image. I think the floating window cant detect the view of the apps underneath.
One possible solution could be to start the other application as a separate activity and get its view, but I am not sure how to do that. Maybe there is some easier way to achieve this?
//Creating the floating window and set its Layout parameters
mFloatViewLayoutParams = new WindowManager.LayoutParams();
mFloatViewLayoutParams.format = PixelFormat.TRANSLUCENT;
mFloatViewLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED;
mFloatViewLayoutParams.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
mFloatViewLayoutParams.gravity = Gravity.START;
mFloatViewLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatViewLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// adding the floatview to the WindowManager
LayoutInflater inflater = LayoutInflater.from(activity);
mFloatView = inflater.inflate(R.layout.float_view_layout, null);
mWindowManager.addView(mFloatView, mFloatViewLayoutParams);
...
// some clickListener to start the screenshot
ImageButton imageButton = mFloatView.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// here I want to get the view of the whole screen to
screenshot it
}
});
}
});
What I don't want: I don't want to screenshot the view of my main activity which starts the floating button.
I am really out of ideas about how to handle this challenge. Hopefully, someone has a guess!
Cheers!

Android Button Not moving onclick

I have an up button which I want to move up onClick but when I click on it it moves way to far and gets to the end of the screen and then shrinks down untill you cant see it. It moves up too much, but why? I only increase it by one unit?
final Button upbutton = (Button)findViewById(R.id.upbutton);
upbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
upbutton.getLayoutParams();
mParams.bottomMargin += 1;
upbutton.setLayoutParams(mParams);
}
});
}
Because you're not assigning mParams to your buttons params.
mParams = upbutton.getLayoutParams();
You seem to be increasing parameters for your RelativeLayout then assigning those to your button. So the button gets confused. Try looking for a set margin option or something on the actual button view.

Android PopupWindow showAtLocation() vs showAsDropDown() - first one is working for me and second is not

I used the following code to show a PopupWindow in android, I want to pin a popup to a Button,
btnShowPopup = (Button)findViewById(R.id.btnShowPopup);
btnShowPopup.setOnClickListener(this);
#Override
public void onClick(View v) {
if(v == btnShowPopup){
View popupViw = getLayoutInflater().inflate(R.layout.popup_demo, null);
PopupWindow popup = new PopupWindow(this);
popup.setContentView(popupViw);
popup.showAsDropDown(btnShowPopup,10,10);
//popup.showAtLocation(popupViw, Gravity.BOTTOM, 10, 10);
}
}
In this case popup.showAtLocation() is working well, but popup.showAsDropDown() not displaying any popups for me.
It is probably working, but showing the popup below the visible window, as it is attaching to the lower left corner of the btnShowPopup view.

Categories