Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Scroll to certain position on application Android example code

ScrollTo(), getTop(), getBottom(), getLeft(), getRight(), ScrollView, LinearLayout Android Java xml example.
How get position of a View.
MainClass.java

/*
public void scrollTo (int x, int y) 
Since: API Level 1 
Set the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated. 

This version also clamps the scrolling to the bounds of our child.

Parameters
x  the x position to scroll to 
y  the y position to scroll to  
  */

// sroll to top of hscrollViewMain
ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViewMain);
hscrollViewMain.scrollTo(0, 0); // scroll to application top

// get position of a View
EditText hEdit = (EditText)findViewById(R.id.username_edit);
int nY_Pos = hEdit.getTop(); // getBottom(); X_pos  getLeft(); getRight();
// scroll to top of hEdit
hscrollViewMain.scrollTo(0,nY_Pos); 


main.xml


<LinearLayout
    xmlns:android="//schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ScrollView
        android:id="@*id/scrollViewMain"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:paddingTop="5dip"
            android:paddingBottom="13dip"
            android:paddingLeft="20dip"
            android:paddingRight="20dip">
            <TextView
                android:id="@+id/message"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dip" />
            <TextView
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/login_activity_username_label" />
            <EditText
                android:id="@+id/username_edit"
                android:singleLine="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:minWidth="250dip"
                android:scrollHorizontally="true"
                android:capitalize="none"
                android:autoText="false"
                android:inputType="textEmailAddress" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

396LW NO topic_id




AD

Další témata ....(Topics)


289

How disable Choreographer in LogCat Eclipse Android | how-to-disable-choreographer-messages


choreographer skipped frames
Create filter with TAG regex to disable Choreographer messages, see code and picture below:

^((?!Choreographer).)*$


android/choreographer-disable-remove.png
293

Get Bitmap Size Get Free Memory Exception Android | get-bitmap-size-get-free-memory-exception-android


Bitmap size calculation:
bmpHeight * bmpWidth
For example:
Resolution of image 1024x860 = 880 640 pixels
If every pixel get 4 byte of memory:
880 640x4= 3 522 560 (3.5MB)

Get bitmap size without allocation of memory:

   	BitmapFactory.Options options = new BitmapFactory.Options();
        // If set to true, the decoder will return null (no bitmap), but the out... fields will still
        // be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. 
    	options.inJustDecodeBounds = true; 
    	BitmapFactory.decodeResource(getResources(), R.drawable.my_image, options);
    	int imageHeight = options.outHeight; // 1024
    	int imageWidth = options.outWidth; // 860
    	String imageType = options.outMimeType; // .jpg .png .gif
 


Get Memory size:
Make your bitmap not bigger as maxMemory size
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    float density = getResources().getDisplayMetrics().density;

    Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
    Debug.getMemoryInfo(memoryInfo);
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int freeMemory = (int) (Runtime.getRuntime().freeMemory() / 1024);
    String memMessage = String.format(
        "Free=%d kB,

MaxMem=%d kB,
Memory: Pss=%.2f MB, Private=%.2f MB, Shared=%.2f MB", freeMemory, maxMemory, memoryInfo.getTotalPss() / 1024.0, memoryInfo.getTotalPrivateDirty() / 1024.0, memoryInfo.getTotalSharedDirty() / 1024.0); ((TextView)findViewById(R.id.textViewInfo)).setText(memMessage );
205

at java.util.Arrays$ArrayList.get(Arrays.java:75) | at-java-util-arraysarraylist-getarrays-java75


Problem in Android application:

E/AndroidRuntime(416): FATAL EXCEPTION: main
E/AndroidRuntime(416): java.lang.IndexOutOfBoundsException
E/AndroidRuntime(416): 	at java.util.Arrays$ArrayList.get(Arrays.java:75)
E/AndroidRuntime(416): 	at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
E/AndroidRuntime(416): 	at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
E/AndroidRuntime(416): 	at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
E/AndroidRuntime(416): 	at android.widget.Spinner.makeAndAddView(Spinner.java:192)
E/AndroidRuntime(416): 	at android.widget.Spinner.layout(Spinner.java:151)
E/AndroidRuntime(416): 	at android.widget.Spinner.onLayout(Spinner.java:115)


Solution: check code for set and get selection

// Spinner _spin1 contain only  49 items 
// you can set max 48 ( range 0 - 48)
// 50 is IndexOutOfBoundsException
_spin1.setSelection(50);
40

Spinner ComboBox DropDown set on selected item listener setOnItemSelectedListener Java Android example | spinner-combobox-set-on-selected-item-listener-setonitemselectedlistener-java-android-example


Spinner, ArrayAdapter, setOnItemSelectedListener, onItemSelected, setDropDownViewResource

MainClass.java

public class Vypocet extends Activity {
 Spinner   hSpinnerMetry;
// .......
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

	       hSpinnerMetry = (Spinner) findViewById(R.id.idSpinnerMetry);
	        ArrayAdapter<CharSequence> adapterMetry = ArrayAdapter.createFromResource(
	        		this, R.array.metry_array, android.R.layout.simple_spinner_item);
	        adapterMetry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        hSpinnerMetry.setAdapter(adapterMetry);
			
	           hSpinnerMetry.setOnItemSelectedListener(
	                    new OnItemSelectedListener() {
	                        public void onItemSelected(
	                                AdapterView<?> parent, View view, int position, long id) {
	                        int nPos = position;
	                        }

	                        public void onNothingSelected(AdapterView<?> parent) {
	                        
	                        }
	                    });	        

      }
}



strings.xml


<string-array name="metry_array">
<item>m</item>
<item>dm</item>
<item>cm</item>
<item>mm</item>
</string-array>


179

HTC Incredible 2 | htc-incredible-2


HTC Incredible 2 cena od 6 000 KCZ Kč (únor.2012)
Spokojenost uživatelů nadprůměrná.
HTC Incredible 2 je chytrý telefon s operačním systémem Android.
HTC Incredible 2 je (22.února2012) 7. nejpoužívanějším chytrým telefonem u programu Sky Map viz tabulka.

HTC Incredible 2 - video recense