I'm new to android.
I'm trying to make a simple application.
There are two buttons(left and right) and an image.
Image moves left or right according to the clicked button.
The problem is like the following pictures :
LEFT button is clicked multiple times
RIGHT button is clicked several times
Moving left is OK, but when the image moves right, the image will disappear behind something. Please, let me know why the image disappears and how to fix this problem.
This is activity_main.xml.
<?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="com.alticast.avoidpoo.MainActivity">
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
<ImageView
android:id="#+id/img_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonLinearLayout"
android:layout_centerHorizontal="true"
android:visibility="visible"
app:srcCompat="#mipmap/ic_launcher" />
<LinearLayout
android:id="#+id/buttonLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="#+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/left"
android:visibility="visible" />
<Button
android:id="#+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/right"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
This is MainActivity.class.
package com.alticast.avoidpoo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
int layout_width; // the width of relative layout
int layout_height; // the height of relative layout
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
Button btn_left = (Button) findViewById(R.id.btn_left);
Button btn_right = (Button) findViewById(R.id.btn_right);
btn_left.setOnClickListener(moveBtnClickListener);
btn_right.setOnClickListener(moveBtnClickListener);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
ViewTreeObserver vto = relativeLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout_height = relativeLayout.getMeasuredHeight();
layout_width = relativeLayout.getMeasuredWidth();
}
});
}
View.OnClickListener moveBtnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView img_android = (ImageView) findViewById(R.id.img_android);
int id = view.getId();
if (id == R.id.btn_left) {
int x = img_android.getLeft();
if (x-10 <= 0) return;
img_android.setLeft(x-10);
} else if (id == R.id.btn_right) {
int x = img_android.getLeft();
if (x+10 > layout_width-img_android.getWidth()) return;
img_android.setLeft(x+10);
}
}
};
}
==============================================================================
I solved this problem by using setLayoutParams() methods.
Guessing that calling setLeft () directly on the image view seems to be a bad idea. It seems to be the key to using LayoutParams.
The new MainActivity.class is the following. :
View.OnClickListener moveBtnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
ImageView img_android = (ImageView) findViewById(R.id.img_android);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)img_android.getLayoutParams();
int id = view.getId();
if (id == R.id.btn_left) {
if (params.leftMargin-10 < 0) return;
params.leftMargin = params.leftMargin - 10;
} else if (id == R.id.btn_right) {
if (params.leftMargin+10 > layout_width-img_android.getWidth()) return;
params.leftMargin = params.leftMargin + 10;
}
img_android.setLayoutParams(params);
}
};
In activity_main.xml, I just removed 'android:layout_centerHorizontal="true"' from the image view.
<ImageView
android:id="#+id/img_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonLinearLayout"
android:visibility="visible"
app:srcCompat="#mipmap/ic_launcher" />
Related
I'm new to android application development, I need to add buttons with different names, that is, the name of the button is taken from EditText and a button is created. The nuance of all this is that after I try to create another button and enter another name, this very name is transferred to other buttons. That is, if I create a button for the first time, then it is given the text Sometning1, but after I create the second button with the text Sometning2, then the first button gets the name of the second one, and so on, all previous buttons get the name of the new one, how to fix this? By the way, each button should be completely different, with different properties, but for now let's focus on the text in it.(sorry, hard to explain)
Xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity6">
<TableLayout
android:id="#+id/Tb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<TableRow
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TableRow
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r4"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r5"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r6"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r7"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r8"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
</TableLayout>
<EditText
android:id="#+id/adress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:inputType="text"
android:text="Something"
android:textColor="#424242"
app:layout_constraintBottom_toBottomOf="#+id/btn1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="LabelFor" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.serverconnectionukaa;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
public class MainActivity6 extends AppCompatActivity {
EditText adress;
Button btn1;
private TableLayout buttonTableLayout;
int clickcount=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
adress = findViewById(R.id.adress);
btn1 = (Button) findViewById(R.id.btn1);
buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==0)
{
}
else
{
//check how many times clicked and so on
Toast.makeText(getApplicationContext(),"Button clicked count is"+clickcount, Toast.LENGTH_LONG).show();
}
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int x = 0;
for (int row = 0; row < 5; row++) {
TableRow currentTableRow = getTableRow(row);
for (int column = 0; column < 2; column++) {
Button newGuessButton = new Button(MainActivity6.this);
String myName = adress.getText().toString();
newGuessButton.setText(myName);
currentTableRow.addView(newGuessButton);
x++;
if (x==clickcount) {
break;
}
}
if (x==clickcount) {
break;
}
}
}
});
}
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}
}
Thanks to everyone
Can anyone help or share a link on how to implement this toggle button in android:
I've tried using this:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Active"
android:textOff="Completed"/>
but it's not having the desired output.
It has only one button and it changes when i click on it. What i want is two buttons side by side as shown in the picture above.
With some research, i found a better way with the new Material Design. We need to use the MaterialButtonToggleGroup which is provided in the Android material library:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
app:checkedButton="#id/active">
<com.google.android.material.button.MaterialButton
android:id="#+id/active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Active"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/completed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Completed"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
Hope this helps other persons as well...
Custom Implementation ----
Colors
<color name = "background">#5cc0fa</color>
<color name = "white">#FFFFFF</color>
XML layout
<?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"
>
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/background"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textColor="#color/white"
android:background="#color/background"
android:text="All" />
<Button
android:id="#+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textColor="#color/background"
android:background="#color/white"
android:text="Done" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Inside Activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
int var = 0;
Button btnAll;
Button btnDone;
LinearLayout linearLayout;
public void toggle() {
if(var == 0) {
var = 1;
btnAll.setBackgroundColor(getResources().getColor(R.color.white));
btnAll.setTextColor(getResources().getColor(R.color.background));
btnDone.setBackgroundColor(getResources().getColor(R.color.background));
btnDone.setTextColor(getResources().getColor(R.color.white));
} else {
var = 0;
btnAll.setBackgroundColor(getResources().getColor(R.color.background));
btnAll.setTextColor(getResources().getColor(R.color.white));
btnDone.setBackgroundColor(getResources().getColor(R.color.white));
btnDone.setTextColor(getResources().getColor(R.color.background));
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linear_layout);
btnAll = findViewById(R.id.btn_all);
btnDone = findViewById(R.id.btn_done);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
btnAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggle();
}
});
}
}
I searched a lot to find a direct and clear solution for I think a popular problem but unfortunately I couldn't find it.
We want to have a list of cardviews so each card bind to a specific data and each card has a list inside that shows meta or detail data about its parent.
So we have a nested list inside a cardview.
With a simple search, we know that we should use expanded list view which parents items are cardviews and we must have another layout for its child.
So when you click on cards a list of items appears below of your cards on the root.
But we want to show child list inside of cards?
And there is no access to the something like child list id or any other things to refer to a transition animation and change of layout.
So the clear question is how to add a listview inside a cardview?
i follow my work by using tablelayout and table row. and prefer not to use external libraries for stability and version problems.
this is what i mean.
Image that describe my question
If you don't want to use an external library, you can make your CardView expand like this:
Layout file for an expandable CardView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
>
<TextView
android:id="#+id/textView_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<!--My dropdown Button -->
<RelativeLayout
android:id="#+id/button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end"
android:layout_alignParentRight="true"
android:gravity="center"
>
<View
android:layout_width="12dp"
android:layout_height="12dp"
android:background="#drawable/triangle"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false" />
</RelativeLayout>
</RelativeLayout>
<!--The layout below is my ExpandableLayout -->
<LinearLayout
android:id="#+id/expandableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imageView_Owner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Owner"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView_OwnerUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My ExpandableRecyclerAdapter Class
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
private List<Repo> repos;
private SparseBooleanArray expandState = new SparseBooleanArray();
private Context context;
public ExpandableRecyclerAdapter(List<Repo> repos) {
this.repos = repos;
//set initial expanded state to false
for (int i = 0; i < repos.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ExpandableRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
this.context = viewGroup.getContext();
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_card_row, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ExpandableRecyclerAdapter.ViewHolder viewHolder, final int i) {
viewHolder.setIsRecyclable(false);
viewHolder.tvName.setText(repos.get(i).getName());
viewHolder.tvOwnerLogin.setText("Owner: " +repos.get(i).getOwner().getLogin());
viewHolder.tvOwnerUrl.setText(repos.get(i).getOwner().getUrl());
Picasso.with(context)
.load(repos.get(i).getOwner().getImageUrl())
.resize(500, 500)
.centerCrop()
.into(viewHolder.ivOwner);
//check if view is expanded
final boolean isExpanded = expandState.get(i);
viewHolder.expandableLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.buttonLayout.setRotation(expandState.get(i) ? 180f : 0f);
viewHolder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayout, i);
}
});
}
#Override
public int getItemCount() {
return repos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvName,tvOwnerLogin, tvOwnerUrl;
private ImageView ivOwner;
public RelativeLayout buttonLayout;
public LinearLayout expandableLayout;
public ViewHolder(View view) {
super(view);
tvName = (TextView)view.findViewById(R.id.textView_name);
tvId = (TextView)view.findViewById(R.id.textView_id);
tvUrl = (TextView)view.findViewById(R.id.textView_url);
tvOwnerLogin = (TextView)view.findViewById(R.id.textView_Owner);
tvOwnerUrl = (TextView)view.findViewById(R.id.textView_OwnerUrl);
ivOwner = (ImageView) view.findViewById(R.id.imageView_Owner);
buttonLayout = (RelativeLayout) view.findViewById(R.id.button);
expandableLayout = (LinearLayout) view.findViewById(R.id.expandableLayout);
}
}
private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {
//Simply set View to Gone if not expanded
//Not necessary but I put simple rotation on button layout
if (expandableLayout.getVisibility() == View.VISIBLE){
createRotateAnimator(buttonLayout, 180f, 0f).start();
expandableLayout.setVisibility(View.GONE);
expandState.put(i, false);
}else{
createRotateAnimator(buttonLayout, 0f, 180f).start();
expandableLayout.setVisibility(View.VISIBLE);
expandState.put(i, true);
}
}
//Code to rotate button
private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(new LinearInterpolator());
return animator;
}
}
In your Activity/Fragment, set up RecyclerView like this
private RecyclerView recyclerView;
private List<Repo> data;
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//fetch data and on ExpandableRecyclerAdapter
recyclerView.setAdapter(new ExpandableRecyclerAdapter(data));
So, its quite simple to create an expandable CardView without external Library. The code is almost exactly the same as using a normal CardView with RecyclerView, the main change is setting the View.GONE/View.VISIBLE on button click.
You can use ExpandLayout,and then add it in cardview.
<com.kyo.expandablelayout.ExpandableLayout
android:id="#+id/expandlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp" >
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/child"
app:canExpand="true" />
</com.kyo.expandable.ExpandableLayout>
I'm trying to make a workout like App. I have a full screen ImageView and a ImageButton with a play icon. I want that when I click on the ImageButton the icon changes to a stop icon immediately, and the ImageView shows the image of the first exercise that will change after lets say 20secs to the image of the second exercise, but if a want to pause the counter I click again on the ImageButton to stop it.
My layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF5722"
android:orientation="vertical"
android:id="#+id/linearLayout">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Inicio"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:layout_margin="8dp"
android:layout_marginLeft="16dp"
android:id="#+id/Boton1"
android:onClick="sendMessage"
android:clickable="true"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:layout_below="#+id/linearLayout"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imagenRutina"
android:layout_marginBottom="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff5722"
android:orientation="horizontal"
android:id="#+id/linearLayout3">
</LinearLayout>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imageButton"
android:layout_alignTop="#+id/linearLayout3"
android:layout_centerHorizontal="true"
android:background="#ff5722"
android:clickable="true"
android:layout_marginTop="5dp"
/>
</RelativeLayout>
</RelativeLayout>
And this is my java file
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Rutina1Reproduccion extends AppCompatActivity {
ImageButton mPlayButton;
boolean isPlay = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rutina1_reproduccion);
mPlayButton = (ImageButton) findViewById(R.id.imageButton);
background="#drawable/default"
mPlayButton.setBackgroundResource(R.drawable.playicon);
mPlayButton.setOnClickListener(mTogglePlayButton);
}
View.OnClickListener mTogglePlayButton = new View.OnClickListener(){
final int[] imageArray = {R.drawable.imagengemelos, R.drawable.imagengluteos, R.drawable.imagenbrazos};
final ImageView imageView = (ImageView) findViewById(R.id.imagenRutina);
final Handler handler = new Handler();
#Override
public void onClick(View v) {
if (isPlay) {
v.setBackgroundResource(R.drawable.playicon);
Runnable runnable = new Runnable() {
int i = 0;
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if (i > imageArray.length - 1) {
i = 0;
}
handler.postDelayed(this, 3000); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
}else{
v.setBackgroundResource(R.drawable.stopicon);
}
isPlay = !isPlay; // reverse
}
};
public void sendMessage(View view){
Intent intent=new Intent(Rutina1Reproduccion.this,Rutina1.class);
startActivity(intent);
}}
The error I'm getting its fatal exception whenIi open the activity.
If I also want to play some sound when the ImageButton is clicked, how can I do it?
Well i have one button and one ImageView in my app.
What i am trying to do is when i pressing on the button then the image at the ImageView will change.
All i have are two pics file.
What i am trying to do is - if the first pic is linked to the ImageView than change it to pic2 by clicking on the button, and if pic2 is linked than a click on the button will change it back to the first pic file.
here's the onClick method i tried to use:
public void onClick(View v) {
ImageView ib1 = (ImageView)findViewById(R.id.imageView1)
View p1 = findViewById(R.drawable.pic1);
if(ib1.getResources()==R.drawable.pic1){
ib1.setImageResource(R.drawable.pic2);
}else{
ib1.setImageResource(R.drawable.pic1);
}
}
Thanks for any kind of help
Rather than checking the image, I would suggest set the information tag of the ImageView each time you change the image, like:
if(ib1.getTag() != null && ib1.getTag().toString().equals("pic1")){
ib1.setImageResource(R.drawable.pic2);
ib1.setTag("pic2");
} else {
ib1.setImageResource(R.drawable.pic1);
ib1.setTag("pic1");
}
private ImageView ib1;
private int currentImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ib1 = (ImageView) findViewById(R.id.imageView1);
currentImage = R.drawable.pic1;
}
public void onClick(View view){
currentImage = (currentImage == R.drawable.pic1) ? R.drawable.pic2 : R.drawable.pic1;
ib1.setImageResource(currentImage);
}
You can also create a Boolean variable and assign it true and then if the boolean variable is true you change the picture of image button and set the boolean variable to false:- here my code example in java:-
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton play_pause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_pause = findViewById(R.id.play_pause_button);
final boolean[] play_or_pause = new boolean[1];
play_pause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
play_or_pause[0] = true;
play_pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (play_or_pause[0]) {
play_pause.setImageResource(R.drawable.ic_baseline_pause_24);
play_or_pause[0] = false;
}else {
play_pause.setImageResource(R.drawable.ic_baseline_play_arrow_24);
play_or_pause[0] = true;
}
}
});
}
}
and xml code:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#color/black"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="252dp"
android:layout_height="48dp"
android:gravity="center"
android:text="TBT Music Player"
android:textColor="#color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="247dp"
android:layout_height="263dp"
android:src="#drawable/ic_baseline_library_music_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.124" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="327dp"
android:layout_height="41dp"
android:thumbTint="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:layout_constraintVertical_bias="0.163"
android:secondaryProgressTint="#color/white"/>
<ImageButton
android:id="#+id/play_pause_button"
android:layout_width="64dp"
android:layout_height="77dp"
android:backgroundTint="#color/black"
android:src="#drawable/ic_baseline_play_arrow_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/seekBar"
app:layout_constraintVertical_bias="0.213" />
</androidx.constraintlayout.widget.ConstraintLayout>