I want to iterate over a LinearLayout's children and apply Slide transitions so that they appear one after the other in a coordinated manner. First, views are visible. Then I call TransitionManager.beginDelayedTransition and apply gone or invisible visibilities on the children. The view hierarchy:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container"
android:orientation="vertical"
>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bruce Lee"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jason Statham"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jean-Claude Van Damme"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Arnold Schwarzenegger"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sylvester Stallone"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scot Atkins"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bruce Lee"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:padding="#dimen/padding"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
When applying gone, the children disappear abruptly. With, invisible, they disappear in a venticular chaotic manner.
My activity code is:
public class MainActivity extends AppCompatActivity {
Button mButton;
ViewGroup mContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = findViewById(R.id.container);
mButton = findViewById(R.id.start);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity", "onClick: child count: " + mContainer.getChildCount());
for(int i = 0; i < mContainer.getChildCount(); i++) {
Log.d("MainActivity", "onClick: transitioning child " + mContainer.getChildAt(i).toString());
TransitionManager.beginDelayedTransition((ViewGroup)findViewById(R.id.container), new Slide().setDuration(1000));
(mContainer.getChildAt(i)).setVisibility(View.GONE);
}
}
});
}
}
You should change layout_height in your LinearLayout to "match_parent".
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:orientation="vertical"
>
UPDATE: I can't believe you want to hide your items forever, moreover I think you don't want the items to go to the bottom instead of the right side of screen. Let me suggest you one more fix for your Java code:
for (int i = 0; i < mContainer.getChildCount(); i++) {
View child = mContainer.getChildAt(i);
TransitionManager.beginDelayedTransition(mContainer, new Slide(Gravity.END).setDuration(1000));
child.setVisibility(child.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
Anyway, now you have more options to play with.
UPDATE2: Post transition as delayed Runnables.
public void onClick(View view) {
for(int i = 0; i < mContainer.getChildCount(); i++) {
final View child = mContainer.getChildAt(i);
final Transition slide = new Slide(Gravity.END).setDuration(1000);
mContainer.postDelayed(new Runnable() {
#Override
public void run() {
TransitionManager.beginDelayedTransition(mContainer, slide);
child.setVisibility(child.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
}
}, 1000 * i);
}
}
Related
What might cause changes made to a view programmatically to not be reflected on screen or even in debugging? For example, changing height or visibility of a view?
I'm seeing an issue where after one layout finishes animating, any changes I make to a different layout (height, child visibility, etc), no longer get applied.
In the image below, the sequence of events would be:
Show 1 is clicked to display "Hello World" in blue at the bottom.
Show 1 is clicked to hide "Hello World"
Show 2 is clicked to display "Goodbye World" in green/yellow at the bottom. Depending on the requested size, the height should be 50 or 100. When 100, there will be extra text.
Note: this gif shows expected behavior, but the issue is described further below.
What I am seeing is that when adjusting the second layout using the code below, the changes do not appear on screen and will not appear in debugging, but only if the first layout is ever displayed. For example, if the code tries to change the height to 100, the layout will still appear as height 50 or vice versa. This also applies to the visibility of the testHide TextView where it will be set to Gone but appear as Invisible.
private void ButtonTwo_Click(object sender, EventArgs e)
{
if (layoutTwo.Visibility == ViewStates.Visible)
{
layoutTwo.StartAnimation(hide);
}
else
{
var layoutParams = layoutTwo.LayoutParameters;
Random r = new Random();
int coin = r.Next(2);
if (coin == 0)
{
layoutParams.Height = 100;
testHide.Visibility = ViewStates.Visible;
}
else
{
layoutParams.Height = 50;
testHide.Visibility = ViewStates.Gone;
}
height.Text = "Height: " + layoutParams.Height;
layoutTwo.LayoutParameters = layoutParams;
layoutTwo.Visibility = ViewStates.Visible;
layoutTwo.StartAnimation(show);
}
}
ButtonOne_Click
private void ButtonOne_Click(object sender, EventArgs e)
{
if (layoutOne.Visibility == ViewStates.Visible)
{
layoutOne.StartAnimation(hide);
}
else
{
layoutOne.Visibility = ViewStates.Visible;
layoutOne.StartAnimation(show);
}
}
content_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show 1"
android:id="#+id/button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Height:"
android:id="#+id/height"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="show 2"
android:id="#+id/button2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#123ABC"
android:id="#+id/layout1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello World!" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ABC123"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:id="#+id/layout2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:text="Goodbye World!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:id="#+id/testHide"
android:text="test hide" />
</LinearLayout>
Some things I have tried:
Add/remove child view before animating
Using View.Post to handle animation
View.Invalidate before animation
Assign new LinearLayout.LayoutParams object with MatchParent for width and the desired height.
I have also run a loop in a separate thread that monitors each view and confirmed the second layout's height was not being changed after displaying the first layout, but would be changed if the first layout was never displayed.
I was not able to fully recreate your problem but based on code You have submitted I think something like this should work fine:
MainActivity:
public class MainActivity : AppCompatActivity
{
private LinearLayout layoutOne;
private LinearLayout layoutTwo;
private TextView testHide;
private TextView height;
private Button button1;
private Button button2;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
layoutOne = FindViewById<LinearLayout>(Resource.Id.layout1);
layoutTwo = FindViewById<LinearLayout>(Resource.Id.layout2);
testHide = FindViewById<TextView>(Resource.Id.testHide);
height = FindViewById<TextView>(Resource.Id.height);
button1 = FindViewById<Button>(Resource.Id.button1);
button2 = FindViewById<Button>(Resource.Id.button2);
button1.Click += ButtonOne_Click;
button2.Click += ButtonTwo_Click;
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void ButtonOne_Click(object sender, EventArgs e)
{
Animation show = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.show);
Animation hide = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.hide);
if (layoutOne.Visibility == ViewStates.Visible)
{
layoutOne.StartAnimation(hide);
layoutOne.Visibility = ViewStates.Gone;
}
else
{
layoutOne.Visibility = ViewStates.Visible;
layoutOne.StartAnimation(show);
}
}
private void ButtonTwo_Click(object sender, EventArgs e)
{
Animation show = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.show);
Animation hide = AnimationUtils.LoadAnimation(Application.Context, Resource.Animation.hide);
if (layoutTwo.Visibility == ViewStates.Visible)
{
layoutTwo.StartAnimation(hide);
layoutTwo.Visibility = ViewStates.Gone;
}
else
{
var layoutParams = layoutTwo.LayoutParameters;
Random r = new Random();
int coin = r.Next(2);
if (coin == 0)
{
layoutParams.Height = 100;
testHide.Visibility = ViewStates.Visible;
}
else
{
layoutParams.Height = 50;
testHide.Visibility = ViewStates.Gone;
}
height.Text = "Height: " + layoutParams.Height;
layoutTwo.LayoutParameters = layoutParams;
layoutTwo.Visibility = ViewStates.Visible;
layoutTwo.StartAnimation(show);
}
}
}
Layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show 1"
android:id="#+id/button1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Height:"
android:id="#+id/height"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="show 2"
android:id="#+id/button2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#123ABC"
android:id="#+id/layout1"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hello World!" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ABC123"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:id="#+id/layout2"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:text="Goodbye World!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right"
android:id="#+id/testHide"
android:text="test hide" />
</LinearLayout>
</RelativeLayout>
Show Animation:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<translate
android:fromYDelta="100%p"
android:toYDelta="0%p"
android:duration="700" />
</set>
Hide Animation:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<translate
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="700" />
</set>
I'm creating a list view in an android app and each item expands and collapses.
Here is each item's code:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="#drawable/alarme_layout"
android:backgroundTint="#color/colorPrimaryDark"
android:padding="20dp">
<TextView
android:id="#+id/textView_aHora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="00:00"
android:textSize="26sp"
android:textColor="#color/colorOnPrimary"
android:layout_marginBottom="10dp"/>
<Switch
android:id="#+id/switch_aEstado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:id="#+id/layout_ultimaLinhaCollapsed"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/layout_diasRepetir"
android:orientation="horizontal"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="25dp">
</LinearLayout>
<ImageView
android:id="#+id/imageView_arrowDown"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_detalhesExpanded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora">
<CheckBox
android:id="#+id/checkbox_repetir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/repetir"
android:textColor="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"/>
<LinearLayout
android:id="#+id/layout_diasRepetirButtons"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_repetir"
android:layout_alignStart="#+id/checkbox_repetir"
android:layout_marginTop="10dp"
android:visibility="invisible">
</LinearLayout>
<LinearLayout
android:id="#+id/ultimaLinhaExpanded"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_diasRepetirButtons"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/editText_mensagem"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="wrap_content"
android:hint="#string/mensagemHint"
android:inputType="text"
android:backgroundTint="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"
android:textColor="#color/colorSecondary"
android:shadowColor="#color/colorSecondary"
android:textColorHighlight="#color/colorSecondaryLight" />
<ImageView
android:id="#+id/imageView_arrowUp"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_up_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
And here is the "getView()" method of list view:
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.alarme, null);
final Alarme alarme = alarmes.get(position);
//main view
TextView hora = convertView.findViewById(R.id.textView_aHora);
Switch estado = convertView.findViewById(R.id.switch_aEstado);
LinearLayout layoutDiasRepetir = convertView.findViewById(R.id.layout_diasRepetir);
final ImageView arrowDown = convertView.findViewById(R.id.imageView_arrowDown);
//detalhes
final RelativeLayout layoutDetalhesExpanded = convertView.findViewById(R.id.layout_detalhesExpanded);
CheckBox cbRepetir = convertView.findViewById(R.id.checkbox_repetir);
LinearLayout layoutDiasRepetirButtons = convertView.findViewById(R.id.layout_diasRepetirButtons);
EditText editText = convertView.findViewById(R.id.editText_mensagem);
final ImageView arrowUp = convertView.findViewById(R.id.imageView_arrowUp);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
hora.setText(alarme.getHora());
estado.setChecked(alarme.isLigado());
cbRepetir.setChecked(alarme.isRepete());
arrowDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowDown.setVisibility(View.INVISIBLE);
arrowUp.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.VISIBLE);
alarme.setCollapsed(false);
}
});
arrowUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowUp.setVisibility(View.INVISIBLE);
arrowDown.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
alarme.setCollapsed(true);
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
alarme.setMensagem(s.toString());
}
});
arrowDown.setVisibility(alarme.isCollapsed() ? View.VISIBLE : View.INVISIBLE);
arrowUp.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
layoutDetalhesExpanded.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
editText.setText(alarme.getMensagem());
return convertView;
}
I thought each item would have its size wrapped depending on the visible content, but I have this result instead:
This is the item when expanded:
https://imgur.com/gallery/oIv50dT
This is the item when collapsed:
https://imgur.com/gallery/dmzw5GU
I wanted the collapsed item to have its size wrapped to the content.
I am going to guess here that in your XML layout you have set a static height.
You need to set it to be wrap_content which allows for dynamic row heights.
I solved it by changing the visibility of the views from View.INVISIBLE to View.GONE.
Basically, I want to have an AlertDialog in which multiple images will load on the same ImageView. By clicking the NEXT button it will get the next image bitmap from bitmap list and finally will load on that ImageView. Same case for PREV button. It will take previous bitmap and will load the image on the same ImageView.
But the problem is after loading the first image it does not load the next image. If I click the next button then it takes the next bitmap but the imageview.setImageBitmap(bitmap) does not work.
How to remove or clear the previous image to place the next photos?
The XML file are given below :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/question"
android:gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:text="#string/question"
android:textSize="14sp"
android:textColor="#color/light_black"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/selected_place_images"
android:scaleType="centerCrop"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#color/divider"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="47dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/previous"
android:text="#string/previous_page"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:text="#string/next_page"
android:textColor="#color/black"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The java code are given below :
private void showDialogOfImages() {
Log.d(TAG,"showDialogOfImages : showing places images");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = this.getLayoutInflater();
View customLayout = inflater.inflate(R.layout.custom_displaying_selected_place_images,null);
mSelectedPlaceImages = (ImageView) customLayout.findViewById(R.id.selected_place_images);
nextPage = (TextView) customLayout.findViewById(R.id.next);
previousPage = (TextView) customLayout.findViewById(R.id.previous);
displayPhoto();
nextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentPhotoIndex++;
displayPhoto();
}
});
previousPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentPhotoIndex--;
displayPhoto();
}
});
builder.setView(customLayout);
builder.show();
}
private void displayPhoto() {
if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
mSelectedPlaceImages.setImageBitmap(bitmap);
setButtonVisibility();
}
}
private void setButtonVisibility() {
if(mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() == 1){
nextPage.setEnabled(false);
nextPage.setClickable(false);
nextPage.setTextColor(getResources().getColor(R.color.divider));
previousPage.setEnabled(false);
previousPage.setClickable(false);
previousPage.setTextColor(getResources().getColor(R.color.divider));
}
else if (mCurrentPhotoIndex == 0 && mSelectedLocationPhotosBitmap.size() > 1){
nextPage.setEnabled(true);
nextPage.setClickable(true);
nextPage.setTextColor(getResources().getColor(R.color.black));
previousPage.setEnabled(false);
previousPage.setClickable(false);
previousPage.setTextColor(getResources().getColor(R.color.divider));
}
else if (mCurrentPhotoIndex == mSelectedLocationPhotosBitmap.size()-1 && mSelectedLocationPhotosBitmap.size() > 1){
nextPage.setEnabled(false);
nextPage.setClickable(false);
nextPage.setTextColor(getResources().getColor(R.color.divider));
previousPage.setEnabled(true);
previousPage.setClickable(true);
previousPage.setTextColor(getResources().getColor(R.color.black));
}
else{
nextPage.setEnabled(true);
nextPage.setClickable(true);
nextPage.setTextColor(getResources().getColor(R.color.black));
previousPage.setEnabled(true);
previousPage.setClickable(true);
previousPage.setTextColor(getResources().getColor(R.color.black));
}
}
You need to clear the previous image from imageview and then you can set the new on top.
Do as below
private void displayPhoto() {
if (mCurrentPhotoIndex < mSelectedLocationPhotosBitmap.size()) {
Bitmap bitmap = mSelectedLocationPhotosBitmap.get(mCurrentPhotoIndex);
Toast.makeText(context,""+mCurrentPhotoIndex+" : "+bitmap,Toast.LENGTH_LONG).show();
mSelectedPlaceImages.setImageBitmap(null)
mSelectedPlaceImages.setImageBitmap(bitmap);
setButtonVisibility();
}
}
Try this:
mSelectedPlaceImages.destroyDrawingCache();
mSelectedPlaceImages.setImageBitmap(bitmap);
mSelectedPlaceImages.buildDrawingCache();
I have a CoordinatorLayout with a custom view and a persistent bottom sheet. I have disabled hiding for the bottom sheet and want to show the top couple of views in a LinearLayout in the collapsed state. To account for different screen sizes, I dynamically set the peekHeight in my activity's onCreate, after everything has been laid out.
The issue is when the activity first starts, the BottomSheet peeks at one height, and then after it is expanded and collapsed again the peekHeight seems to have changed. Interestingly enough, it visually looks like the peekHeight added is the layouts activity padding (16dp). However, the BottomSheetBehavior reports the same peekHeight for both.
I have created a hacky workaround for this, but I want to see if there is anything I am doing wrong. Below is a gif showing when I do not have my workaround and one when I am using my workaround. I have also included the relevant code and layout files. Again, It seems to be directly related to my padding (16dp), but I don't understand how to fix it.
NOT using workaround:
Using workaround:
Activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_schedule_details_activity);
final CoordinatorLayout activityLayout =
(CoordinatorLayout) findViewById(R.id.edit_schedule_details_layout);
ButterKnife.bind(this);
// ... removed unrelated logic
final View bottomSheet = findViewById(R.id.edit_details_sheet);
final LinearLayout peekContainer = (LinearLayout) findViewById(R.id.info_title_container);
activityLayout
.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Set the measured peek height and add in 16 dps to account for inconsistency
// Adding the 16 dps is part of the workaround
bottomSheetBehavior.setPeekHeight(peekContainer.getHeight());
//+ (int) (16 * getResources().getDisplayMetrics().density)); /* workaround */
Timber.i("Title height: " + bottomSheetBehavior.getPeekHeight());
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(View bottomSheet, int newState) {
String stateString = "";
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
stateString = "Expanded";
/* workaround */
/*if(!bottomSheetSet){
// remove the extra 16 dp added since the layout now looks correct
bottomSheetBehavior
.setPeekHeight(
bottomSheetBehavior.getPeekHeight()
- (int) (16 * getResources().getDisplayMetrics().density));
bottomSheetSet = true;
}*/
}
else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
stateString = "Collapsed";
Timber.i("peekHeight = " + bottomSheetBehavior.getPeekHeight());
}
else if (newState == BottomSheetBehavior.STATE_HIDDEN) {
stateString = "Hidden";
}
else if(newState == BottomSheetBehavior.STATE_SETTLING) {
stateString = "Settling";
}
else if(newState == BottomSheetBehavior.STATE_DRAGGING){
stateString = "Dragging";
}
Timber.i("bottomSheetState = " + stateString);
}
#Override
public void onSlide(View bottomSheet, float slideOffset) {
}
});
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/edit_schedule_details_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin"
android:background="#ff171e26">
<com.cel.cortetcommercial.widget.ring_view.RingView
android:id="#+id/edit_ringview"
android:layout_width="match_parent"
android:layout_height="400dip"
app:inner_radius="95dip"
app:outer_radius="130dip"
app:color="#color/colorSecondaryDark"
app:is_touchable="true"
app:text_size="12dip"
app:tick_length="0dip"
app:tick_width="0dip"
android:transitionName="#string/transition_details_to_edit"/>
<LinearLayout
android:id="#+id/add_remove_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
app:layout_anchor="#id/edit_ringview"
app:layout_anchorGravity="bottom">
<Button
android:id="#+id/remove_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_gravity="end"/>
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/edit_details_sheet"
android:layout_width="match_parent"
android:layout_height="250dp"
android:clipToPadding="true"
android:background="#color/colorPrimary"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
app:behavior_hideable="false">
<include layout="#layout/current_info_sheet">
</include>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
current_info_sheet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/info_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/info_title_container">
<TextView
android:id="#+id/segment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:text="#string/placeholder"
android:textAppearance="#android:style/TextAppearance.Material.Headline"
android:textColor="#color/white"/>
<TextView
android:id="#+id/time_remaining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="#string/placeholder"
android:textColor="#color/white"/>
<Space
android:layout_width="1dp"
android:layout_height="16dp"/>
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:textColor="#color/white"
android:text="#string/luminosity"/>
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"/>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:textColor="#color/white"
android:text="#string/color_warmth"/>
<SeekBar
android:id="#+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"/>
</LinearLayout>
I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)