First of all create New Project and give the Name And MainACtivity name all that.
Now open the MainActivity.
MainActivity
- public class MainActivity extends AppCompatActivity {
- ImageView img;
- Button b;
- int flag = 0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- img = (ImageView) findViewById(R.id.imageView1);
- b = (Button) findViewById(R.id.button1);
- b.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if (flag == 0) {
- img.setImageResource(R.drawable.home1);
- flag = 1;
- } else if (flag == 1) {
- img.setImageResource(R.drawable.home2);
- flag = 2;
- } else if (flag == 2) {
- img.setImageResource(R.drawable.home3);
- flag = 0;
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }




Join the conversation! Your thoughts help the community grow.