Android moving 'windows' - java

I'm new to android and I'm need a little help understanding how to move from one window to another. I know i use setContentView(R.layout.main) to load an xml layout file, but how do i swap to another layout file? I assume i would use an onClick method on a button and change setContentView(R.layout.other_layout), but would doing all this inside my main activity make my code cluttered? I could end up having 10000+ LOC easily. Can someone explain the correct way to do this please. Thanks

Intents allow us to call another activity from our present activity. For example our current activity is Act1 and we want to move to another activity, Act2. this can be done as:
Intent i = new Intent(Act1.this, Act2.class);
startactivity(i);
Refer http://developer.android.com/guide/topics/intents/intents-filters.html for more information on intents and activity.
Another option is to call setContentView() 2nd time to change the layout.

You use Intents to launch other Activities.
In your current Activity (i.e. window), you can do the following code to launch a new Activity
Intent i = new Intent(this, NewActivityName.class);
startActivity(i);

You should create Activities. Activity is equivalent to window/frame concept on desktop. Each activity should have a goal towards user interaction, ie. taking inputs and showing output. In your case create two activies, and both of them should have their own layour XML and a call to setContentView() within onCreate().
On button click use startActivity() to invoke a new one. Keep in mind that these activities are stacked top of one another.

A visible screen in Android is represented by an Activity. So instead of loading a different layout file into the same activity, you simple create a new activity with it's own layout and java file.
In order to call this second activity from the first one, or to communicate between activities in general android uses so called Intents.
Just look that chapter up in Android's Dev-Gui.

Related

How to change layout on button click in android app?

I am new in Android so correct me if i am wrong. When another activity is opened, the first one is destroyed. So i have to pass all variables to the new activty if i don't want to lose the data. Or, can I run another not UI thread that manages my data?
Basically I need to change layout in my app on button click. Is there any way to do it within the same activity or i have to start another activity with the new layout?
You can use one activity in your app and do what you want in fragments. Navigation is good tool to use for relating fragments to each other.
well initially you can use a single activity and in that activity you can call multiple fragments as per your need,
and for the data you can use MVVM architecture with room architecture, it saves your lots of time and code.
and go with navigation graph if you want a quick and easy implementation.
you can start all this with this demo.

Can use the intent-filter in android to start a method

me and a partner are working on a project and have designed an app that uses nfc, we want it so that when you're off the app you can tap an nfc card and read it as well, the problem is we don't specifically have an activity for reading or writing a card, its triggered using a button on main activity that starts a dialog fragment, starts a class and reads the card
is it possible, using the intent filter (or any other way without having to create an activity for it) trigger a method in the activity? at the moment we just have it to bring you to the main activity but we want to start a method in main activity when that happens.
Intent filters start Activities. Activities always start with onCreate. If you want to call a single function in your activity, your code is mis-architected and that function should be in a common helper class, not in your activity.

How do you make a button in android that opens a new activity and this activity is not just a default activity its a custom one

I am trying a make notepad app so every time a new note is opened the layout will be the same. also, the number of activity(new note) should not be defined as many possible
If the activity is always the same, you should probably create an adapter that allows you to change the texts and images of your activity without the need to create several activities.

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html

activity changing in android

I am making a game for Android, but I never really found a good way to change the activities, or the content views. I have 1 main menu activity now, whose content view receives MotionEvents and dispatches them to my custom buttons. Another Activity has a simple contentView which just paints the screen in one color. All contentViews have the same base class and activity 2 is derived from activity 1.
The problem is, that the app just crashes if a try to change the activity. It takes about 20 seconds, then the error message appears that says the app isnt responding.
In logcat, theres also a message keyDispatchingTimedOut sending to activity2
Below is the code for activity change:
public void changeActivity() {
Log.d("changing", "activity");
Intent i=new Intent(this, Activity_Level.class);
startActivity(i);
}
Any ideas?
You have to use context of your activity in method
Intent i=new Intent(Youar_Activity_Name.this, Activity_Level.class);
^^^^^^^^^^^^^^^^^^^^^^
Use above code in your changeActivity() method.
if the app isn't responding , it's because you do a long operation on the UI thread . maybe after calling this function you continue to do something else ?
if , as people said , the activity isn't opened (and you can check it by writing to the log inside the onCreate method) , check the manifest.
in any case , if you want to have better control of activities , you can check the possible flags to use for the intents , and you can also use fragments (when possible) , just like google recommends .
Without seeing the logcat message, I bet you forgot to add the activity in AndroidManifest.xml
I sense that you didn't add all the activities to the manifest.xml file. Try to add all the activities there, and give it a run.

Categories