What I've been trying to do is create a menu that stays "static" no matter what activity or layout is being showed. Is there any way to do this, or anything similar to this??
Thanks!
Yes,
1- Create a custom Activity (extend it from Activity)
2- Write the code for menu creation in that custom Activty.
3- Now extend all your Activities from this custom Activity.
Related
I want to know is it possible to create menu like this?
Most likely, your screenshot is of an ExpandableListView, or possibly a RecyclerView that uses a library to add expandable contents.
Yes, and it's called Expandable List View.
Currently when I make a new layout I create a new class that handles that layout and then make a method to communicate with a class that the layout might communicate with, but can I use one class for everything. Like if I make a game can 1 class handle the Main Menu layout from which by pressing buttons you will be able to go to Options, High Scores and New Game. Can I do everything on a class ?
It's not a normal class that handles the layout. In Android, a class when extends an Activity or Fragment, it becomes one of the component of Android.
An Activity can render multiple layouts using setContentView() method. You can call this method with different layout as you click on different buttons.
BUT, this is a bad approach. To keep the same activity and change layouts, use Fragments. Fragments are part of an Activity that can have their own life cycle and can be attached or detached from Activity dynamically. They are mostly used for layouts for multiple sized devices. Read more about fragments here.
Here is tutorial of how to use Fragment in Android: https://www.raywenderlich.com/117838/introduction-to-android-fragments-tutorial
I have an activity which i want to open with multiple styles. There shall be a menu option to open it as a normal activity, but also the open to open it as a dialog-styled activity from another activity.
My current "hack": Define a new class by letting it extend the other one and leave it empty. So i have two identically classes with different names where one extends the other.
Is there a better method?
Set style of activity to android:style/Theme.Dialog.
Use a DialogFragment. It can be embedded in an activity, or shown as a dialog.
I need to programmatically create a DialogFragment layout, but I don't know how to proceed. I cannot use xml layout because the DialogFragment will be a part of closed-source JAR file.
Normal Dialogs accept an Activity in their constructor, so it is then possible to instantiate a new Layout like this: layout = new LinearLayout(this);. DialogFragments, on the other hand, do not usually take the Activity as a parameter, so I don't know how to perform this first step.
How should I create the layout?
Is it OK to ask for an Activity in the constructor?
Is creating a layout of DialogFragment any different from creating a layout of normal Dialog?
I will be grateful for any other advice regarding manual DialogFragment design.
How should I create the layout?
How ever you want it to look. If you can't/don't want to use a layout file that you inflate and return from the onCreateView method you'll have to build the entire view hierarchy of your new dialog in code.
Is it OK to ask for an Activity in the constructor?
There is no need for this, the Fragment will get a reference to an Activity, you'll have a reference to that context by using getActivity().
Is creating a layout of DialogFragment any different from creating a
layout of normal Dialog? I will be grateful for any other advice
regarding manual DialogFragment design.
No, it's not different. The documentation for the DialogFragment has a great example on how to build a custom DialogFragment, you should check it out.
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.