How to get Activity's content view?
this.getWindow().getDecorView().findViewById(android.R.id.content)
or
this.findViewById(android.R.id.content)
or
this.findViewById(android.R.id.content).getRootView()
How to get current content view of activity?
public class MainActivity extends AppCompatActivity {
Boolean isClose = true;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
}
public void onpressed(View view){
isClose = false;
setContentView(R.layout.second);
}
@Override
public void onBackPressed() {
if (isClose){
finish();
} else {
setContentView(R.layout.activity_main);
isClose = true;
}
}
}
How to get Activity's view?
The only thing that you need is set an id
of you root view and create an instance of it.
activity_main.xml
<LineaLayout
...
android:id="my_root">
...
</LinearLayout>
MainActivity
private LinearLayout mRootView;
onCreate(...){
...
mRootView = (LinearLayout) findViewById(R.id.my_root);
...
}
And, in you alert dialog just create a snackbar like:
Snackbar.make(mRootView, "Wrong password. Try again.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
How to get view from main Activity
There are quite several ways to do this :
this.Window.DecorView.RootView;
or
this.Window.DecorView.FindViewById(Android.Resource.Id.Content);
or
this.FindViewById(Android.Resource.Id.Content);
or
this.FindViewById(Android.Resource.Id.Content).RootView;
or
((ViewGroup) this.FindViewById(Android.Resource.Id.Content)).GetChildAt(0);
Although, since it's probably a layout, not a single view, I recommend using ViewGroup
instead of View
(these methods returns View
so you have to cast them to ViewGroup
if you want)
=================
Credit to this answer
Get root view from current activity
If you need root view of your activity (so you can add your contents there) use
findViewById(android.R.id.content).getRootView()
Also it was reported that on some devices you have to use
getWindow().getDecorView().findViewById(android.R.id.content)
instead.
Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).
If you need to get view that you added to your activity using setContentView()
method then as pottedmeat wrote you can use
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
But better just set id to this view in your xml layout and use this id instead.
How to get current content view in Android programming?
Citing Any easy, generic way in Android to get the root View of a layout?
This answer and comments give one method: [Get root view from current activity
findViewById(android.R.id.content)
Given any view in your hierarchy you can also call:
view.getRootView()
to obtain the root view of that hierarchy.
The "decor view" can also be obtained via getWindow().getDecorView(). This is the root of the view hierarchy and the point where it attaches to the window, but I'm not sure you want to be messing with it directly.
How to set the content view of a Activity from another actvity
I would add a callback to your Dialog that you simply invoke to set your content, something like
class MyDialog(val switchChangedCallback: (Boolean) -> Unit) : DialogFragment() ...
When you change the switch, invoke the callback and handle the result in the Activity:
val dialog = MyDialog(switchChangedCallback = { isOn ->
if (isOn) {
setContentView(R.layout.abc)
} else {
setContentView(R.layout.def)
}
})
dialog.show(supportFragmentManager, MyDialog::class.java.name)
You may need to check that the callback survives app rotation!
Go back to activity contentView after having set contentView to web view in android
Dont call setContentView twice. You should put your webview in activity_login file like:
<WebView android:id="@+id/webview"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="invisible"/>
And when the theLogin Button clicked do your operations. Also dont forget set visibility for your webview.setVisibility(View.VISIBLE).
hope it helps
Related Topics
How to Programmatically Enable/Disable Accessibility Service in Android
Androidruntime: Fatal Exception: Androidmapsapi-Zoomtablemanager
How to Get the Current Orientation (Activityinfo.Screen_Orientation_*) of an Android Device
Android Listview with Onclick Items
Android Communication Between Two Applications
Android Detect Done Key Press for Onscreen Keyboard
Exception:Aapt2 Error: Check Logs for Details
How to Cancel an Dialog Themed Like Activity When Touched Outside the Window
Getting the Dimensions of the Soft Keyboard
Add a Search Filter on Recyclerview with Cards
List All Camera Images in Android
Disable Sound from Notificationchannel
Cannot Resolve Method Setlatesteventinfo
Android: How to Share Image with Text on Facebook via Intent
Edittext Seterror() with Icon But Without Popup Message
Hide Ics Back Home Task Switcher Buttons