make a single button width to fill parent programmatically - android

How do I make a single button width to fill parent programmatically? I have done this but it cannot seem to work it is still located on top left with width just wrapping the content... here is some of the code on the button creation...
public class TEST_GIFDisplay extends Activity implements View.OnClickListener {
SurfaceView sview;
GifRun gr = new GifRun();
Button btnBack;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sview = new SurfaceView(this);
sview.setZOrderOnTop(true);
gr.LoadGiff(sview, this, R.drawable.smiley);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout1.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.VERTICAL);
linearLayout2.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1.0f;
btnBack = new Button (this);
btnBack.setText("Back");
btnBack.setBackgroundColor(Color.parseColor("#f1c40f"));
btnBack.setGravity(Gravity.CENTER_HORIZONTAL);
btnBack.setLayoutParams(p);
btnBack.setOnClickListener(this);
linearLayout2.addView(btnBack);
linearLayout1.addView(linearLayout2);
linearLayout1.addView(sview);
setContentView(linearLayout1);
}
#Override
public void onClick(View view) {
btnBack = (Button) view;
btnBack.setBackgroundColor(Color.parseColor("#2980b9")); //color change
new CountDownTimer(100, 50) {
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); // original color
}
}.start();//end color changed
finish();
}
}

You are adding the Button to linearLayout2. You should change the linearLayout2's width to MATCH_PARENT
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
I hope this helps.
P.S: You can create Button's selectors for pressed and selected states, instead of using timer to show a pressed button effect. Here is a basic link that can help you in achieving this : android button selector

Related

How to add onclicklistener to dynamically generated textViews?

I write code to generate dynamic TextViews as a user what he looked like and any time
, But the problem each text view has only one click,
And any other click on textView does not reach onClick
I found some answers but not work with my code... Please help me.
This is my code
RelativeLayout mainBackground = findViewById(R.id.mainBackground);
private void AddTextView() {
RelativeLayout.LayoutParams lparams = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final TextView tv = new TextView(this);
tv.setId(textCount);
tv.setLayoutParams(lparams);
tv.setText(text);
tv.setTextColor(colorChooser);
tv.setTextSize(size);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("textID",view.getId()+"");
}
});
this.mainBackground.addView(tv);
}

Dynamically add LinearLayout into LinearLayout

I have a issue trying to dynamically add some linear layout into a linearlayout container after user have clicked on a button.
private void AddView() {
MyView myView1 = new MyView("Name");
this.mainLinearLayout.addView(myView1);
}
This code works great in activity's onCreate method but not after handling user event.
Do you have any idea why it's not working ? (I mean nothing appears on UI)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AddView(); => works great
}
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddView(); => not working
}
});
Thanks,
I think that AddView is wrong.
private void addView() {
LayoutParams lparams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
MyView myView1 = new MyView("Name");
myView1.setLayoutParams(lparams);
this.mainLinearLayout.addView(myView1);
}

Programmatically create ImageView in Android

I followed this to create a simple animation.
The example creates manually an ImageView in the main layout, having a resource like this android:src="#anim/anim_android
The problem is, when I make a dynamic ImageView, and I set the resource like this myImageView.setImageResource(R.anim.anim_android);, the animation doesn't work, just views the first image of the sequence.
Any help?
Thanks.
you sould start the animation from you imageView :
myImageView.setImageResource(R.anim.anim_android);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myImageViewn.getDrawable();
myImageView.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
EDIT : i think you didn't add the ImageView to your Layout ( contentView of your Activity ) ; try this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rootLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rootLayout.setLayoutParams(params);
ImageView myAnimation = new ImageView(this);
RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
myAnimation.setLayoutParams(paramsImage);
myAnimation.setImageDrawable(getResources().getDrawable(R.anim.anim_android));
rootLayout.addView(myAnimation);
setContentView(rootLayout);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(new Runnable() {
#Override
public void run() {
myAnimationDrawable.start();
}
});
}

dynamically added imageviews which one is clicked on?

I have a linear layout which I fill with imageviews everytime the user takes a picture with the camera. So these imageviews are added dynamically.
To each of these imageviews I attached an OnClick event to open the picture and show it in another imageview in another activity.
Each imageview has a tag containing an arraylist item with the bitmap information.
The OnClick event:
ImageView iv = new ImageView(this);
LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(params);
iv.setImageBitmap(mBitmap);
iv.setTag(pli);
iv.setPadding(5, 5, 5, 5);
lvp.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
PhotoList pli = (PhotoList) arg0.getTag();
Intent i = new Intent(getBaseContext(), PhotoActivity.class);
i.putExtra("photo", pli.Photo);
i.putExtra("PhotoId", pli.id);
startActivity(i);
}
});
lvp.addView(iv);
Obviously the line with arg0.getTag() is not working.
Variable arg0 is of the linearlayout, but I need the clicked on imageview.
How do I detect wich imageview in the linearlayout was clicked on?
rg,
Eric
You have set the OnClickListener to the parent LinearLayout instead of the ImageView.
Instead of this:
lvp.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
...
}
});
Use this:
iv.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
...
}
});

Android : How to set TextViews positions depending on other ones programatically?

I want to put elements in my view depending on the position of the other ones. Let me explain : First, I have two textviews and I've put successfully the second (villeOffre) one under the first one (titreOffre) :
titreOffre = (TextView) findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre = (TextView) findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
infos = (TextView) findViewById(R.id.infos);
ViewTreeObserver vto = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(
{
#Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+12,15,0);
villeOffre.setLayoutParams(params);
titreOffre.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
This works perfectly, but now I want to put a third textview (infos) which is depending on villeOffre position. How to do that ?
Thanks a lot.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rr = new RelativeLayout(this);
b1 = new Button(this);
b1.setId(R.id.Button01);
recent = b1;
b1.setText("Click me");
rr.addView(b1);
setContentView(rr);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1 = new TextView(can.this);
tv1.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
recent = tv1;
}
});
}

Categories

Resources