Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android C# TextView.setGravity

first question here.

I am trying to develop an app using Xamarin and targetting android with C#. My problem starts when i instantize an instance of a TextView and try to manipulate it via code:

text = new TextView(context);
text.LayoutParameters = new GridView.LayoutParams(hwidth, hheight);
text.SetPadding(8, 8, 8, 8);

now i have been reading the android API and i need to change the gravity of this Textview so that the text is in the center but when i try:

text.setGravity(Gravity.CENTER); 

It simply errors saying that Android.Widget.TextView does not contain a definition for 'setGravity'. Am i missing something ridiculously simple or what?

All help appreciated!

like image 638
Jarvis Avatar asked Jun 10 '13 14:06

Jarvis


People also ask

Is Android C or Java?

Though Java is basically used for Android game development, Android Studio has added Native Development Kit (NDK) to enable developers to use C++.

Does Android use C?

The Android Native Development Kit (NDK): a toolset that allows you to use C and C++ code with Android, and provides platform libraries that allow you to manage native activities and access physical device components, such as sensors and touch input.

Is Android made with C++?

C++ C++ can be used for Android App Development using the Android Native Development Kit(NDK). However, an app cannot be created totally using C++ and the NDK is used to implement parts of the app in C++ native code.

What C library does Android use?

Bionic is an implementation of the standard C library, developed by Google for its Android operating system.


2 Answers

As @jarvis mentioned in the Comment

TextView.Gravity = GravityFlags.Center;

This will work.

like image 56
Amalan Dhananjayan Avatar answered Sep 23 '22 15:09

Amalan Dhananjayan


You have to set LAYOUT gravity instead of gravity, you can try :

params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);

xamarin allows you to use XML files ? If yes, you can set gravity into:

<TextView
    android:id="@+id/smiley"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
     />

Hope this help

like image 35
Adrien Cerdan Avatar answered Sep 25 '22 15:09

Adrien Cerdan