Request Window Feature from another class - android

I want the screen set to (Window.FEATURE_NO_TITLE). I know that I can put requestWindowFeature(Window.FEATURE_NO_TITLE); in the OnCreate method but I have another Class let's say Class B which is extended to the Fragment and it has a button in the layout. I want when that button is clicked in clicked in Class B, the Window should be shown without the Title. I tried as below:
AnandSahib anad = new AnandSahib();
anad.requestWindowFeature(Window.FEATURE_NO_TITLE);

I think you cant do it.you should call anad.requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView().please See this Android Developer site

Related

startActivityOnResult() in recycler view onitemClick()

so I need to create a notes app, in which user opens and sees a RecyclerView-grid of notes. on clicking any note, it opens a new activity SecondActivity with 2 edit texts title and data.
On closing secondActivity, I want the user to see the title he had set in secondActivity on the grid he clicked on the main page.HOW?
After searching for a white, i got this startActivityForResult(). i understood its working, but it is not available in the adapter/holder, where my onitemclicklistener is present.
So how can i get to recieve this title? can I use the saveInstanceState bundle for this work?I also researched and observed that a child Acticvity's(SecondActivity) onSaveInstanceState is not called when the secondActivity is destroyed. so maybe that bundle thing would be a wrong path.In my OnBindHolder() function, i was using myholder.itemview.getContext().startActivity(...,...) for calling the secondActivity. I also tried passing the context for main Activity in adapter and using it for starting activity, but it still didn't show startActivityForResult() ...
UPDATE:
here are the java classes from my app(I have explained in detail about my problem in a comment in Main Activity.java):
MainActivity.java
Details.java
(Reycler View Files:)
RVadapter.java
RVholder.java
RVdata.java
RVfeeder.java
githubLink
You need to cast Context to Activity because it has startActivityForResult.
((Activity) context).startActivityForResult()
or
(((Activity) itemView.getContext()).startActivityForResult()

Dynamically show Activity as dialog

I have an Activity that I have already implemented sometime ago.
It involves around making a in app purchase, so all the logic is relatively self contained. it doesn't need to care about anything else.
Now, i wish to make that Activity to optionally show up in a dialog in some other activity. Is there a quick way to do that? I still need to keep the old behavior however, where the activity show up as a regular screen.
So is there someway that I could launch the activity with that make it show up as a dialog?
Thanks
You cant show activity as dialog.
Your options are:
1: Open the other activity with some boolean extra like "showDialog", true
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("showDialog", true);
and in the other activity in (for example) onCreate:
Boolean showDialog = getIntent().getExtras().getBoolean("showDialog");
if (showDialog) {
// Code to show dialog
}
2: Create a DialogFragment and show it in your original activity. This custom DialogFragment you can use on both activities
https://guides.codepath.com/android/Using-DialogFragment
Probably your cleanest option depending on how complex your Activity is, is to create a new DialogFragment based on your current activity.
A DialogFragment is basically a Fragment, so has a relatively similar set of lifecycle callbacks to your Activity so it shouldn't be too difficult to re-work as a DialogFragment.
If the in-app purchase framework has specific callback requirements with an Activity then you will need to take that into account.
Another separate option would be to mock the appearance of a Dialog, by creating an Activity that may be transparent around the border of the main content.
Just Inflate the layout one button click on onCreate Method.
WhAT I WILL SUGGEST IS try alert box and in place of normal layout inflate you activity layout .
these might help
The easiest way to do that is to apply a dialog theme to the activity:
<activity android:theme="#style/Theme.AppCompat.Dialog" />
Or in the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog);
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
You can customize parameters of the theme in styles.xml, e.g. dim enabled/disabled, click outside behavior.
The crucial point is to perform setTheme() before super.onCreate(), because Theme is immutable, once set through super.onCreate() it cannot be mutated later.

Show View in all my app

I need show a TextView in all Activities, but is much work to do it one by one, because I have +10 Activities.
My objective is when I click in a button, show a textview ("Importing ...") at the bottom of the application. This textview will disappear when I receive a push notification, and I owe a pop up with the response (the pop up also has to appear in any activity).
My project has a custom abstract BaseActivity and all activities extends it.
public abstract class BaseActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void setActionBar(#IdRes int idResToolbar) {
Toolbar toolbar = (Toolbar) findViewById(idResToolbar);
setSupportActionBar(toolbar);
updateFont(toolbar);
}
// ...
}
I think I could use for my purpose but not how to do it.
If anyone has any suggestions I will be happy to hear it.
Thanks in advance.
Use fragments for your content (instead of different activites) you then can add global views to the activity, which holds the fragments.
If you don't want to do that, you'd have to modify the layout(s) in your Base class.
I would suggest you to use a PopupWindow that contains the text view and create a separate class that initializes the PopupWindow on the basis of context given to it.
Now in all your Activities you will have the control of showing and hiding the window as you want. Make sure to make all utility methods required in the separate class to avoid coherence for example hiding and showing the window. setting text of text view of the window and etc.
You can write in onCreate() of your base activity something like
setContentView(R.layout.base_layout);
And in every other Activity at start of onCreate() method, just use super.onCreate()
And more than that to support different layouts add something like this in onCreate() (example for one of activities)
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.activity_1_layout,rootGroup)
where rootGroup is a ViewGroup in your Base Activity, in which you will add additional components for every other activity
Create a service, which creates a View which can be drawn over other apps (will require the relevant permission in the manifest)
You could use one of the open source libraries available like this or refer to this example
It's better you use fragments instead of using many activities. However, if you don't wanna do so, I suggest you create a factory which will generate a textview to all activities. Then you must add it into each activity's view.

One class for each layout (new page)?

I wast just wondering if the standard practice is to create an activity/fragment class for each layout file (new page).
Example:
MainActivity.java
onCreate(){
setContentView(R.layout.**start_page**)
}
And than when the user clicks a button in the action bar (or some other button on the screen):
onOptionItemSelected() {
switch XX -> case XX: setContentView(R.layout.**next_page**)
}
So could i do the above instead of launching a new activity.java (that contains a new layout.xml) with an intent, or inflating the view with a fragment.java (that also contains a new layout.xml).
I can see that the up/back navigation wouldn't work with the above code, but is that the only reason why you basically have to create two files (.java & .xml) for each new page in your app.
Yes you could do it technically but beware that if you already create an instance of view lets say Button and you change the layout button will be null because button is not located in your View and also it will take time to render again the layout. So it is a best practice to start a new activity or just create a fragment.
You can do that, but every view will be on the given Activity, and the event handlers would be in the same class, which isn't really modular. It could get extremely bloated and you'll have a 2000 line superclass because it handles every single button click in arbitrary functions (or even worse, in a single onClick function).

how to use the widgets assigned in a activity in another activity?

I am creating an android project . In that i i got the ids from the XML file and wrote the click events in a (class or activity) . I want to use the widgets from another class without getting the id's again. like (Button btn=(Button)findViewById(R.id.button1);) i want to use this code in one class
But I want to use the Button btn in another class and also the click event should work.
If you want to use any View in more than one Activity you can create a class BaseActivity that extends Activity and include all the common Views in that Activity and then extend this BaseActivity to all the Activity in which you want to use common views/layouts/headers/footers. For a Pseudo code you can check my answer here.

Categories

Resources