Change linear layout to constraint layout by hitting a button - java

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!

Related

Limit the click area on the view android

I need to limit the click area on the view. For example, I have a full-screen view, but I need only the top of the screen to be clickable, while clicking on the bottom should not give a result, how can I implement this? adding on top of the container doesn't help
You will need to create a click listener ONLY on the view you want to respond.
TopView.setOnClickListener()
BottonView (nothing).
If for some reason, your top View is not independent, just create another view that is transparent, and set a clickListener on that.

android - slide relativelayout to the side with static buttons

In my Activity I have two RelativeLayouts and two buttons. When you first open the Activity, you have to see a RelativeLayout and the buttons. Then you can slide the RelativeLayout to the right or to the left. The buttons don't move. While sliding, you see the other RelativeLayout below. After a completed slide you see the full RelativeLayout that was below.
How can I implement this in my Android application (min SDK version 14)?
Code examples are a big plus, but not necessary. It is ok to show me the way :)
There's something called a navigation drawer which comes as a template in Android Studio. There are also many sliding menu libraries out there: https://android-arsenal.com/search?q=sliding specifically you might look at https://android-arsenal.com/details/1/1429
If you want to do it from scratch, it'll go something like: have one layout, say a Frame Layout as your root. Then you add the two Relative Layouts and your buttons. Then you need to implement a OnTouchListener in your activity and set on the relative layout that can slide: when the MotionEvent is ACTION_MOVE, you need to calculate the difference between the last touch event and the current touch event and then you can call translateX(dx) on the sliding relative layout where dx is the calculated difference.

Android: Using both adjustPan and adjustResize doesn't work but I need both

I haven't officially decided if I want to head this route or not but I have an adview in my XML document. The XML document contains a Relative layout that contains a ScrollView and and Adview. The Adview sticks to the bottom of the relative layout while the scrollview is everywhere else.
When a user focuses on an edittext inside of my app, some of the page would be cut off. I fixed that by adding:
android:windowSoftInputMode="adjustPan"
But then the problem became that my AdView gets cut off everytime the user has the keyboard open. So to fix that issue I changed the Android Manifest to:
android:windowSoftInputMode="adjustResize"
Which kept the AdView on top of the keyboard, but now the Adview covers some of the content on the app. I was wondering if there was a way to have my app content not be covered and keep the AdView above the keyboard when it appears?
I have tried this but it did not work:
android:windowSoftInputMode="adjustPan|adjustResize"
Not sure if I actually want to go that route, I just want the choice. It kind of looks annoying in my opinion.
You can't set adjustPan and adjustResize at the same time. They mean completely different and conflicting behaviours. They only affect how your root view behaves when the keyboard appears or disappears.
adjustPan means "When the keyboard appears, if a view behind the keyboard gains focus, move the view up so that the user can see it" but it doesn't allow the user to move the view around themselves.
adjustResize means "When the keyboard appears, change the height of the view to fit above the keyboard". That doesn't mean that your view will adapt correctly to fit in that space - that's your job.
It sounds like what you're trying to do is have a ScrollView above the advert which is stuck to the bottom of the root RelativeLayout view. In your case, you definitely want to use adjustResize to cause the RelativeLayout to resize to a shorter height. This in turn moves the advert up to the top of the keyboard as you described. Then you have to make sure that the main ScrollView is always above the advert and not somehow tucked behind it. You probably want a layout that looks something like this:
<RelativeLayout
android:layoutWidth="match_parent"
android:layoutHeight="match_parent">
<ScrollView
android:layoutWidth="match_parent"
android:layoutHeight="match_parent"
android:layoutAbove="#+id/advert_view">
<!-- Your scrolling views go here -->
</ScrollView>
<AdvertView
android:id="#id/advert_view"
android:layoutWidth="match_parent"
android:layoutHeight="wrap_content"
android:alignParentBottom="true" />
</RelativeLayout>
Note that the ScrollView says it wants to be the height of the parent, but laid out above the AdvertView. This will mean it isn't behind the AdvertView at the bottom edge and you'll be able to scroll to anything that exists inside the ScrollView.

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.

Switching between layouts and keeping content displayed

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.

Categories

Resources