Error: Cannot find symbol variable when trying to establish imageviews - java

So I have an ImageView in my xml file that is an image I created. The id of it is gameplayTop. In my java file for that activity, I try to set a variable to set that imageview, but then when I start up my emulator it says "Error: cannot find symbol variable gameTop".
Here is my 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.ics466.brainscrew.GameplayActivity"
android:background="#color/background">
<Chronometer
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/chronometer"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gameplay_Area"
android:src="#drawable/playarea"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="250dp"
android:id="#+id/gameLeft"
android:src="#drawable/gameplay_left_blue"
android:layout_alignParentStart="true"
android:layout_centerVertical="true" />
<ImageView
android:layout_width="250dp"
android:layout_height="100dp"
android:id="#+id/gameTop"
android:src="#drawable/gameplay_top_blue"
android:maxWidth="250dp"
android:maxHeight="95dp"
android:layout_marginTop="120dp"
android:layout_below="#+id/chronometer"
android:layout_centerHorizontal="true"
android:layout_gravity="right" />
</RelativeLayout>
And here is my javacode (with stuff cut out, but mainly I'm concerned with my imageview)
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class GameplayActivity extends AppCompatActivity {
ImageView top, bot, left, right;
String message;
private android.widget.RelativeLayout.LayoutParams layoutParams;
/**
* Sets the screen layout and sets this screen to fullscreen-mode.
*
* #param savedInstanceState saved previous state.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gameplay);
top = (ImageView) findViewById(R.id.gameTop);
Random rnd = new Random(2);
int colorIndex = rnd.nextInt(8);
makeBorders(rand4());
}
Do I need to do something else to have it recognize the id?

Check your Xml files, and make sure they are properly closed />

Related

Getting error in changing co-ordinates of imageview in android studio

I am a beginner to android. I want to slide a slidebar in a horizontal linear layout but when I run the code, the application ends giving error FallingBall keeps stopping
My xml:
<?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=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:weightSum="100"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="72"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="horizontal">
<ImageView
android:id="#+id/slideBar"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="#android:drawable/bottom_bar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:orientation="horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/leftBut"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/left_but"
android:onClick="hitLeft"/>
<ImageButton
android:id="#+id/rightBut"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/right_but"
android:onClick="hitRight"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My java file is like this:
package com.example.fallingball;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView s = findViewById(R.id.slideBar);
float x = s.getX();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
void hitLeft(View view){
x--;
s.setX(30);
}
void hitRight(View view){
x++;
s.setX(40);
}
}
Note: leftBut and rightBut are two Image button and I have copied the images to the drawable folder so if you are trying this code on your system, make sure you adjust this else it will show you Image not found or so...
In MainActivity.java you should initialize your value into onCreate() method
ImageView s;
float x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
s = findViewById(R.id.slideBar);
x = s.getX();
}
Well, I found my own mistake. The thing what hossein answered earlier was right but the application was still crashing. Actually the problem was coming that I was triggering the methods hitLeft and hitRight which were not initialized as public. That was the mistake and now it is working alright...

Method is missing or has incorrect signature

I am making a simple app in which context view changes and then a toast message is displayed as input given by user. I don't understand why the app keeps on crashing when changing the context view.
Also Android studio gives this warning:
Method "Toaster" is missing in "FirstActivity" or has incorrect signature.
Here is my code:
activity_me_clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textSize="36sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="#string/intro"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/named"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="MainProcess"
android:text="#string/done" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:ignore="UselessLeaf"></LinearLayout>
</LinearLayout>
</LinearLayout>
FirstActivity.Java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
public static String owner_string;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
}
public void MainProcess(View view) {
final String TAG="DEBUG";
Log.d(TAG,"At least process started");
EditText owner = (EditText) findViewById(R.id.name);
owner_string = owner.getText().toString();
Log.d(TAG,"owner name stored");
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
setContentView(R.layout.activity_main_screen);
Log.d(TAG,"content shown");
}
}
activity_main_screen.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:orientation="vertical">
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_would_you_like_me_to_do_today"
android:textSize="18sp" />
<Button
android:id="#+id/Cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/camera" />
<Button
android:id="#+id/Mus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/music" />
<Button
android:id="#+id/QR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/scanQR" />
<EditText
android:id="#+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/order"
android:inputType="textPersonName" />
<Button
android:id="#+id/toaster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:onClick="toaster"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/toast"
android:layout_marginEnd="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
public void toaster(View view){
EditText toast = (EditText)findViewById(R.id.toastText);
String final_toast = toast.getText().toString();
Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
}
}
EDIT: As suggested, I moved the toaser function to FirstActivity.Java and deleted the MainScreen.java file as it becomes pointless to keep it. But the major problem is when I press the button (id named) the app keeps stopping.
EDIT2: I found that setContentView(R.layout.activity_main_screen) in FirstActivity.Java needs to be above this code
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
so that Activity has access to all the layout components. Thanks solved :)
You replaced the content view in the first activity with a layout that include the onClick attribute, but you have no public void toaster(View view) method there.
So, either don't use setContentView a second time, or implement that method on both Activities.
The recommended way to replace the view is Fragments, by the way
This is a wrong way to change the contents of your Activity, The main problem here is that you have onClick attribute set to "toaster", And you don't have a function called "toaster" in your FirstActivity.
Beside that, The MainScreen Activity in your code will never be used.
So, Your problem is that you set the FirstActivity contents to "activity_main_screen.xml", And FirstActivity doesn't have "toaster" method in it, When you change the context, the Activity will try to find toaster method and the app will crash because the method doesn't exist in FirstActivity.
You can solve this problem by making a method called "toaster" inside FirstActivity, But
This is a very bad practice, You can use Fragments instead, Or you can use Intent to move to another activity.
Some useful links about Fragments:
https://developer.android.com/guide/components/fragments.html
https://www.tutorialspoint.com/android/android_fragments.htm
https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
Why should I use fragment in Android?

Error in StartActivity Android

my code below is crashing. I am trying to have an app where the user inputs a word in the EditText and, after clicking the button, the app checks if the inputed word equals "hello", and if it does, the app takes the user to another activity.
The error is:
java.lang.IllegalStateException: Could not find method toHi (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.reynaldo.myapplication.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button"
android:onClick="toHi (MainActivity)" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="hi"
android:ems="10"
android:id="#+id/text"
android:layout_marginTop="15dp"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void toHi (View v){
EditText text = (EditText) findViewById(R.id.text);
String key = text.getText().toString();
if (key.equals("hello")){
Intent i = new Intent (this, Main2Activity.class);
startActivity(i);
}
}
}
The problem is with the value of onClick:
replace
android:onClick="toHi (MainActivity)"
with
android:onClick="toHi"
The problem is that your event specified in Button onClick is "toHi (MainActivity)" while you are defining method public void toHi(View view).
One thing more,there are circular dependencies in your xml file.You are specifiying layout_below in both button and edit text.
Code like this:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button"
android:layout_below="#+id/text" //Specify this here only
android:onClick="toHi" /> // Change here
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="hi"
android:ems="10"
android:id="#+id/text"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

setImageResource doesn't work in Android

I'm a beginner of java, I want to randomize few images, I use this code for that
int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
Random n = new Random();
Resources resources = getResources();
imgRandom.setImageResource(cards[n]);
and it doesn't work beacause setImageResource is red and the Android Studio says: cannot resolve symbol 'setImageResource'. Someone can help me, please?
All java code:
package eduard.alarmamotivationala;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
ImageView m_imgRandom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public ImageView getM_imgRandom() { int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
Random n = new Random();
Resources resources = getResources();
m_imgRandom.setImageResource(cards [3]);
return m_imgRandom; }
}
all XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#000000"
tools:context="eduard.alarmamotivationala.MainActivity">
<Button
android:text="Porneste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button" />
<Button
android:text="Opreste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
<TextView
android:text="Inca nu ai setat alarma"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:id="#+id/textView" />
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:headerBackground="#android:color/transparent"
android:numbersSelectorColor="#027aed"
android:id="#+id/timePicker"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
android:id="#+id/imgRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType = "centerCrop" />
</RelativeLayout>
int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
int n = new Random().nextInt(3);
imgRandom.setImageResource(cards[n]);
To generate a random number from 0 to 2
int n = new Random().nextInt(3);
puck the random image from array and set to image
imgRandom.setImageResource(cards[n]);
Thats all !!
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#000000"
tools:context="eduard.alarmamotivationala.MainActivity">
<Button
android:text="Porneste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/button" />
<Button
android:text="Opreste alarma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
<TextView
android:text="Inca nu ai setat alarma"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:id="#+id/textView" />
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.7"
android:scaleY="0.7"
android:headerBackground="#android:color/transparent"
android:numbersSelectorColor="#027aed"
android:id="#+id/timePicker"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<!--changed wrap_content to match_parent for getting the full screen image view-->
<ImageView
android:id="#+id/imgRandom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType = "centerCrop" />
</RelativeLayout>
Also try,
android:scaleType = "centerInside"
and
android:scaleType = "fitXY"
feel the change.
And in java file,
package eduard.alarmamotivationala;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing the image view
ImageView m_imgRandom = (ImageView) findViewById(R.id.imgRandom);
// choosing a random number from 0 to 2
int n = new Random().nextInt(3);
// choosing the random image from cards array using the random number
m_imgRandom.setImageResource(cards[n]);
}
}
Not tested.hope it works !!

How to show more than one picture with ImageSwitcher, Android Eclipse?

I'm trying to make the app show 5 pictures that are include in the drawable file of project, I need when the user tap the "Next" button for first time show the first picture when he tap second show the second picture and so on. but when he tap the "Back" button the previous picture show up, for example the user click 4 times "next" button then when he tap "Back" button it shows the third picture and so on.
JAVA CODE:
package com.example.tables;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory{
ImageSwitcher imgS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgS=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
imgS.setFactory(this);
Animation inShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
imgS.setInAnimation(inShow);
Animation outShow=AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
imgS.setOutAnimation(outShow);
}
#Override
public View makeView() {
ImageView tableImage=new ImageView(getApplicationContext());
tableImage.setScaleType(ImageView.ScaleType.FIT_CENTER);///mal2 alcenter
tableImage.setLayoutParams(new ImageSwitcher.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT , ActionBar.LayoutParams.WRAP_CONTENT));
return tableImage;
}
public void buttonNext(View view){
imgS.setImageResource(R.drawable.i);
}
public void buttonBack(View view){
imgS.setImageResource(R.drawable.ii);
}
}
XML CODE:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tables.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reserve table" />
<TextView
android:id="#+id/selectTableViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#f00"
android:layout_marginLeft="30dp"
android:text="Please check the button up to Reserve the table">
</TextView>
<ImageSwitcher
android:id="#+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ImageSwitcher>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="buttonNext"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="buttonBack"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Make an array of the images then add that to your image switcher. You would also need to add some type of current index constant variable so you can index the switching

Categories

Resources