How to translate an included layout in an activity? - android

I want to translate my included layout on Y-axis in my activity. I could do it before if was using data binding but now since the root view my included layout is i cannot get it like this.
ConstraintLayout layout = (ConstraintLayout) mbinding.includedlayout;
Ive tried this:
LayoutBottomSheetBinding bottomSheet =
LayoutBottomSheetBinding.inflate(getLayoutInflater());
bottomSheet.setViewModel(mActivityViewModel);
bottomSheet.constraintLayoutBottomSheet.setTranslationY(-300);
. It does not give any error but nothing happens on my UI and i just see the included layout and it does not move up or down. I guess this makes is referring to another instance of that xml (not sure).
LayoutBottomSheetBinding bottomSheet =
LayoutBottomSheetBinding.inflate(getLayoutInflater());
bottomSheet.setViewModel(mActivityViewModel);
bottomSheet.constraintLayoutBottomSheet.setTranslationY(-300);
ConstraintLayout layout = (ConstraintLayout) includedRootView
mBinding.buttonBottomSheet.setOnClickListener(v -> {
if (position == 0) {
bottomSheet.constraintLayoutBottomSheet.animate().translationY(0);
position = 1;
} else {
bottomSheet.constraintLayoutBottomSheet.animate().translationY(-300);
position = 0;
}
});
Can anyone help me or tell me what am i doing wrong ? I just want to be able to translate that included layout like im doing above.

You have to start the animation:
bottomSheet.constraintLayoutBottomSheet.animate().translationY(0).start();

Related

Android visibility not working for MotionLayout

I'm trying to make visibility changes for a view under MotionLayout using this answer https://stackoverflow.com/a/62658424/5412554
but for me, it's not working under observe. For eg:
viewModel.messageLinkedList.observe(viewLifecycleOwner) {
binding.motionLayout.getConstraintSet(R.id.start).getConstraint(binding.deleteAllText.id).propertySet.mVisibilityMode = 1; // 1 - ignore or 0 - normal
binding.deleteAllText.visibility = View.GONE
}
If I use simply in onCreateView of fragment it works.
For eg:
binding.motionLayout.getConstraintSet(R.id.start).getConstraint(binding.deleteAllText.id).propertySet.mVisibilityMode = 1; // 1 - ignore or 0 - normal
binding.deleteAllText.visibility = View.GONE
Please help me with the correct solution.
I don't know it works with data-binding but I found a Solution for Kotlin, maybe you can something figure out:
motion_layout_id.getConstraintSet(R.id.start)?.let {
it.setVisibility(R.id.deleteAllText, View.GONE)
motion_layout_main.requestLayout() // this must be done to apply the visibility change
}

Grabbing a pointer to a TextView of a tab on the action bar?

Some other users and I are developing an Android application for the Stack Exchange chat network. We're adding a tutorial for each activity to explain the UI to the user. However, we've run into a bit of a road block.
The tutorial library wants a pointer to a view (be it a TextView, ImageView, whatever) in order to get the coordinates of the view in the display so it knows where to draw the drop shadows and stuff.
We have one activity which uses the standard "Tabbed Activity" from Android Studio, so we aren't using any custom toolbars.
The action bar looks like this:
And we want to grab a pointer to the TextView on each tab that holds the title of the tab.
So for example, we want to be able to access this Textview:
We haven't been real successful in finding anything on the internet about how to do this. It appears to be relatively easy if you're using a custom toolbar, but we aren't.
Digging in the AOSP source code, we found a potential way to do it, but the fields that we needed access to were either private or otherwise unaccessible from the main activity code.
So the question is, how can we grab a pointer to that TextView? Is it even possible?
Well, it isn't pretty but we found a way to do it. Using the layout inspector in Android Device Monitor to look at the view hierarchy, we were able to grab a pointer to it in the following way.
Keep in mind:
You may need to adjust for your activity's layout
If you're using a custom toolbar there's an easier way to do this
That being said, here's what worked for this specific use case:
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
LinearLayout testb = (LinearLayout) viewGroup.getChildAt(0);
FrameLayout testc = (FrameLayout) testb.getChildAt(1);
ActionBarOverlayLayout testd = (ActionBarOverlayLayout) testc.getChildAt(0);
ActionBarContainer teste = (ActionBarContainer) testd.getChildAt(1);
LinearLayoutCompat testg;
if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT)
{
ScrollingTabContainerView testf = (ScrollingTabContainerView) teste.getChildAt(2);
testg = (LinearLayoutCompat) testf.getChildAt(0);
}
else //Landscape
{
Toolbar teste2 = (Toolbar) teste.getChildAt(0);
ScrollingTabContainerView testf = (ScrollingTabContainerView) teste2.getChildAt(0);
testg = (LinearLayoutCompat) testf.getChildAt(0);
}
testg.setId(android.R.id.tabcontent);
//String IdAsString = testg.getResources().getResourceName(testg.getId());
//Log.e("TestG", IdAsString);
TutorialStuff.chatsExplorationTutorial(this, testg);
And here's the end result:

Android: Bottom Sheet text view not updating

Here is my code:
public void openBottomSheet(long id) {
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
_bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
_bottomSheet.setPeekHeight(140);
coordinatorLayout.requestLayout();
Item item = _selected.getItem(id);
final String titleText = item.getTitleText();
_bottomSheetTitle.setText(titleText);
coordinatorLayout.requestLayout();
int imageResource = (id > 5) ? R.drawable.ic_closed : R.drawable.ic_open;
_bottomSheetState.setBackgroundResource(imageResource);
coordinatorLayout.requestLayout();
coordinatorLayout.invalidate();
}
Firstly, I'm broadly having the issue described at https://code.google.com/p/android/issues/detail?id=205226 where they suggested requestLayout() was the solution.
And it does sort of fix it. However, I am finding the first time I open the bottom sheet (by clicking some other element in the UI) the text does not load. The second, third, fourth and so on time, the text does update. So it's just on that initial load that it's not quite working.
The image resource seems to load fine on the first attempt. It's just the textview not updating.
Does anyone have any suggestions?
Edit
I've tried putting the requestLayout() in a number of places to see if it would make a difference.

How to make a ViewFlipper behave like a Scroller?

Good day everyone.
I am creating a calendar component, and I'm working in the month view. I have created a view named MonthView, and I am adding a couple instances of this to a ViewFlipper:
viewFlipper = new ViewFlipper(getContext());
viewFlipper.addView(new MonthView(viewFlipper.getContext()));
viewFlipper.addView(new MonthView(viewFlipper.getContext()));
I have implemented the fling gesture so that I change views when sliding my finger left or right. This will cyclically update and display the months.
Now, I need to give the fling gesture a smoothly effect when touching and slowly sliding my finger. The same we get when we use a Slider instead a ViewFlipper.
The problem with Scroller is that the effect is not cyclic. Once I get to the last view, I have to slide in the other direction.
I need someone help me find how to give a scroll-like effect to the ViewFlipper, or how to make a Scroller cyclic.
Thanks in advance.
Extra comment:
I have already implemented a ViewFlipper with 2 views. I update the views by using the SimpleOnGestureListener.onFling(...) method, and the behavior I got is something like this:
Imagine I always slide from rigth to left, like flipping a book's page to read the next one, and also imagine there is a caption in the header of the view that is displayed after flipping.
View # 0 --> Caption: January 2011
View # 1 --> Caption: Febrary 2011
View # 0 --> Caption: March 2011
View # 1 --> Caption: April 2011
View # 0 --> Caption: May 2011
If at this point I slide from left to right, the result will be something like:
View # 1 --> Caption: April 2011
View # 0 --> Caption: March 2011
The ability to cyclically move forward or backward, giving the user the idea of having infinite views, but using only a couple is characteristic of ViewFlipper, and that's what I can't loose.
That's why I need a way to add the cool scroll effect without loosing what I've got.
Thanks.
Then you can use ViewFlinger!
viewflinger this an android widget (extends ViewGroup) that allows to group a set of views that can be swiped horizontally. It offers a smoother transition that cannot be accomplished using ViewFlipper. This is based on the Workspace class on the iosched project, which is based in the class with the same name of the Launcher app.
Download: 1.0.2 | Sources | JavaDoc
If you use Maven, you can use it as an artifact from this repository: http://mvn.egoclean.com/. Also, you would want to look this video where I show how it looks like: http://www.youtube.com/watch?v=gqIXq5x7iLs (sorry for my accent which sucks)
I think what you wanted to do is create an apparently infinite list of layouts being flinged by either the ViewFlipper or Christian's ViewFlinger. And also you want to keep reusing views / layouts inside the Flinger / Flipper. Right ?
If yes, probably the following is what you wanted to do. I've done this based on Christian's ViewFlinger,
Here you go,
First add three layouts to the ViewFlinger:
<com.egoclean.android.widget.flinger.ViewFlinger
android:id="#+id/calendarViewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/calendarViewLayout0"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ScrollView>
<ScrollView
android:id="#+id/calendarViewLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ScrollView>
<ScrollView
android:id="#+id/calendarViewLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ScrollView>
</com.egoclean.android.widget.flinger.ViewFlinger>
Then inside your activity, you take an array of three views so that you can access them directly through the array instead of searching every time inside the flinger,
private ViewFlinger viewFlinger;
private ViewGroup layouts[] = new ViewGroup[3];
private boolean userEvent = false;
#Override
public final void onCreateSub(Bundle savedInstanceState)
{
setContentView(R.layout.your_layout);
viewFlinger = (ViewFlinger) findViewById(R.id.calendarViewFlipper);
layouts[0] = (ViewGroup) findViewById(R.id.calendarViewLayout0);
layouts[1] = (ViewGroup) findViewById(R.id.calendarViewLayout1);
layouts[2] = (ViewGroup) findViewById(R.id.calendarViewLayout2);
viewFlinger.setOnScreenChangeListener(new ViewFlinger.OnScreenChangeListener()
{
#Override
public void onScreenChanging(View newScreen, int newScreenIndex)
{
}
#Override
public void onScreenChanged(View newScreen, int newScreenIndex)
{
if (userEvent)
{
ViewGroup tempLayout = null;
if (newScreenIndex != 1)
{
// We don't want our actions to raise events and create a cyclic event chain
userEvent = false;
if (newScreenIndex == 2) // Scrolling towards right
{
tempLayout = layouts[0];
viewFlinger.removeViewFromFront();
viewFlinger.addViewToBack(tempLayout);
layouts[0] = layouts[1];
layouts[1] = layouts[2];
layouts[2] = tempLayout;
// Any other logic comes here...
}
else if (newScreenIndex == 0) // Scrolling towards left
{
tempLayout = layouts[2];
viewFlinger.removeViewFromBack();
viewFlinger.addViewToFront(tempLayout);
layouts[2] = layouts[1];
layouts[1] = layouts[0];
layouts[0] = tempLayout;
// Any other logic comes here...
}
// We switch the screen index back to 1 since the current screen index would change back to 1
viewFlinger.setCurrentScreenNow(1, false);
userEvent = true;
// And any other logic that you'd like to put when the swapping is complete May be fill the swapped view with the correct values based on its new location etc...
View result = refreshView(tempLayout.getChildAt(0));
if (result.getParent() != tempLayout)
{
((ViewGroup) result.getParent()).removeView(result);
tempLayout.removeAllViews();
tempLayout.addView(result);
}
}
}
}
});
}
I hope this is clear to you and helps you with your problem. It is working very fine for me! Should work fine for you too.
P.S. Thanks # Christian for the ViewFlinger, it is awesome. However it lacks some good onConfigurationChanged logic, if you get time do put something in :). The rest is the best !

Expand a list view when clicked on an element of it

I am beginner in the android development, can any one tell me how to expand a list when I click on an element of it say an image icon.
This does it for me, due to privacy I can't write the whole code here
TextView desc = (TextView) v.findViewById(R.id.editText1);
ImageView icon = (ImageView) v.findViewById(R.id.listIcon);
if (desc.getVisibility() == View.VISIBLE) {
icon.getLayoutParams().height = heightIcon;
desc.setVisibility(View.GONE);
} else {
icon.getLayoutParams().height = LayoutParams.FILL_PARENT;
desc.setVisibility(View.VISIBLE);
}
Now when I click the icon, the text view placed below to it, becomes visible and thus has solved the question.
Take a look at ExpandableListView and ExpandableListAdapter
Here is the example code snippet to do it : ExpandableList1.java
You can even find it in your Android SDK folder
Drive:***\***\Android-sdk\samples\android-<apilevel>\ApiDemos\src\com\example\android\apis\view\ExpandableList1.java

Categories

Resources