i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is that i can't put some background color for these buttons dynamically using code like
buttons[i][j].setTextColor(getResources().getColor(R.color.mycolor));
buttons[i][j].setBackgroundColor(getResources().getColor(R.color.mycolor));
my code is as given below, can anyone please tell me some solution for giving background color for button
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(35);
buttons[i[j].setWidth(75);
buttons[i][j].setBackgroundResource(R.color.mycolor);
buttons[i][j].setTextColor(Color.parseColor("#FFBBFF"));
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
I suspect that you need to use:
buttons[i][j].setBackgroundResource(R.color.mycolor);
That way you overwrite the existing background image (which is a Drawable) with the colour you want.
Related
I'm learning how to make UI programatically in Android. I'm trying to make an app with just one button, which when clicked, a toast pops up saying how many times the button has been clicked.
package com.example.android.dynamicui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
}
}
Please set the on click on button after the layout has been set i.e.:-
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
}
}
It was crashing because I was using getResources() method, before onCreate(), which passed a null value to the 'den' variable.
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i press one button which is defined statically which results in changing the button (text named as 5) background color to yellow.
can anyone please tell me some solution for this
my code is as given below
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
Button change;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
change= (Button) findViewById(R.id.change);
setContentView(R.layout.mymain);
createCalender();
change.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// what to write here in order to change the color of button titled as 5 to yellow as its background color
}
});
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(10, 10, 10, 10);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setBackgroundColor(Color.BLACK);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(55);
buttons[i][j].setWidth(80);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
Try something like:
if("5".equals(v.getText())){
v.setBackgroundColor(Color.YELLOW);
}
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i put some background color for these buttons dynamically the press effect of the buttons is being lost
can anyone please tell me some solution for retaining the press effect of the button click
my code is as given below
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(10, 10, 10, 10);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setBackgroundColor(Color.BLACK);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(55);
buttons[i][j].setWidth(80);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
After buttons[i][j].setBackgroundColor(Color.GREEN);
buttons[i][j].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY); // Choose whichever color
new Handler().postDelayed(new Runnable() {
public void run() {
v.setBackgroundColor(Color.GREEN);
// Button Click Code Here
}
}, 100L); // Change this value to whatever is suitable
}
});
I know this is easy but i m just not able to debug it.
I am dynamically creating 3 LinearLayouts and adding them to a scroll View . I am adding a Button "ins" to the top of 3 linearlayout.
The problem is that on clicking the ins Button , its on clicklistener is being called 3 times .
code:
package com.integrated.mpr;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Page1 extends Activity implements OnClickListener{
static int pos = new Choose().n;
static String partname;
int i;
int[][] id = new int[pos][5];
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int p =0;
for(int i =0;i<3;i++){
for(int j =0;j<5;j++){
p++;
}
}
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
int resid = getResources().getIdentifier("background", "drawable", getPackageName());
ll.setBackgroundResource(resid);
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ll.addView(ins);
for(i =0;i<3;i++){
LinearLayout llay = new LinearLayout(this);
llay.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("enter the " +(i+1)+" position name");
EditText et = new EditText(this);
et.setId(id[i][0]);
Button starta = new Button(this);
starta.setText("Record 1");
starta.setId(id[i][1]);
starta.setOnClickListener(this);
Button startb = new Button(this);
startb.setText("Record 2");
startb.setId(id[i][2]);
startb.setOnClickListener(this);
Button startc = new Button(this);
startc.setText("Record 3");
startc.setId(id[i][3]);
startc.setOnClickListener(this);
Button stop = new Button(this);
stop.setText("Submit");
stop.setId(id[i][4]);
stop.setOnClickListener(this);
TextView tv1 = new TextView(this);
tv1.setVisibility(llay.INVISIBLE);
llay.addView(tv);
llay.addView(et);
llay.addView(starta);
llay.addView(startb);
llay.addView(startc);
llay.addView(stop);
llay.addView(tv1);
ll.addView(llay);
}
Button bcon = new Button(this);
bcon.setText("Continue");
bcon.setId(10000);
bcon.setOnClickListener(this);
ll.addView(bcon);
sv.addView(ll);
this.setContentView(sv);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==5000){
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
}
}
}
I know definitely , there is some problem in the code of OnCreate but what it is ? Help please
Can anyone further explain how to set the button aligned o the right of screen , that is how to set its layout gravity?
I am not sure about what exactly you want to achieve.but you can set the flag.if flag value is 1 then only process onClick else not.
Below snippet will help you.
Yes but you will have to somehow reset that flag to 1.
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId()==5000)
{
if(flag)
{
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
flag=0;
}
}
You can also call button click event like this
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ins.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
});
I have to resize one of the views or both...
It seems that once setContentView has been called a new setCotentView cannot be called. I was trying to re size the content by creating again the whole layout.
Please tell me how to do it...
Here is the code I have so far:
package v.w;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class VariableWidthActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout RLGreen;
LinearLayout RLChange;
Button btnClick;
LinearLayout RLYellow ;
Button btClick;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RLChange = new LinearLayout(this);
RLChange.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
RLChange.setOrientation(LinearLayout.VERTICAL);
RLGreen = new LinearLayout(this);
RLGreen.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
RLGreen.setBackgroundColor(Color.GREEN);
RLGreen.setOrientation(LinearLayout.HORIZONTAL);
btnClick = new Button(this);
btnClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT) );
//btnClick.setBackgroundColor(Color.RED);
btnClick.setText("BUTTON");
RLYellow = new LinearLayout(this);
RLYellow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
200));
RLYellow.setBackgroundColor(Color.YELLOW);
RLChange.addView(RLGreen);
RLGreen.addView(RLYellow);
RLGreen.addView(btnClick);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setContentView(RLChange);
btClick=new Button(this);
btClick.setText("BUTTON");
btClick.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT) );
//btnClick.setBackgroundColor(Color.RED);
RLYellow.addView(btClick);
btClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//RLChange.forceLayout();
RLChange.removeAllViews();
RLChange.addView(RLYellow);
RLChange.addView(RLGreen);
RLGreen.addView(btnClick);
setContentView(RLChange);
}
});
}
}