Open activity in normal mode AND as dialog - java

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.

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.

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.

android 2 activities on the same screen

I've a tabBar with 4 tabs, the Main tab is the first one, and when I click the second one I want do display another activity above the main activity in the half of the screen, reducing the opacity of the main. I'm doing this with a Dialog but it's not the result I want. What are the best way to do this?
example image
Look at Fragments, this seems like the perfect time to use them.
There are only 3 options to achieve what you want.
Second activity can be dialog (You already said you do it but you don't like).
Using fragments. (recommended)
Using layouts by playing with visibility, (not recommended in your case)
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities

How to make one part of activity always visible on Android?

I've created ListActivity and other Activities, which I would like to be displayed when I click item from ListActivity. My problem is that I don't know how to do it in the same window, so that the ListActivity will stay always visible, while other part of the window will change based on the pressed element.
You would have to have only one activity, such as by converting "other Activities" into fragments that are displayed by the ListActivity.
Instead of using Activities, I would implement your functionality in Fragments. They allow for increased modularity of an application, and will allow for the functionality that you desire, at runtime they can be "swapped out" with inside your ListActivity.
Reference to a thorough overview of Fragments on the Android Developer website: http://developer.android.com/guide/components/fragments.html

Android moving 'windows'

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.

Categories