Android Multiple Button and Color Example
Actually in This example i am going to explain several topic:
- Use Of Multiple Button
- Giving click event via XML File
- Accessing Color using import android.graphics.Color class
- Accessing Color property from value>color.xml file
step 1: create an color.xml file in res/values/ folder
step 2: create an activity_MultiButton.xml in res/layout folder
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
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=".Button1Activity" >
step3: Create MultiButtonActivity class
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.TableLayout;
public class MultiButtonActivity extends Activity {
TableLayout background;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button1);
background = (TableLayout)findViewById(R.id.TableLayout1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.button1, menu);
return true;
}
public void setColor(View v)
{
switch(v.getId())
{
case R.id.button1 :
background.setBackgroundColor(getResources().getColor(R.color.red));
break;
case R.id.button2 :
background.setBackgroundColor(Color.BLUE);
break;
case R.id.button3 :
background.setBackgroundColor(getResources().getColor(R.color.black));
break;
case R.id.button4 :
background.setBackgroundColor(Color.WHITE);
break;
case R.id.button5 :
background.setBackgroundColor(getResources().getColor(R.color.gold));
}
}
No comments:
Post a Comment