How to use an integer resource in Android? - java

I'm trying to use an integer stored in a fikle in the values folder but something isn't woring right. Here's the xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="numb">5</integer>
</resources>
And here is the main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("testLog", R.integer.numb+"");
}
But when I run it the output is:
04-02 14:08:30.044: D/test(14839): 2131099648
Why do I get get 5 instead?

Use
int value = getResources().getInteger(R.integer.numb);
For more information check the docs under the topic Integer.
http://developer.android.com/guide/topics/resources/more-resources.html#Integer
Quoting docs
Resources
The Android resource system keeps track of all non-code assets
associated with an application. You can use this class to access your
application's resources. You can generally acquire the Resources
instance associated with your application with getResources().
getResources() is a method of Context.

When you are accessing values from resources you need to use getResources() method like below,
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("testLog", getResources().getInteger(R.integer.numb) + "" );
}

Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("testLog", getResources().getInteger(R.integer.numb)+"");
}

Use in Activity:
int numb = getResources().getInteger(R.integer.numb);
Use in Fragment:
int numb = getActivity().getResources().getInteger(R.integer.numb);

Related

findViewById for view inside an included merge root-element

I want to use a view in multiple activities. In my case it's a FloatingActionButton. The moment I am searching for the view via findViewById the program throws a NullPointerException. How do I get access to the FloatingActionButton?
As the button should always be the same, I created a XML Layout only including the FloatingActionButton inside a mergeroot element to reduce overhead when using ìnclude.
floating_button.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/include_merge" >
<android.support.design.widget.FloatingActionButton
android:id="#+id/addBloodDonation"/>
</merge>
To use this in my other XML Layouts I use the include tag
XML Activitiy Layouts that should include the FloatingActionButton
<include
android:id="#+id/include_merge"
layout="#layout/floating_button" />
That works so far.
To have the same functionality in all my Activities, I created a BaseActivity which my other classes inherit from, e.g. my MainActivity.
BaseActivity
public abstract class BaseActivity extends AppCompatActivity {
public FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View include_merge = findViewById(R.id.include_merge);
fab = include_merge.findViewById(R.id.addBloodDonation);
}
}
MainActivity (exemplarily)
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
When starting the application I receive a NullPointerException in BaseActivity for trying to find the inner element as the View variable include_merge is null.
As I read here the include tag and the root element should have the same android:id. Is there a difference when using a merge as root element? And is it even possible to convert a merge tag into a View.
Do I need to make use of setContentView in the BaseActivity as its onCreate method is called before the one of MainActivity?
Edit:
Added setContentView(R.layout.activity_main); to BaseActivity as mentioned in the comments which still does not fix it.
You don't have to set an id to the <include> or <merge> tag. Just remove it as well as findViewById(R.id.include_merge). The <merge> tag indicates that all its views are added to the container of the <include> tag. Thus there's no View with the id you've set to it. But you can directly find the FAB.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = include_merge.findViewById(R.id.addBloodDonation);
}

Layout rejects to read TextView,ImageViews,Any view at all

The xml code: -->Also tried with ImageView and buttonView, Picker, and so on, it won't read data from this xml.
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is a textview."
android:background="#f00"></TextView>
Java code where i set the view to the activity containing this XML.
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
Now, what i SHOULD see is this:
What i REALLY see is this:
Also i have declared the java in the android Manifest.
maybe it'll help but This activity is reached by pressing 2 buttons from 2 different activities, I've tested the same logic with a new project and it works, but it doesn't work on my main project.
Make sure that the part where you start your activity with Intents is right.
It should be something like this :
Intent i = new Intent(context, YourActivity.class):
startActivity(i);
if there's no problem with that, try checking your layout's name for any chance of conflict.
At the last try changing your OnCreate to :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
Looks like there is no problem in your XML, but also check the root of your layout.
ANSWER: There was some weird bug i myself don't understand, i removed the java code and re-added it again, and now it suddenly works, Thanks for suggestions everyone.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}

Extending library activity doesn't call onCreate

I have added a project 'A' as library in my project 'B'. Now in project 'B', I have an activity 'MainActivity' which extends an activity 'SplashActivity' of project A.
When i'm running the app, the onCreate of 'MainActivity'(Project B) is never called, rather the OnCreate of 'SplashActivity'(Project A) is called every time. What could be the problem?
Also what if I want to call the onCreate method of 'SplashActivity'(Project A) and then the onCreate of 'MainActivity'(Project B), is it possible?
The code is given below:
public class MainActivity extends SplashActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();//never called
}
}
change your xml ref
setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();
to
setContentView(R.layout.activity_main);
SplashActivity splashactivity = new SplashActivity();
splashactivity.setLogo();
I solved my own answer ,and thanks to #jimit patel and # ρяσѕρєя K , I solved it and and if anyone looking for an answer
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_splash_activity_temp);
ImageView imageView = (ImageView) findViewById(R.id.splash_logo);
super.setLogo(imageView,this);
super.onCreate(savedInstanceState);
}
Just call the super.onCreate at the end of the code , and the code will work just as expected . :)

TextView doesn't show the required text

My MainActivity class has an if that checks is something is true it starts a new Activity which sets the text of a TextView to what I want.
MainActivity:
public static int caseA = 1;
if(caseA) {
Intent i = new Intent(Timer.this, WorkTimerNotification.class);
startActivity(i);
}
then it goes to the new Activity and it should check the value of caseA.
WorkTimerNotification:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
TextView timerMetric = (TextView)findViewById(R.id.tester_texter);
if(MainActivity.caseA) {
timerMetric.setText("min");
}
Problem: It does not change the text. Also, lets say I have many other if statements in mainactivity like:
if(caseB) {}
if(caseC) {}
//and so on..
How would I perform the checks? More importantly, how do I get this caseA check to work though.
You need to call findViewById() in onCreate():
private TextView timerMetric;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
timerMetric = (TextView)findViewById(R.id.tester_texter);
if(caseA) {
timerMetric.setText("min");
}
}
Right now you are calling it before calling setContentView(), before the view hierarchy is generated. For this reason, your code fails.

App crashes when setting layout background

I'm trying to set a drawable resource as background for my main relative layout via java, but whenever I do it, my app crashes.
Here is the part of the code which doesn't work fine:
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) findViewById(R.layout.activity_game);
setContentView(R.layout.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Any idea?
Thanks in advance
Call findViewById() only after setContentView() and supply an identifier in R.id (and not R.layout) so that it can return something other than null.
Remember,you are trying to find a view by id but what you are doing is actually trying to inflate a layout the wrong way.Change the R.layout.activity_game to R.id.activity_game and make sure the relative layout is givent the
android:id = "#+id/activity_game"
The full code should be
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout = (RelativeLayout) findViewById(R.id.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Hope it helps.Happy coding.

Categories