Android: put something on top of linear layout - android

What I want to do is show a "frame" (or new layout) on top of "2" (second LinearLayout), when a button would be pressed. How should I do it? Precreate it and make it somehow hidden if button not pressed?
I have this type of layout:
XML:
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ViewFlipper>
</ViewFlipper>
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>

Use FrameLayout to show view over-lapping another view. You can keep the view as INVISIBLE or using GONE in the xml and then just make it visible when the Button is Clicked.

Yes...you should prepare it in xml and give it an id.then you can easily manage its visibility on button click using mLinearLayout.setVisibility(View.GONE); and mLinearLayout.setVisibility(View.VISIBLE); like:
Button mButton=(Button)findViewById(R.id.button);
LinearLayout ll=(LinearLayout)findViewById(R.id.frame_layout);
static int count=0;
mButton.setOnClick.... (new OnClick...()
public void onClick(){
count++;
if(count==1)
ll.setVisibility(View.VISIBLE);
else
{
count=0;
ll.setVisibility(View.GONE);
}
}
);

Here you have two options:
As you said pre-create layouts and set visibility to Visibility_Gone to layouts initially, not to be shown, set Visibitlity to View.Visible to display the layouts.
Another approach is to create views dynamically, and adding to parent on specified index, like to add on top of linearlayout use:
linearLayout.addView(view, 0);

If you want to show any view on button click then first put that view inside xml and make its visibility gone, and on button click make it visible. I have put imageview inside your code which visibility is set as gone so it wont show in layout.
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
For making image view visible,
imag1.seVisibility(View.VISIBLE);

Related

Android Studio Make Buttons Disappear From ScrollView

I have a ScrollView that contains a lot of buttons.
I want to create a search bar, so how can I disappear a button from the ScrollView so he will be hidden and the ScrollView will get shorter?
and then ofcourse I want to be able to return the button to the ScrollView.
you can change the visibility of the button from the activity by
if(true){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.GONE);
};
you may also have to change the visibility in XML also
<ButtonView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btn"
android:visibility="gone" />

how to use buttons in tab layout in android and use this button to call different fragments and place the tab layout at the bottom of the screen

I want to place buttons in a tab layout and use these buttons to call different fragments in a tab layout. Moreover I want to place some image in each button .Can anyone please help ??
You can use ImageButton in your xml file and align it to the bottom of the page and set the text and back image of the ImageButton from code section.
somthing like this:
the_xmlLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/theImageButton"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
And somthing like this in your java code:
View v = (View) getActivity().getLayoutInflater().inflate(R.layout., null);
ImageButton ib = (ImageButton) v.findViewById(R.id.theImageButton);
ib.setBackgroundResource(R.drawable.X);
You cand define a method to change the background randomly with setBackgroundResource method.

Animating layouts using the "animateLayoutChanges" attribute in XML

I have a LinearLayout, for which I apply android:animateLayoutChanges="true" in the parent LinearLayout. When the user clicks a toggle button, the LinearLayout "collapses" (I set the view's visibility as LinearLayout.GONE programmatically, and when they click it again, it expands by programmatically setting the visibility back to LinearLayout.VISIBLE.
The animation of it collapsing and expanding works correctly.
However, any items below the collapsable/expandable LinearLayout snap back to the top before the animation of the collapse is complete. The items that are snapping back are NOT inside the parent which has animateLayoutChanges set to true, and I don't think there is any way I can put them inside it.
Here is my layout hierarchy (I didn't mention the attributes to keep it short):
<!-- Top most LinearLayout-->
<LinearLayout>
<!-- LinearLayout containing android:animateLayoutChanges="true"-->
<LinearLayout>
<!-- RelativeLayout containing button to toggle LinearLayout visibility below-->
<RelativeLayout>
</RelativeLayout>
<!-- LinearLayout that has its visibility toggled -->
<LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
This entire layout is inserted programmatically into another LinearLayout (see below):
<LinearLayout
android:id="#+id/form_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This is where the previous XML layout containing the toggle-able LinearLayout
is inserted programmatically. -->
<!-- This button snaps back up to the top before the animation is complete. -->
<Button />
</LinearLayout>
I realize the problem would be solved if I added the Button that snaps up to the LinearLayout that has animateLayoutChanges as true. However, this isn't an options for a few reasons.
So is there any other work around?
Instead of using animateLayoutChanges try adding
TransitionManager.beginDelayedTransition(transitionsContainer); in your onClick method where transitionsContainer is parent of views that should be animated.
For example your parent layout is is
<LinearLayout android:id="#+id/transitions_container">
<!--add your widgets here-->
</LinearLayout>
And in code
final ViewGroup transitionsContainer = (ViewGroup) view.findViewById(R.id.transitions_container);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TransitionManager.beginDelayedTransition(transitionsContainer);
// do your staff with changing children of transitionsContainer
}
});
Check https://medium.com/#andkulikov/animate-all-the-things-transitions-in-android-914af5477d50 for details.
Just a thought... What if you remove the 'animateLayoutChanges' from the embedded layout, and add it to the parent layout (second XML)? I suspect that this would animate everything. You may have to set the property to true in code since you're programmatically embedding the layout.
Here's how to do it programmatically
Stack-overflow
Another option would be to use the .animate property programmatically on the button that snaps back
myButton.animate().translationY(floatYposition).setDuration(300); //300 milliseconds
for linear layout try to manually enable 'changing' transition too
look at this for frame layout.. try that for your linear layout..
https://stackoverflow.com/a/51116293/2719243

How to make a view as always on top?

I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:
If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).
You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"

Getting nested item on onClick of ListView

I have row_item.xml with nestem LinearLayout and multiple ImageViews. It generates 2 x 2 image grid in each row of ListView. On onClick aI want to know on which Image item user has clicked.
In onClick method I have tried to see which item is actually coming using item.getClass() and it shows LinearLayout. I am sure this will be top level layout in row Item. Here is my row item skeleton layout. I am looking for some generic approach where in I can get hold of clicked item at any depth.
<LinearLayout>
<LinearLayout>
<ImageView> </ImageView>
<ImageView> </ImageView>
</LinearLayout>
<LinearLayout>
<ImageView> </ImageView>
<ImageView> </ImageView>
</LinearLayout>
</LinearLayout>
You can set your image views clickable by setting appropriate options in xml where you declare your listView. And then just add onclickListeners to your imageViews.
For me solution that worked was setting:
clickable
focusable
focusableInTouchMode
to true. If you have this done, you can in your adapter getView/bindView setonClickListeners to your imageviews.
For each ImageView add the following:
android:clickable="true"
android:onClick="myOnClickFunction"
and add code like:
public void myOnClickFunction(final View imageView) {
// imageView.getId() - actual image view id.
}

Categories

Resources