Notice that the width and height return from Button.getWidth() and Button.getHeight() will not reflect the updated size at once. You can click the "Check my size" button to check the updated size.
package com.AndroidResizeView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class AndroidResizeView extends Activity{
TextView textMyTextView, textViewWidth, textViewHeight;
Button myButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textMyTextView = (TextView)findViewById(R.id.mytextview);
textViewWidth = (TextView)findViewById(R.id.viewwidth);
textViewHeight = (TextView)findViewById(R.id.viewheight);
myButton = (Button)findViewById(R.id.mybutton);
Button btnIncWidth = (Button)findViewById(R.id.incwidth);
Button btnDecWidth = (Button)findViewById(R.id.decwidth);
Button btnIncHeight = (Button)findViewById(R.id.incheight);
Button btnDecHeight = (Button)findViewById(R.id.decheight);
btnIncWidth.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ViewGroup.LayoutParams params = myButton.getLayoutParams();
params.width++;
myButton.setLayoutParams(params);
updateSizeInfo();
}});
btnDecWidth.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ViewGroup.LayoutParams params = myButton.getLayoutParams();
params.width--;
myButton.setLayoutParams(params);
updateSizeInfo();
}});
btnIncHeight.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ViewGroup.LayoutParams params = myButton.getLayoutParams();
params.height++;
myButton.setLayoutParams(params);
updateSizeInfo();
}});
btnDecHeight.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ViewGroup.LayoutParams params = myButton.getLayoutParams();
params.height--;
myButton.setLayoutParams(params);
updateSizeInfo();
}});
myButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
updateSizeInfo();
}});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
updateSizeInfo();
}
private void updateSizeInfo(){
textViewWidth.setText(String.valueOf("Button's Width(pixels): " + myButton.getWidth()));
textViewHeight.setText(String.valueOf("Button's Height(pixels): " + myButton.getHeight()));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/incwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button Width++"
/>
<Button
android:id="@+id/decwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button Width--"
/>
<Button
android:id="@+id/incheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button Height++"
/>
<Button
android:id="@+id/decheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button Height--"
/>
<Button
android:id="@+id/mybutton"
android:layout_width="200px"
android:layout_height="wrap_content"
android:text="Check my size"
/>
<TextView
android:id="@+id/viewwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/viewheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
really helpful! thank youuuu
ReplyDeletethanks
ReplyDeletehai i'm sathish i need trapezoid button help me anybody. this is my mail id sathish92info@gmail.com
ReplyDelete