sorry for annoying you but i am in my first step in android programming and need your support
the question is,
i want to change between photos using next and previous Button
i have 3 photos in an array
and 2 button (next and previous )
put when i started my app. i should press next button twice to change the photo
also in previous i should press it twice to change back
hope someone help me to improve the code down
public class SabahList extends AppCompatActivity {
ImageView img;
int[] mario = new int[]{R.drawable.image_a,R.drawable.image_b,R.drawable.image_c};
int n =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sabah_list);
img= (ImageView)findViewById(R.id.imageView2);
}
public void btu_next(View view) {
img.setImageResource(mario[n]);
if(n<2)
n++;
}
public void btu_prev(View view) {
img.setImageResource(mario[n]);
if(n>0)
n--;
}
}
Here is your logic problem. Your button's function command them set resource first, then increase/decease value, therefore when you click the second time, it will be set as previous increasingly/decreasingly.
public void btu_next(View view) {
if(n < 2)
n++;
img.setImageResource(mario[n]);
}
This answer may helpful to you if you are using normal array added images from drawable
//Your xml like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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.devobal.quitchet.SabahList"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/previous"
android:text="previous"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:text="next"/>
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
//And your Activity like this
public class SabahList extends AppCompatActivity {
ImageView imageView2;
Button previous,next;
int i=0;
private int[] textureArrayWin = {R.drawable.star1,R.drawable.star2, R.drawable.star3,};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sabah_list);
imageView2 = (ImageView)findViewById(R.id.imageView2);
previous = (Button)findViewById(R.id.previous);
next = (Button)findViewById(R.id.next);
if(i==0){
previous.setVisibility(View.GONE);
}
if (i==2){
next.setVisibility(View.GONE);
}
previous.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView2.setImageResource(textureArrayWin[i]);
i--;
if(i==0){
previous.setVisibility(View.GONE);} else{ previous.setVisibility(View.VISIBLE);}
if (i==2){
next.setVisibility(View.GONE);
} else{next.setVisibility(View.VISIBLE)}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//if you are loading from bitmap
imageView2.setImageResource(textureArrayWin[i]);
i++;
if(i==0){
previous.setVisibility(View.GONE); } else {previous.setVisibility(View.VISIBLE); }
if (i==2){
next.setVisibility(View.GONE);
} else{next.setVisibility(View.VISIBLE)}
}
});
}
}
Related
I have a problem on getting my imageview button to work onclick. what im trying to do is to make the first image invisible so that the second one behind it can appear and at the same time define int special based on what image that has been clicked.
public class TrainingActivity extends AppCompatActivity implements View.OnClickListener {
public static int special;
public static ImageView a12,b13,a12a,b13a;
#Override
protected void onCreate(Bundle savedInstanceState) {
////////////////////////////////////////////////////////////////////////
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
a12 = (ImageView) findViewById(R.id.young);
b13 = (ImageView) findViewById(R.id.old);
a12a = (ImageView) findViewById(R.id.young2);
b13a = (ImageView) findViewById(R.id.old2);
a12.setOnClickListener(this);
b13.setOnClickListener(this);
a12a.setOnClickListener(this);
b13a.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.young) {
a12.setVisibility(View.GONE);
a12a.setVisibility(View.VISIBLE);
special =0;
} else {
a12.setVisibility(View.VISIBLE);
a12a.setVisibility(View.GONE);
}
if (v.getId() == R.id.old) {
b13.setVisibility(View.GONE);
b13a.setVisibility(View.VISIBLE);
special =1;
} else {
b13.setVisibility(View.VISIBLE);
b13a.setVisibility(View.GONE);
}
}
this is my main class file. I've had already tried this codes in a new project and it works there but somehow doesn't here.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/gray_color"
tools:context="com.codestudioapps.cardioexcercise.WalkandStep.activities.TrainingActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/young2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_gravity="center_horizontal|left|center_vertical"
android:src="#drawable/img_body_surface_area1"
/>
<ImageButton
android:id="#+id/young"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_gravity="center_horizontal|left|center_vertical"
android:src="#drawable/img_body_surface_area" />
<ImageButton
android:id="#+id/old2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp"
android:layout_gravity="center_horizontal|right|center_vertical"
android:src="#drawable/img_body_fat1"
/>
<ImageButton
android:id="#+id/old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="35dp"
android:layout_gravity="center_horizontal|right|center_vertical"
android:src="#drawable/img_body_fat" />
</FrameLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
And this is the layout. The problem might happen because of my manifest file but im unsure. Does anyone have any ideas?
Your code seems okay. Just, casting problem i.e. ImageButton to ImageView.
Your code should be as follows:
public class DemoActivity extends AppCompatActivity implements View.OnClickListener {
public static int special;
public static ImageButton a12, b13, a12a, b13a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
a12 = findViewById(R.id.young);
b13 = findViewById(R.id.old);
a12a = findViewById(R.id.young2);
b13a = findViewById(R.id.old2);
a12.setOnClickListener(this);
b13.setOnClickListener(this);
a12a.setOnClickListener(this);
b13a.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.young) {
a12.setVisibility(View.GONE);
a12a.setVisibility(View.VISIBLE);
special = 0;
} else {
a12.setVisibility(View.VISIBLE);
a12a.setVisibility(View.GONE);
}
if (v.getId() == R.id.old) {
b13.setVisibility(View.GONE);
b13a.setVisibility(View.VISIBLE);
special = 1;
} else {
b13.setVisibility(View.VISIBLE);
b13a.setVisibility(View.GONE);
}
}
}
I want to move a cursor in EditText with custom left and right buttons. When I click the left button then it should go one character left and when I click a right button then it should go one character right.
I created a simple project for it. To move cursor You just can use this function: editText.setSelection(position). But before calling this function You have to check if You are at the start/end because You can get exception java.lang.IndexOutOfBoundsException.
EditText editText;
Button buttonBackward, buttonForward;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
buttonBackward = findViewById(R.id.buttonBackward);
buttonForward = findViewById(R.id.buttonForward);
buttonForward.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (editText.getSelectionEnd() < editText.getText().toString().length())
{
editText.setSelection(editText.getSelectionEnd() + 1);
}
else
{
//end of string, cannot move cursor forward
}
}
});
buttonBackward.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (editText.getSelectionStart() > 0)
{
editText.setSelection(editText.getSelectionEnd() - 1);
}
else
{
//start of string, cannot move cursor backward
}
}
});
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonForward"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Forward" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<Button
android:id="#+id/buttonBackward"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Backward" />
</LinearLayout>
This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 3 years ago.
I want to display a timer from 1 to 100 in my TextView, but this throws me an Exception. My application throws a RuntimeException like shown below:
Thanks in advance for your help.
java.lang.RuntimeException: An error occurred while executing
doInBackground()
My MainActivity class where the RuntimeException is thrown.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = findViewById(R.id.txt);
Button button1 = findViewById(R.id.bt1);
Button button2 = findViewById(R.id.bt2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new QQ().execute();
}
});
}
public void onProgressUpdate(int i) {
TextView txt = findViewById(R.id.txt);
Button button1 = findViewById(R.id.bt1);
Button button2 = findViewById(R.id.bt2);
txt.setText(i);
}
private class QQ extends AsyncTask<Integer, Integer, Integer> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Integer doInBackground(Integer... args) {
for (int i = 0; i < 1000000; i++) {
try {
Thread.sleep(1000); ///задержка
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
return null;
}
return null;
}
private void publishProgress(int i) {
publishProgressq(i);
}
protected void onPostExecute(int i) {
}
}
private void publishProgressq(int i) {
onProgressUpdate(i);
}
}
My Layout class in xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
android:gravity="top"
android:orientation="horizontal">
<TextView
android:id="#+id/txt"
android:layout_width="151dp"
android:layout_height="110dp"
android:textSize="45dp"
android:text="0">
</TextView>
<Button
android:id="#+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="START" />
<Button
android:id="#+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="STOP" />
</LinearLayout>
</LinearLayout>
You can change your code as below:
public void onProgressUpdate(int i) {
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView txt = findViewById(R.id.txt);
Button button1 = findViewById(R.id.bt1);
Button button2 = findViewById(R.id.bt2);
txt.setText(i);
}
});
}
It will run on main thread.
Inside your public void onProgressUpdate , set text like this :
txt.setText(String.valueOf(i));
If you set a TextView to an int, it will be interpreted as an Android resource id. If you want the value of the int as your text (and not the resource it points to), make it a String first.
When I click the button my app crashes and I have no idea why. I've looked up my code a lot of times but can't seem to find any "major flaws". The purpose of the app is just clicking the button twice as fast as possible and it displays the amount of time needed from the first tap to the second tap. Some sort of reaction thing. I used a timer because I didn't know what else to use.
(Value of start time) - (value of current timer) = (time between first and second click).
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#000000"
tools:context="com.keklabs.reactiontimer.MainActivity">
<TextView
android:id="#+id/scoreText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:text="Click below to start!"
android:textColor="#39FF14"
android:textSize="30dp" />
<Button
android:id="#+id/startStopButton"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#000000"
android:text="Start / Stop"
android:textSize="25dp"
android:textColor="#39FF14" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private final long startTime = 0;
private final long interval = 100;
private boolean buttonClicked;
private Button startStopButton;
private TextView scoreText;
CountDownTimer timer = new CountDownTimer(30000, 100) {
#Override
public void onTick(long millisUntilFinished) {
scoreText.setText("kek"); //displayed text is value of starttime (30000) - current value of timer,
} //some sort of reaction game thing
#Override
public void onFinish() {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView scoreText = (TextView) findViewById(R.id.scoreText);
Button startStopButton = (Button) findViewById(R.id.startStopButton);
startStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!buttonClicked) {
timer.start();
buttonClicked = true;
}
else {
timer.cancel();
buttonClicked = false;
}
}
});
}
}
EDIT
How do i do it the right way in the line scoreText.setText(startTime - millisUntilFinished); to display startTime-current timer value?
CountDownTimer timer = new CountDownTimer(startTime, interval) {
#Override
public void onTick(long millisUntilFinished) {
scoreText.setText(startTime - millisUntilFinished);
}
#Override
public void onFinish() {
}
};
Actually, you are declaring your TextView scoreText twice. One gloabally and other locally. You can replace TextView scoreText = (TextView) findViewById(R.id.scoreText);
to scoreText = (TextView) findViewById(R.id.scoreText);
in your onCreate()
I am currently experimenting with layouts and opening new pages, I would like to open one page with a OnLongClickListener but a toast with a OnClickListener using the same text view box.
This is what I've designed so far. It won't work as the long click needs to output a boolean. Any Ideas?
XML main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.how_to_use.MainActivity"
android:id="#+id/main_view">
<TextView
android:id="#+id/long_press_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Long Press To Open \n'Drag n Drop'"/>
</LinearLayout>
XML 2nd page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/swirl_img"
android:src="#drawable/Swirl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private LinearLayout mainView;
private TextView txtView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.long_press_textView);
addListenerLongPressBtn();
addListenerOnClickBtn();
}
public void addListenerOnClickBtn() {
txtView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show();
}
});
}
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(this, DragDropActivity.class);
startActivity(intent);
return true;
}
});
}
}
I found a solution to my own problem.
public void addListenerLongPressBtn() {
txtView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent intent = new Intent(view.getContext(), DragDropActivity.class);
startActivity(intent);
return true;
}
});
}