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);
}
Related
I'm creating an App for an Android Wearable Device.
When showing a custom layout as a dialogue fragment, the app will get minimized (getting replaced by the watch face) after about 30 seconds. I know that this is supposed to be the default behavior of apps out of touch with their users, but in my case, the app needs to stay visible even if not touched four minutes.
The Activities calling it did get these instructions inside their onCreate implementation.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
that works like a charm and in the way I want it. However, the DialogFragment that serves as an error notification (and may need to get observed without touching it for some time) does not obey this setting.
I tried to get the flag inside the DialogFragment too by placing it inside the onViewCreated calls, but it does not have the getWindow Method. While the next code segment is valid, is does not work either.
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The way the dialog fragment is called looks like this. That's the code to call it from the FragmentActivity needing to show it. As you might notice, there is a field "activity", which is because the whole call is done from a static class outside the activity. I want to call the same DialogFragment from multiple activities, with only the text and the title being different.
public static void showDialogCuston(String title, String message, FragmentActivity activity){
ErrorDialogFragment edf = ErrorDialogFragment.newInstance(title,message);
FragmentManager fm = activity.getSupportFragmentManager();
edf.show(fm, "TAG");
}
And that's what the DialogFragment does look like inside. I kicked out all TextView text assignments because I doubt that they could offer any kind of information about that request, and just making the code fragment less readable.
public class ErrorDialogFragment extends DialogFragment {
//some private text views
public ErrorDialogFragment(){
}
public static ErrorDialogFragment newInstance(String title, String text){
ErrorDialogFragment edf = new ErrorDialogFragment();
Bundle args = new Bundle();
args.putString("title",title);
args.putString("text",text);
edf.setArguments(args);
return edf;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_error, container, false);
// R.layout.fragment_error is the layout that serves as my custom dialog
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//assigning layout elements to private fields
//assigning stuff from the bundle inside some textViews
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
//playing a sound from SoundPool and doing haptic feedback on button press
}
});
}
}
So, all I want is that the app won't disappear by itself when one is staring too long on the DialogFragment without touching it.
In your case, I would like to suggest keeping the screen-on flag in the dialog layout file so that when the dialog view is visible, it keeps the screen on. Check the developer's documentation for more information. However, I am adding the code from the documentation for convenience.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
Set the android:keepScreenOn to true in the root element of your dialog layout. And I hope you also have the following permission in your AndroidManifest.xml file.
<uses-permission android:name="android.permission.WAKE_LOCK" />
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);
}
I'm trying to execute more then one layout screens from single class using onClick() method
Here goes my code
Button bt1,bt2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2 = (Button)findViewById(R.id.button1);
onClick(); //onClick(View) in MainActivity cannot be applied to ()
}
public void onClick(View v){
if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
else if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
}
Kindly help me out, Thank you
Fragment is what you have to use, in this case. Load either of the fragment based on button click.
If you go for setContentView in this scenario, then your code will be clumsy and it will be very tedious for you to keep track on view.
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.
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);