How do I change the format of my SeekBar? - java

I am beginner student in Java and android studio. I am trying to make a seekbar that gives both negative and positive values. I managed to make a seekbar as shown in the picture [1]: https://i.stack.imgur.com/tJZJN.png). However it start at 0 and finishes at 100. I would like it to start at -50 and finishes at 50. the code I have is right here. Hopfully someone can help .
thank you in advance.
Here is my code
public class MainActivity extends AppCompatActivity {
ArcSeekBar defaultSeekBar, seekBarBackground, gradientSeekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
defaultSeekBar= (ArcSeekBar) findViewById (R.id.defaultSeekBar );
defaultSeekBar.setOnProgressChangedListener(new ProgressListener() {
#Override
public void invoke(int i) {
Log.d("VALUE",""+i);
}
});
}
}

Wouldn't that be setMin(x) and setMax(x) ? In addition to doing this programmatically you could also apply it within the layout itself.
Refer to (bookmark this site and always explore what APIs exist within the class you are using):
https://developer.android.com/reference/android/widget/AbsSeekBar?hl=en#setMax(int)

Related

Can't get a game loop to work in Android Studio

So I understand the basics of java programming but when I'm trying to use my little knowledge in android studio it make everything harder having classes and different files needing to be referenced. Coming from python, when making a simple game I would define different functions, then run them in a game loop like
while running:
or something similar. I know to define something in java you go like
public void Example() {}
but when I use this in java, when I try to run the program my game either instantly crashes or doesnt load anything.
The code at the moment is
public class MainActivity extends AppCompatActivity {
//Variables
Boolean running = true;
public int years = 0;
//Setup Year Counter
TextView textView = (TextView) findViewById(R.id.year_counter);
//Advance Button
public void advance() {
ImageButton button = (ImageButton) findViewById(R.id.advance);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
years += 1;
textView.setText("" + years + "");
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Game Loop
while (running) {
advance();
}
}
}
And this results in the app not opening.
Any help at all would mean a lot to me.
Thanks in advance :)
Although I don't actually see a crash, since you didn't really upload one, I can see why you might think your app wont work.
What you are doing constantly in the while loop is that you are only setting the button's click listener over and over again.
public class MainActivity extends AppCompatActivity {
//Variables
Boolean running = true;
public int years = 0;
//Setup Year Counter
TextView textView;
ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// you set up your views once, after the layout is inflated
setUpViews();
// you initialize your buttons functionality
initClickEvents();
//Game Loop
while (running) {
// do other stuff
}
}
private void setUpViews() {
textView = (TextView) findViewById(R.id.year_counter);
button = (ImageButton) findViewById(R.id.advance);
}
private void initClickEvents() {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
years += 1;
textView.setText("" + years + "");
}
});
}
}
I'd recommend working with a game engine.
If you want to stick with java, libGDX is an option.
But if language (and IDE) doesn't matter, than a better option is Godot. The reason why I recommend Godot over some of the more popular game engines is because it's open source, 100% free and plus GDScript (godot's scripting language) is heavily influenced by python.
If you want to make you own game engine in java check out this: https://java-design-patterns.com/patterns/game-loop/#
Keep in mind that the while loop is calling the advance method. So every loop you are setting the view for button and then setting an OnClickListener for it. Not everything needs to be in the while loop.
You must implement (override) the render method (it is called in the game loop).
I suggest trying a complete example in the documentation.
``
#Override
public void render() {
ScreenUtils.clear(0, 0, 0.2f, 1);
advance();
...
``

Android: App crashes on calling toast from another class [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
I am a novice Android Developer.
I have created a package-private class which extends Application, and contains the required code for specific functions. I basically want to display if the user-selected button is the correct choice or not, via a toast. Since I have to call this code for many activities, I just created a package-private class for it. However, on clicking the button, the app crashes. Please see the code given below for reference.
I cannot change the onClick method to non-static because if I do that, Android Studio shows an error, and if I change it to static, I am unable to use the method getApplicationContext(), because it is not accessible inside static blocks.
I think that using view.getContext() is causing the crash.
Is there any workaround, or a solution?
Your help would be greatly appreciated. Thanks :)
Here is the code for your reference.
activity.java:
public class activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(functions.select);
functions.makeLayout(expression, buttons);
}
}
Here is the code which crashes the app.
functions.java:
class functions extends Application {
private static int idx;
public static View.OnClickListener select=new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_index=(int) view.getTag();
if(selected_index==idx)
{
Toast.makeText(view.getContext(), "Correct.", Toast.LENGTH_LONG).show();
((Button) view).setTextColor(Color.GREEN);
}
else
{
Toast.makeText(view.getContext(), "Wrong.", Toast.LENGTH_LONG).show();
((Button) view).setTextColor(Color.RED);
}
}
};
Okay, I figured out that it was not view.getContext() but the line int selected_index=(int) view.getTag(); which was causing the crash. I solved it first making it into a string and then int by using the following code:
String selected_index=view.getTag.toString();
int sidx=Integer.parseInt(selected_index);

android onclick () not responding

I am new to android programming and trying to develop TIC TAC TOE game. I have created gameLogic() method and the problem is this that it is not working as it is expected to do, means on click of ImageView none of the images is getting displayed. Any help will be highly appreciated.
This is my code:
public class MainActivity extends AppCompatActivity {
public void gameLogic(View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
public void onClick (View view) {
ImageView tappedView = (ImageView)view;
tappedView.setTranslationY(-3000f);
tappedView.setImageResource(R.drawable.black);
tappedView.animate().translationYBy(3000f).setDuration(500)
Try changing the class name to onClick and go to XML the page where you designs the button and declare onClick method in the image view button.
All you have to do is let the design page know where the code is by providing a common name to look for which is on click.
Also you have to add an onClick listener which listens for the button to click as the code should know which button it is linked to.
So declare a setonclicklistener method on the button Id and you're set to go.
Also write the code within the override after the setting of the layout.
Hope you understood :)
If you're new try head first Android it'll be really helpful I know how struggling it is during the initial days.

User Input Time - Android [duplicate]

This is my first app and I'm having some trouble.
When I run the app it crashes and I don't know how to fix this error.
public class MainActivity extends AppCompatActivity {\
TextView outputBottom = (TextView)findViewById(R.id.output);
}
public void play_the_big_lie(View view) {
the_big_lie.start();
outputBottom.setText("ObamaCare, the big lie");
}
String literal in setText cannot be translated
This is not an error and you can safely ignore it (unless you need translation in your app).
It is simply a notification by Android Studio that you should be using a string resource file instead of a hard-coded string.
If I had to guess at the source of the issue since you did not post the logcat, it is this.
You can't use findViewById before setContentView because there is no view to "find".
Please try something like the following code
public class MainActivity extends AppCompatActivity {
private TextView outputBottom;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.output);
}
There are two issues here. First, you can't use findViewById until you have "created" things and have a view to find things with, so as the previous answer you want to separate them.
public class MainActivity extends AppCompatActivity {
private TextView outputBottom;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
outputBottom = (TextView)findViewById(R.id.output);
}
...
}
To set text, it's better not to "hardcode" with a string in quotes, but to create a string file and ask for it, which will look more like this:
outputBottom.setText(R.string.my_words);
Here are the developer notes on strings.
If you're using Android Studio there are tutorials for how to make that happen.
You can ignore these warning by adding
lintOptions {
disable 'MissingTranslation'
}
to the gradle.build file.

How to replace deprecated code in class derived from PreferenceActivity? [duplicate]

I just noticed the fact that the method addPreferencesFromResource(int preferencesResId) is marked deprecated in Android's documentation (Reference Entry).
Unfortunately, no alternative method is provided in the method's description.
Which method should be used instead in order to connect a preferenceScreen.xml to the matching PreferenceActivity?
No alternative method is provided in the method's description because the preferred approach (as of API level 11) is to instantiate PreferenceFragment objects to load your preferences from a resource file. See the sample code here: PreferenceActivity
To add more information to the correct answer above, after reading an example from Android-er I found you can easily convert your preference activity into a preference fragment. If you have the following activity:
public class MyPreferenceActivity extends PreferenceActivity
{
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.my_preference_screen);
}
}
The only changes you have to make is to create an internal fragment class, move the addPreferencesFromResources() into the fragment, and invoke the fragment from the activity, like this:
public class MyPreferenceActivity extends PreferenceActivity
{
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.my_preference_screen);
}
}
}
There may be other subtleties to making more complex preferences from fragments; if so, I hope someone notes them here.
#Garret Wilson Thank you so much! As a noob to android coding, I've been stuck with the preferences incompatibility issue for so many hours, and I find it so disappointing they deprecated the use of some methods/approaches for new ones that aren't supported by the older APIs thus having to resort to all sorts of workarounds to make your app work in a wide range of devices. It's really frustrating!
Your class is great, for it allows you to keep working in new APIs wih preferences the way it used to be, but it's not backward compatible. Since I'm trying to reach a wide range of devices I tinkered with it a bit to make it work in pre API 11 devices as well as in newer APIs:
import android.annotation.TargetApi;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class MyPrefsActivity extends PreferenceActivity
{
private static int prefs=R.xml.myprefs;
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try {
getClass().getMethod("getFragmentManager");
AddResourceApi11AndGreater();
} catch (NoSuchMethodException e) { //Api < 11
AddResourceApiLessThan11();
}
}
#SuppressWarnings("deprecation")
protected void AddResourceApiLessThan11()
{
addPreferencesFromResource(prefs);
}
#TargetApi(11)
protected void AddResourceApi11AndGreater()
{
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PF()).commit();
}
#TargetApi(11)
public static class PF extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(MyPrefsActivity.prefs); //outer class
// private members seem to be visible for inner class, and
// making it static made things so much easier
}
}
}
Tested in two emulators (2.2 and 4.2) with success.
Why my code looks so crappy:
I'm a noob to android coding, and I'm not the greatest java fan.
In order to avoid the deprecated warning and to force Eclipse to allow me to compile I had to resort to annotations, but these seem to affect only classes or methods, so I had to move the code onto two new methods to take advantage of this.
I wouldn't like having to write my xml resource id twice anytime I copy&paste the class for a new PreferenceActivity, so I created a new variable to store this value.
I hope this will be useful to somebody else.
P.S.: Sorry for my opinionated views, but when you come new and find such handicaps, you can't help it but to get frustrated!
My approach is very close to Garret Wilson's (thanks, I voted you up ;)
In addition it provides downward compatibility with Android < 3.
I just recognized that my solution is even closer to the one by Kevin Remo. It's just a wee bit cleaner (as it does not rely on the "expection" antipattern).
public class MyPreferenceActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
onCreatePreferenceActivity();
} else {
onCreatePreferenceFragment();
}
}
/**
* Wraps legacy {#link #onCreate(Bundle)} code for Android < 3 (i.e. API lvl
* < 11).
*/
#SuppressWarnings("deprecation")
private void onCreatePreferenceActivity() {
addPreferencesFromResource(R.xml.preferences);
}
/**
* Wraps {#link #onCreate(Bundle)} code for Android >= 3 (i.e. API lvl >=
* 11).
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void onCreatePreferenceFragment() {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new MyPreferenceFragment ())
.commit();
}
}
For a "real" (but more complex) example see NusicPreferencesActivity and NusicPreferencesFragment.
Instead of exceptions, just use:
if (Build.VERSION.SDK_INT >= 11)
and use
#SuppressLint("NewApi")
to suppress the warnings.
Instead of using a PreferenceActivity to directly load preferences, use an AppCompatActivity or equivalent that loads a PreferenceFragmentCompat that loads your preferences. It's part of the support library (now Android Jetpack) and provides compatibility back to API 14.
In your build.gradle, add a dependency for the preference support library:
dependencies {
// ...
implementation "androidx.preference:preference:1.0.0-alpha1"
}
Note: We're going to assume you have your preferences XML already created.
For your activity, create a new activity class. If you're using material themes, you should extend an AppCompatActivity, but you can be flexible with this:
public class MyPreferencesActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_preferences_activity)
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, MyPreferencesFragment())
.commitNow()
}
}
}
Now for the important part: create a fragment that loads your preferences from XML:
public class MyPreferencesFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.my_preferences_fragment); // Your preferences fragment
}
}
For more information, read the Android Developers docs for PreferenceFragmentCompat.

Categories

Resources