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);
}
});
}
}
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.
How to Display ActionBar when Dialog is shown in Android? My ActionBar should be enabled even when dialog is appeared.
In my app it is becoming dim when dilog is appeared.
import android.app.*;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;
public class ShowPopUp extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}
Hope it helps you
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 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.
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
}
});