Switching between layouts and keeping content displayed - java

In my main layout (mainlayout) I am displaying some text and images which are set dynamically based on the actions of the user. For one particular button click I need to display the contents of another layout (secondlayout). I do this using:
setContentView(R.layout.secondlayout);
On the second layout I have another button that I use to return to the main layout, once again using:
setContentView(R.layout.mainlayout);
The problem is on displaying the mainlayout again all the text and images I was displaying have now disappeared.
How can I return to the mainlayout and still display the contents I was displaying?

don't do it that way. setContentView() is meant to be called once in your onCreate() method. however, couple of reasonable ways to do it,
encapsulate each layout in a fragment, then show / hide each fragment as needed.
bundle both layouts into a single layout, and show / hide each section of the layout by calling setVisibility() on the layout's outermost container.

Related

Change linear layout to constraint layout by hitting a button

So the screen is going to have some textViews and a button, by hitting the button, the layout should change from linear to constraint, vice versa.
these two should have exact same paddings and alignments. I have my linear layout set up, how do I make an extra screen to perform constraint layout?
do I simply hide and unhide components?
or is there a way to overlap the components?
maybe create another layout.xml something like that?
You can make an copy of your linear layout.
Then open the copied new layout in design view, right click the root LinearLayout object in the hierarchy view, select "Convert LinearLayout to ConstraintLayout" from the popup menu.
I don't think it can create 100% same layout, but good luck!

setContentView() deletes elements which have been added with addView()

I've 2 layouts on my project. Both have a button to swap between each other using setContentView method. Whenever I swap between these 2 layouts, every UI element I've added using addView() is lost. However static XML elements remains.
That's because the layout is inflated anew, with all the views specified in the xml when you call setContentView(R.layout.xml), that's happening behind the scenes, and all the dynamically added views will be gone.
Optional solutions:
Add the views again after you call setContentView().
The 2 layouts can live on top of each other and you can toggle their visibility. use GONE to hide the layout, not INVISIBLE If the layouts have clickable elements on them.

Keyboard hides content in fragment, I need the frame layout to resize so bottom is just above the keyboard and now scrollable

I have a fragment in my app that has a scroll view for the signup and login pages. Right now there isn't enough content in the scroll view to actually make it scroll, however when the keyboard appears, it does cover up most of the content in the view. This causes a lot of issues especially on devices with smaller screens, it blocks a lot, and the view is NOT scrollable, so I have to close the keyboard to get to the rest of the inputs.
I need the bottom of the fragments frame layout to be pushed up to JUST above the top of the keyboard, so the keyboard won't actually hide any content, and still allow the scroll view to actually scroll to the rest of the content.
I have seen the usual fix to an issue similar to this, which would to change the AndroidManifest.xml to the following:
android:windowSoftInputMode="adjustResize"
but this will push up the entire page, which includes the footer view I have under and outside of the login and signup fragment layouts. It makes my scrollview smaller and allows for it to scroll, but I need the footer to stay hidden under the keyboard still.
I think a work around to this would be to have override onConfigurationChanged(); in MyActivity that will detect if the keyboard has appeared, and if it has, push the bottom of the framelayout to be JUST above the keyboard, thus making the scroll view smaller, and allowing us to actually scroll. I am not quite sure HOW to do this though.
Here is what it looks like with the keyboard up, blocking the content. This would be okay IF the scroll view was scrollable, allowing me to see the rest of the content, however it will not scroll and the only way to access the content under it is to close the keyboard first.
EDIT
I was able to use the answer below, editing the Android manifest for
android:windowSoftInputMode="adjustResize"
and the first method using the code below
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});
I had it adjust my views so the footer would be pushed way down below, then resize the layout holding the fragment to extend down allowing it to be scrollable still.
Okay, here's how I solved it.
The basic idea is that you have to:
Detect whether or not a soft-keyboard is showing,
React. Based on the detected information (is-soft-keyboard-showing), resize your layout accordingly.
There are two ways of achieving this:
to give your activity's root view a known ID, say '#+id/activityRoot', hook a GlobalLayoutListener into the ViewTreeObserver, and from there calculate the size diff between your activity's view root and the window size:
Customize your top-level layout class into one which overrides onMeasure()
And I would like to credit the above answer to this SO Post: how-to-check-visibility-of-software-keyboard-in-android, which I have found earlier on this particular problem.

Hide Activity with linear layout

I am inflating multiple same linear layouts from one activity, and I would like to hide all of these from another activity. How can I accomplish this?
what I tried:
LinearLayout test_layout=(LinearLayout)this.findViewById(R.layout.test);
test_layout.setVisibility(View.INVISIBLE);
But test_layout is always "null"
My Activity is "singleTask", so I think if I get hold of one view and close, it will close all similar views opened by this activity.

Android Update Layout Content

I'm looking for a way to update a layout's content with a new view. Is there any way to easily do this. It would be similar to how tabs work, but I don't want to have to get into extending the current tab structure if I don't have to.
The final result would be a few buttons that would switch the content in a specific linearlayout for each button.
Any help?
Have a look at ViewSwitcher. This is designed to help with the kind of things you are suggesting.
If you want the contents in entirely different layout files, you can use a LayoutInflater and add the inflated view to the parent view.

Categories

Resources