Monday, June 10, 2013

[android help] Change GRAVIITY and SIZE of View programmatically


Change GRAVIITY and SIZE of View programmatically



I would like to change the size of my View (CanvasView extends View) programmatically maintaining gravity. My View is inside FrameLayout, which is inside of RelativeLayout. I tried many different approaches but I am stuck...


The point is, that I would like to set size depending on screen size:



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ToolsAndConstants.metrics = new DisplayMetrics();

windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(ToolsAndConstants.metrics);

setContentView(R.layout.activity_dictionary);

CanvasView canvasView = (CanvasView) findViewById(R.id.activity_dictionary_CanvasView);
canvasView.canvasWidth = (ToolsAndConstants.metrics.widthPixels / 10 * 7) - 10;
canvasView.canvasHeight = (ToolsAndConstants.metrics.heightPixels / 10 * 4) - 10;
canvasView.setLayoutParams(new RelativeLayout.LayoutParams(canvasView.canvasWidth, canvasView.canvasHeight));

CanvasView canvasView = (CanvasView) findViewById(R.id.activity_dictionary_CanvasView);
canvasView.canvasWidth = (ToolsAndConstants.metrics.widthPixels / 10 * 7) - 10;
canvasView.canvasHeight = (ToolsAndConstants.metrics.heightPixels / 10 * 4) - 10;
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ToolsAndConstants.metrics.widthPixels / 10 * 7, (ToolsAndConstants.metrics.heightPixels / 10 * 4) - 10, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
FrameLayout fl = (FrameLayout) findViewById(R.id.activity_dictionary_frameLayout1);
fl.setLayoutParams(params);
canvasView.setLayoutParams(params);

(...)
}


XML file:



xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dictionary_relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gray_patterns_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LogReader" >

android:id="@+id/activity_dictionary_linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >

android:id="@+id/activity_dictionary_progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />

android:id="@+id/activity_dictionary_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Loading dictionary..."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" />


android:id="@+id/activity_dictionary_frameLayout1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >

android:id="@+id/activity_dictionary_CanvasView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
class="com.example.dictionary.CanvasView"
android:orientation="vertical" />





How can I resize it?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...