Change Position Of ImageView Through onClickListener - android

I want to create a program in which when i press the Up Button the Ball image moves towards the top 1px
and when i press the Down Button the image moves to Bottom .
Now Tell me how i get this output.

I did a demo project of your requiremens, and this is how the code looks like:
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ballImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:id="#+id/upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up" />
<Button
android:id="#+id/downBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down" />
<Button
android:id="#+id/leftBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<Button
android:id="#+id/rightBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</LinearLayout>
</RelativeLayout>
Activity Code:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MyActivity extends Activity implements View.OnClickListener{
private Button upBtn;
private Button downBtn;
private Button leftBtn;
private Button rightBtn;
private ImageView ballImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_my);
init();
}
public void init(){
ballImage = (ImageView)findViewById(R.id.ballImage);
upBtn = (Button) findViewById(R.id.upBtn);
upBtn.setOnClickListener(this);
downBtn = (Button) findViewById(R.id.downBtn);
downBtn.setOnClickListener(this);
leftBtn = (Button) findViewById(R.id.leftBtn);
leftBtn.setOnClickListener(this);
rightBtn = (Button) findViewById(R.id.rightBtn);
rightBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.upBtn:{
Toast.makeText(this,"UP",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]);
ballImage.setY(locations[1]-1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
case R.id.downBtn:{
Toast.makeText(this,"DOWN",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]);
ballImage.setY(locations[1]+1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
case R.id.leftBtn:{
Toast.makeText(this,"LEFT",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]-1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
case R.id.rightBtn:{
Toast.makeText(this,"RIGHT",Toast.LENGTH_SHORT).show();
int[] locations = new int[2];
ballImage.getLocationOnScreen(locations);
ballImage.setX(locations[0]+1);
Log.i("COORD X","X: "+locations[0]);
Log.i("COORD Y","Y: "+locations[1]);
break;
}
}
}
}
The final result:
I used as image the default icon.

Related

Access string_array from activity and send selected item to another activity in android

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array

About Correct or Wrong answer

I want a test for 3-4 years old childs. I want if they click different apple , true count must plus one. And if they click one of same aplles false count must plus one. And click any apple the question and apples are must change.
My friend helped me and we did much of them. On clicks question and answers changing. But when click any apple wrong count plus one no matter it's true or false answer.
Can u pls help me?
We think we only have problems on if commands. He tried on it so much but couldn't solve the problem.
class file:
package tr.com.blogspot.etkinlikhavuzu.benimilkogretmenim;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by erisk on 31.01.2017.
*/
public class AA extends Activity implements View.OnClickListener{
TextView soru,dogrusayi,yanlissayi;
ImageView secenek1,secenek2,secenek3;
ArrayList<Soru> sorular;
Random random;
int dogruSayisi,yanlisSayisi,sayac,dogruCevap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.aa);
init();
sorulariYukle();
}
public void init(){
secenek1 = (ImageView) findViewById(R.id.secenek1);
secenek1.setOnClickListener(this);
secenek2 = (ImageView) findViewById(R.id.secenek2);
secenek2.setOnClickListener(this);
secenek3 = (ImageView) findViewById(R.id.secenek3);
secenek3.setOnClickListener(this);
soru = (TextView) findViewById(R.id.soru);
dogrusayi = (TextView) findViewById(R.id.dogrusayi);
yanlissayi = (TextView) findViewById(R.id.yanlissayi);
sorular = new ArrayList<Soru>();
random = new Random();
}
public void sorulariYukle(){
sorular.add(new Soru("Hangi Elma Yeşildir?",R.drawable.yesilelma,R.drawable.kirmizielma,R.drawable.kirmizielma,R.drawable.yesilelma));
sorular.add(new Soru("Hangi Elma Kırmızıdır?",R.drawable.kirmizielma,R.drawable.yesilelma,R.drawable.yesilelma,R.drawable.kirmizielma));
sorular.add(new Soru("Hangi Elma Yeşildir?",R.drawable.kirmizielma,R.drawable.yesilelma,R.drawable.kirmizielma,R.drawable.yesilelma));
sorular.add(new Soru("Hangi Elma Kırmızıdır?",R.drawable.yesilelma,R.drawable.yesilelma,R.drawable.kirmizielma,R.drawable.kirmizielma));
sorular.add(new Soru("Hangi Elma Yeşildir?",R.drawable.kirmizielma,R.drawable.kirmizielma,R.drawable.yesilelma,R.drawable.yesilelma));
sayac = random.nextInt(5);
soru.setText(sorular.get(sayac).getSoru());
secenek1.setImageResource(sorular.get(sayac).getSecenek1());
secenek2.setImageResource(sorular.get(sayac).getSecenek2());
secenek3.setImageResource(sorular.get(sayac).getSecenek3());
dogruCevap = sorular.get(sayac).getDogruCevap();
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.secenek1:
if(secenek1.getDrawable() == getResources().getDrawable(dogruCevap)){
dogruSayisi++;
dogrusayi.setText(String.valueOf(dogruSayisi));
}else{
yanlisSayisi++;
yanlissayi.setText(String.valueOf(yanlisSayisi));
}
sayac = random.nextInt(5);
soru.setText(sorular.get(sayac).getSoru());
secenek1.setImageResource(sorular.get(sayac).getSecenek1());
secenek2.setImageResource(sorular.get(sayac).getSecenek2());
secenek3.setImageResource(sorular.get(sayac).getSecenek3());
dogruCevap = sorular.get(sayac).getDogruCevap();
break;
case R.id.secenek2:
if(secenek2.getDrawable() == getResources().getDrawable(dogruCevap)){
dogruSayisi++;
dogrusayi.setText(String.valueOf(dogruSayisi));
}else{
yanlisSayisi++;
yanlissayi.setText(String.valueOf(yanlisSayisi));
}
sayac = random.nextInt(5);
soru.setText(sorular.get(sayac).getSoru());
secenek1.setImageResource(sorular.get(sayac).getSecenek1());
secenek2.setImageResource(sorular.get(sayac).getSecenek2());
secenek3.setImageResource(sorular.get(sayac).getSecenek3());
dogruCevap = sorular.get(sayac).getDogruCevap();
break;
case R.id.secenek3:
if(secenek3.getDrawable() == getResources().getDrawable(dogruCevap)){
dogruSayisi++;
dogrusayi.setText(String.valueOf(dogruSayisi));
}else{
yanlisSayisi++;
yanlissayi.setText(String.valueOf(yanlisSayisi));
}
sayac = random.nextInt(5);
soru.setText(sorular.get(sayac).getSoru());
secenek1.setImageResource(sorular.get(sayac).getSecenek1());
secenek2.setImageResource(sorular.get(sayac).getSecenek2());
secenek3.setImageResource(sorular.get(sayac).getSecenek3());
dogruCevap = sorular.get(sayac).getDogruCevap();
break;
}
}
}
xml file:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="tr.com.blogspot.etkinlikhavuzu.benimilkogretmenim.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp">
<Button
android:id="#+id/dogru"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/dogru" />
<Button
android:id="#+id/yanlis"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/dogru"
android:layout_marginTop="10dp"
android:background="#drawable/yanlis" />
<TextView
android:id="#+id/dogrusayi"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_above="#+id/yanlis"
android:layout_toEndOf="#+id/dogru"
android:layout_toRightOf="#+id/dogru"
android:gravity="center"
android:text="#string/dogrusayi"
android:textColor="#006600"
android:textSize="30sp" />
<TextView
android:id="#+id/yanlissayi"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignBottom="#+id/yanlis"
android:layout_toEndOf="#+id/yanlis"
android:layout_toRightOf="#+id/yanlis"
android:gravity="center"
android:text="#string/yanlissayi"
android:textColor="#990000"
android:textSize="30sp" />
<ImageView
android:id="#+id/secenek1"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/secenek2"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_alignBottom="#+id/secenek1"
android:layout_centerHorizontal="true" />
<ImageView
android:id="#+id/secenek3"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_alignBottom="#+id/secenek2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/soru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/secenek2"
android:layout_marginBottom="18dp"
android:layout_toEndOf="#+id/dogrusayi"
android:layout_toRightOf="#+id/dogrusayi"
android:gravity="center"
android:text="Hangi Elma Yeşildir?"
android:textColor="#006600"
android:textSize="40sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/siradaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textColor="#000000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="1" />
</RelativeLayout>
</RelativeLayout>
And Soru.class file
package tr.com.blogspot.etkinlikhavuzu.benimilkogretmenim;
/**
* Created by erisk on 31.01.2017.
*/
public class Soru {
String soru;
int secenek1,secenek2,secenek3,dogruCevap;
public Soru(String soru, int secenek1, int secenek2, int secenek3, int dogruCevap) {
this.soru = soru;
this.secenek1 = secenek1;
this.secenek2 = secenek2;
this.secenek3 = secenek3;
this.dogruCevap = dogruCevap;
}
public String getSoru() {
return soru;
}
public void setSoru(String soru) {
this.soru = soru;
}
public int getSecenek1() {
return secenek1;
}
public void setSecenek1(int secenek1) {
this.secenek1 = secenek1;
}
public int getSecenek2() {
return secenek2;
}
public void setSecenek2(int secenek2) {
this.secenek2 = secenek2;
}
public int getSecenek3() {
return secenek3;
}
public void setSecenek3(int secenek3) {
this.secenek3 = secenek3;
}
public int getDogruCevap() {
return dogruCevap;
}
public void setDogruCevap(int dogruCevap) {
this.dogruCevap = dogruCevap;
}
}
Thank you so much
You are comparing drawables. which never matches.
Change your if(CONDITION) to following:
if(secenek1.getDrawable().getConstantState().equals(getResources().getDrawable(dogruCevap).getConstantState())).
same way for other two cases.

change the colors of different buttons randomly at a specified time

I have 4 different buttons. I want to change the background of the buttons at a fixed time (say 1 sec i.e. one button changes its color for one sec then retains its previous color and then other button does the same and so on) in certain random pattern, and then the user will repeat this pattern. However I am unable to change the background of the buttons randomly. I know that a timer or handler will b used but I have no idea ho to use it. Can anyone post a example program that shows how to change the background of buttons randomly?
here is my xml file:
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<TextView
android:id="#+id/levelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="50dp"
android:textColor="#FFFFFF"
android:text = "" />
<TextView
android:id="#+id/countDnText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="100dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:text=""
/>
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button6"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button7"
android:layout_below="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="160dp"
android:layout_height="200dp"
android:background="#000000"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
`
here is my Activity:`
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class EasyGameActivity extends AppCompatActivity
{
private int counter;
private TextView text;
private boolean flag = false;
private Button button = null;
private int i;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_game);
startGame();
}
public void startGame()
{
counter = 3;
int temp;
final Random rand = new Random();
Handler handler = new Handler();
while(true)
{
BinaryTree binaryTree = new BinaryTree(counter);
for(int i = 0; i<counter; ++i)
{
temp = rand.nextInt(3);
// yellow color button...
if(temp == 0)
{
button = (Button) findViewById(R.id.button5);
button.setBackgroundColor(Color.YELLOW);
}
else if(temp == 1)
{
button = (Button) findViewById(R.id.button6);
button.setBackgroundColor(Color.BLUE);
}
else if(temp == 2)
{
button = (Button) findViewById(R.id.button7);
button.setBackgroundColor(Color.RED);
}
else if(temp == 3)
{
button = (Button) findViewById(R.id.button8);
button.setBackgroundColor(Color.GREEN);
}
//button.setBackgroundColor(Color.BLACK);
}
break;
}
}
}`
You could try something like this:
Button[] changingButtons = new Button[4];
changingButtons[0] = (Button)findViewById(R.id.button1_id);
changingButtons[1] = (Button)findViewById(R.id.button2_id);
changingButtons[2] = (Button)findViewById(R.id.button3_id);
changingButtons[3] = (Button)findViewById(R.id.button4_id);
You can then access the corresponding button in the array and change the background colour using changingButtons[<desired index>].setBackgroundResource(<color resource>)
To retain the previous color, you can use ColorDrawable previousBackground = (ColorDrawable)changingButtons[<desired index>].getBackground();
Then, for the "set time" part, use a timer, and do the colour switching in the TimerTask.
If you wish to use a TimerTask would look something like this inside the method calling it:
timer = new Timer();
timer.schedule(new MyTask(buttonNumber), numMilliseconds);
And then MyTask would be an extension class of TimeTask
class MyTask extends TimerTask
{
private int buttonId;
public MyTask(int buttonId)
{
super();
this.buttonId = buttonId;
}
public void run()
{
//Do your button alterations here
}
}
Here's a sample I made using the above code on Ideone
Hopefully this points you in the right direction!
For more information, check out these: Changing the background color programmatically, Getting the background color, Java Timer documentation
You can use Collections.shuffle(colorList);
List<String> colorList=new ArrayList<>();
colorList.add("#108E44");
colorList.add("#000000ff");
colorList.add("#108E44");
for() {
giveMeButtons(i).setBackgorundColor(Color.parseColor(colorList.get(i)));
}
And you must make other method. It will return random View button.

debug error : confirm perspective switch

when i click on button add debug error
and opens confirm perspective switch dialog box
shows error in dis line " EditText add = (EditText) d1.findViewById(R.id.add); "
what is the mistake in my code??
xml page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question1" >
</TextView>
<EditText
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<EditText
android:id="#+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<Button
android:id="#+id/registerques"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="registerques" >
</Button>
</LinearLayout>
java class
showing error in editext line
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Memo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Register Questions");
d1.setContentView(R.layout.memo);
d1.show();
Button view1 = (Button) d1.findViewById(R.id.view);
Button add = (Button) d1.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText add = (EditText) d1.findViewById(R.id.add);
EditText view = (EditText) d1.findViewById(R.id.view);
System.out.println(add.toString());
System.out.println(view.toString());
}
});
view1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), View.class);
startActivity(intent);
}
});
}
}
Probably da error in the code is that you never defined an id "add" - you have #+id/TextView01 in the xml "page" (you mean file - and you must tell us which file) but no #+id/add

spinner and button in android with clickhandler

i try make spinner and button with clickHandler
this is my nyoba.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class nyoba extends Activity {
String[] nama_hari = {"Senin", "Selasa", "Rabu","Kamis","Jum'at","Sabtu","Minggu"};
/** Called when the activity is first created. */
Spinner spinner1;
Button btnSubmit;
TextView textView1;
String tes;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSubmit = (Button)findViewById(R.id.btnSubmit);
textView1 = (TextView)findViewById(R.id.textView1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, nama_hari);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
tes = nama_hari[arg2];
}
public void clickHandler(View view){
switch (view.getId()){
case R.id.btnSubmit:
textView1.setText(tes);
break;
}
}
}
and this is my main.xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickHandler"
android:text="Submit" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
but when i run it to emulator,and i choose 1 option in spinner and click the button , the TextView doesn't show up the word from spinner which i choose
(for example, i choose "senin" at spinner, and click the button, but the text "senin" doesn't appear in the TextView )
i check DDMS and there are no error.....
is the code wrong?
can you help me please which one is wrong?
Thank anyway....
I think you don't set the selection listener on your spinner.
But actually you can just retrieve the spinner's selected position with getSelectedItemPosition in the click handler of your button:
Try this:
public void clickHandler(View view) {
switch (view.getId()) {
case R.id.btnSubmit:
textView1.setText(nama_hari[spinner1.getSelectedItemPosition()]);
break;
}
}

Categories

Resources