5

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();
    }
}
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Chrisantics
  • 165
  • 2
  • 16
  • Where are you adding this button to your layout, please post that code too. – Salman Khakwani Apr 06 '14 at 14:03
  • @MrSMAK I have edited the code with full code, i want to create an activity that displays a gif image and be able to back to previous screen, so the button is to go to previous screen – Chrisantics Apr 06 '14 at 14:09

1 Answers1

4

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

Community
  • 1
  • 1
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • Thanks a lot @MrSMAK!! it worked!! by the way, for the android selector link you've shared, how do I set the buttons background to the xml selector file if my button is created programmatically instead of in an xml layout? thanks in advanced! – Chrisantics Apr 06 '14 at 15:55
  • 1
    My pleasure :-), You can add the selector to you `drawable` directory and set it to your button like this: `Button b = new Button(Context); b.setBackgroundResource(R.drawable.YOUR_XML_SELECTOR);` – Salman Khakwani Apr 07 '14 at 02:07
  • yeap thank you again for your answer on the button color change!!! it worked, but instead of just the button, the whole screen is set to the same button effect. why is it that way? sorry i am a newbie! – Chrisantics Apr 07 '14 at 11:30