Site icon WebArtDeveloper

How to Create Array in Java file in Android and How to Change Background color and image of Application

In this tutorial following is shown:

Code:

activity_main.xml

<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=”.MainActivity” >

<Spinner
android:id=”@+id/img”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true” />

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/img”
android:layout_alignParentTop=”true”
android:layout_marginTop=”65dp”
android:text=”Change Background Image” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/img”
android:layout_below=”@+id/img”
android:layout_marginTop=”22dp”
android:text=”Change Background Color” />

<Spinner
android:id=”@+id/clr”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView2″
android:layout_centerHorizontal=”true” />

</RelativeLayout>

 

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity{

final String color[]={“Select”,”Red”,”Green”,”Blue”,”Yellow”,”Cyan”};
final String[] image={“Select”,”Desert”,”JellyFish”,”Koala”,”LightHouse”,”Penguins”};
Spinner img,clr;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(Spinner)findViewById(R.id.img);
clr=(Spinner)findViewById(R.id.clr);
final View r=img.getRootView();
ArrayAdapter <String> ad1=new ArrayAdapter <String> (this,android.R.layout.simple_dropdown_item_1line,color);
ArrayAdapter <String> ad2=new ArrayAdapter <String> (this,android.R.layout.simple_dropdown_item_1line,image);

img.setAdapter(ad2);
clr.setAdapter(ad1);

img.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(img.getSelectedItem().equals(“Desert”))
{
r.setBackgroundResource(R.drawable.desert);
}
else if(img.getSelectedItem().equals(“JellyFish”))
{
r.setBackgroundResource(R.drawable.jellyfish);
}
else if(img.getSelectedItem().equals(“Koala”))
{
r.setBackgroundResource(R.drawable.koala);
}
else if(img.getSelectedItem().equals(“LightHouse”))
{
r.setBackgroundResource(R.drawable.lighthouse);
}
else if(img.getSelectedItem().equals(“Penguins”))
{
r.setBackgroundResource(R.drawable.penguins);
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

clr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(clr.getSelectedItem().equals(“Red”))
{
r.setBackgroundColor(Color.RED);
}
else if(clr.getSelectedItem().equals(“Green”))
{
r.setBackgroundColor(Color.GREEN);
}
else if(clr.getSelectedItem().equals(“Blue”))
{
r.setBackgroundColor(Color.BLUE);
}
else if(clr.getSelectedItem().equals(“Yellow”))
{
r.setBackgroundColor(Color.YELLOW);
}
else if(clr.getSelectedItem().equals(“Cyan”))
{
r.setBackgroundColor(Color.CYAN);
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

https://youtu.be/uv4VVXeYygo

Exit mobile version