Q8.Create an application with three buttons vertically aligned, on selecting a button color of the screen will change.
Android Program :
Activity_main.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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.kishanhaldar.program8.MainActivity">
<Button
android:text="Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/red"
android:layout_weight="1"
android:layout_gravity="center"
android:onClick="mycolor"/>
<Button
android:text="Blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/blue"
android:layout_weight="1"
android:layout_gravity="center"
android:onClick="mycolor"/>
<Button
android:text="Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/green"
android:layout_weight="1"
android:layout_gravity="center"
android:onClick="mycolor"/>
</LinearLayout>
MainActivity.java
package
com.kishanhaldar.program8;
import
android.graphics.Color;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.LinearLayout;
public
class MainActivity extends AppCompatActivity {
LinearLayout l;
Button r,b,g;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(LinearLayout)findViewById(R.id.layout);
}
public void mycolor(View v){
if(v.getId()==R.id.red){
l.setBackgroundColor(Color.RED);
}
if(v.getId()==R.id.blue){
l.setBackgroundColor(Color.BLUE);
}
if(v.getId()==R.id.green){
l.setBackgroundColor(Color.GREEN);
}
}
}
Comments
Post a Comment