I have button which starts the same activity. How can I get next image from array, after start activity? Here is code of button
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
finish();
startActivity(getIntent());
}
});
Here is array
int[] array_images = {
R.drawable.apple,
R.drawable.p_dolor
};
Why you exactly want to restart activity every-time, You can just change your resource on button click without Restarting the activity. For example -
int index = 0;
int[] imgRes = {R.id.image1, R.id.image2, R.id.image3, R.id.image4};
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
updateImage();
}
});
private void updateImage(){
index++;
if(index >= imgRes.lenght)
index = 0;
ImageView imageView = findViewById(R.id.myImage);
imageView.setResource(imgRes[index])
}
Hope it will help :)
You can try the below one you have to manage array indexing for that like below:-
int index = 0;
int[] array_images = {R.id.image1, R.id.image2, R.id.image3, R.id.image4};
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
ImageView imageView = (ImageView)findViewById(R.id.myImage);
setImageRes();
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
setImageRes();
}
});
private void setImageRes(){
index++;
if(index < array_images.lenght){
imageView.setResource(array_images[index])
}
}
You can do this:
int index = 0;
int[] array_images = {R.id.image1, R.id.image2};
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
ImageView imageView = (ImageView)findViewById(R.id.myImage);
setImageRes(getIntent().getIntExtra());
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
index = getIntent().getIntExtra();
dialog.dismiss();
finish();
Intent intent = new Intent(this,CallActivity.class);
intent.putExtra("position",index++);
}
});
private void setImageRes(int position){
imageView.setResource(array_images[position])
}
Related
I am working with this bingo callboard project in Android Studio. The principle is when I click a number button, it will show on the first textview. When I click the second number button, the first textview will show the current number pressed and the previous string will be passed to the second textview. How would I do this? Screenshot of app included. A sample of the MainActivity.java code is here:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
Screenshot
Hey so from what I understand you need to update the lst textview with the data of txt textView. Well you can write a common method which can be called from any button click with the new data. something like this:
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
updateTextView("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
updateTextView("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
updateTextView("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
updateTextView("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
updateTextView("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
}
private void updateTextView(String newData){
String oldData = txt.getText().toString();
lst.setText(oldData);
txt.setText(newData);
}
}
This will update your last textview with your txt textview data. YOu can call this method from your button clicks with the new data as a parameter . Hope this helps you
There is nothing wrong with p.matthew13's code. The problem is with the code sequence. I may also have to delete a line on my code because it's already redundant.
The redundant code is txt.setText(""); which contradicts txt.setText(newData);
I have to delete my setText code and replace it with the updateTextView("");
Also, I did place p.matthew13's code before my public void.
And that fixes my problem, after trying to shuffle up the code execution sequence.
Thanks very much!
I couldn't phrase the question perfectly with my problem, but basically what I want to achieve is this:-
I have recyclerview of items that contains textview for example, when the item is clicked; a custom dialog will show containing the same content of the clicked element.
so far so good, in the same dialog there's two arrow (left & right) at each of its sides, the arrows buttons should navigate between the recyclerview items and update the content according to the new position.
Here's what I did:
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.tvTitle.setText(data.get(position).getTitle());
holder.itemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Initializing the custom dailog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().getAttributes().gravity = Gravity.TOP;
dialog.setContentView(R.layout.dailog);
dialog.show();
ImageView RightArrow = (ImageView) dialog.findViewById(R.id.dia_right_arrow);
ImageView LeftArrow = (ImageView) dialog.findViewById(R.id.dia_left_arrow);
final TextView textViewTitle = (TextView) dialog.findViewById(R.id.dia_tvTtile);
textViewTitle.setText(data.get(position).getTitle());
RightArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int curPosi = 0;
if (data.size() != 0) {
curPosi = position+1;
}
textViewTitle.setText(data.get(position).getTitle());
}
});
LeftArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int curPosi = 0;
if (data.size() != 0) {
curPosi = position-1;
}
textViewTitle.setText(data.get(position).getTitle());
} });
}});
}
The Result:
When I click on the right arrow it works 100% , but if I click again nothing happens !!
And If click the left arrow at anytime it gives crash with this exception:
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
Is it possible to achieve a loop effect, what I mean is when I reach
the last index and the click again it shows the first index and vice
versa..!
Any help?
It seems the issue is from how you check your click limits. You can try this
Add curPosi as a class member variable
private int curPosi;
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.tvTitle.setText(data.get(position).getTitle());
holder.itemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
curPosi = position
//Initializing the custom dailog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow() .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().getAttributes().gravity = Gravity.TOP;
dialog.setContentView(R.layout.dailog);
dialog.show();
ImageView RightArrow = (ImageView) dialog.findViewById(R.id.dia_right_arrow);
ImageView LeftArrow = (ImageView) dialog.findViewById(R.id.dia_left_arrow);
final TextView textViewTitle = (TextView) dialog.findViewById(R.id.dia_tvTtile);
textViewTitle.setText(data.get(position).getTitle());
RightArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int lastIndex = data.size()-1;
if (lastIndex == curPosi ) { // this prevents the crash, so if the next button is clicked while the last index is showing it will reset and show the first one..!
curPosi = 0;
} else if(curPosi < data.size()) {
curPosi++;
}
textViewTitle.setText(data.get(curPosi).getTitle());
}
});
LeftArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//same thing here with this "if", when the previous button clicked from the first; it will show the last item..!
if (curPosi == 0) {
curPosi = data.size();
}
if (curPosi > 0) {
curPosi--;
}
textViewTitle.setText(data.get(curPosi).getTitle());
} });
}});
}
I have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});
I want to pass an XML resource from one activity to another activity using Java Code? I don't want to create separate different activities for different buttons.
ImageButton imageBttn = (ImageButton)findViewById(R.id.imageButton1);
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, info.class));
}
});
Pseudocode to explain what I'm trying to do:
If BUTTON_1 is clicked
-Pass swirl.png to info.class
If BUTTON_2 is clicked
-Pass golden.png to info.class
If BUTTON_3 is clicked
-Pass arcade.png
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, info.class)
intent.putExtra("image",R.drawable.ic_search_grey);
startActivity(intent);
}
});
On Mainactvitiy get the int drawable
int res = getIntent().getIntExtra("image", -1);
if(res > -1) {
Drawable drawable = getResources().getDrawable(res, null);
imgBtn.setImageDrawable(drawable);
}
InfoActivity.java
public class InfoActivity extends Activity {
private static final String EXTRA_IMAGE = "image";
public static void launch(Activity activity, #DrawableRes int imageResId) {
Intent intent = new Intent(activity, InfoActivity.class);
intent.putExtra(EXTRA_IMAGE, imageResId);
activity.startActivity(intent);
}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
int imageResId = getIntent().getIntExtra(EXTRA_IMAGE, -1);
if (imageResId == -1) {
throw new IllegalArgumentException(EXTRA_IMAGE);
// or set error/default image resource id
}
// ... something to do with imageResId
}
}
MainActivity.java
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.swirl);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.golden);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.arcade);
}
});
}
I apologize if this code looks a bit like a mess (considering the length); I figured I'd just include everything that goes on in my program at the moment.
I'm attempting to create a fairly simple Tic Tac Toe app for Android. I've set up my UI nicely so far so that there are a "grid" of TextViews. As a sort of "debug" right now, I have it so that when one clicks on a TextView, it should display the value of buttonId in a message box. Right now, it displays the correct assigned value for the first element I click, but no matter what I click afterwards, it always just displays the first value buttonID had. I attempted to debug it but couldn't exactly find a point where it would pull the old value (to the best of my knowledge, it reassigned the value).
There's a good possibility I'm missing something small, because this is my first Android project (of any note). Can someone help get different values of buttonId to appear or point out the error in my logic?
The code:
package com.TicTacToe.app;
import com.TicTacToe.app.R;
//Other import statements
public class TicTacToe extends Activity {
public String player = "X";
public int ALERT_ID;
public int buttonId;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Sets up instances of UI elements
final TextView playerText = (TextView)findViewById(R.id.CurrentPlayerDisp);
final Button button = (Button) findViewById(R.id.SetPlayer);
final TextView location1 = (TextView)findViewById(R.id.location1);
final TextView location2 = (TextView)findViewById(R.id.location2);
final TextView location3 = (TextView)findViewById(R.id.location3);
final TextView location4 = (TextView)findViewById(R.id.location4);
final TextView location5 = (TextView)findViewById(R.id.location5);
final TextView location6 = (TextView)findViewById(R.id.location6);
final TextView location7 = (TextView)findViewById(R.id.location7);
final TextView location8 = (TextView)findViewById(R.id.location8);
final TextView location9 = (TextView)findViewById(R.id.location9);
playerText.setText(player);
//Handlers for events
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
if (player.equals("X")){
player = "O";
playerText.setText(player);
}
else if(player.equals("O")){
player = "X";
playerText.setText(player);
}
//Sets up the dialog
buttonId = 0;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 1;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 2;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 3;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 4;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 5;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 6;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 7;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 8;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
location9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Sets up the dialog
buttonId = 9;
ALERT_ID = 0;
onCreateDialog(ALERT_ID);
showDialog(ALERT_ID);
}
});
}
protected Dialog onCreateDialog(int id){
String msgString = "You are on spot " + buttonId;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(msgString)
.setCancelable(false)
.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return alert;
}
}
You shouldn't be calling onCreateDialog() yourself. Android does that for you when you call showDialog().
I'm sure it only calls onCreateDialog() once and caches the value internally, which is why you're only getting the correct value the first time, and then getting the same value for the rest.
You'll have to think of another way to do what you're trying to do. Perhaps passing a different ID to showDialog() (be sure to handle that ID by creating the correct dialog in onCreateDialog()) is the way to go.
Also, don't import R manually.