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.

Create TextView dynamically Android sample


AD MOB

Example source code for Android Developers

// clickable TextView
public TextView createTextView(String sText, Context con){
 TextView b = null;
 try {
  b = new TextView (con);
  b.setTextSize(15.0f);
  b.setTextColor(Color.rgb( 0, 0, 200));
  b.setOnClickListener(this);
  b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  LayoutParams.WRAP_CONTENT));
  b.setText(sText);
  //tr.addView(b, 60,30);
  } catch (Exception e) {
  e.printStackTrace();
 return b;
 }
return b;
}
/*****************/
public void onClick(View view) {
 try {
  String s = ((TextView) view).getText().toString();
 }
 catch (Exception e1) {
   e1.printStackTrace();
 }
}
/***********/

// if you want restore in TextView after chagne of orientation
// you have to put  code to Manifest.xml android:configChanges

activity android:name=".main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation" //this line important !!!!!!!


397LW NO topic_id




AD

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


17

double in Java example for Android development | double-in-java-example-for-android-development


Java double is 64 bit double precision type used when fractional
precision calculation is required.

Java double je datový typ (reálné číslo) o velikosti 64 bitů. Používá se například pro přesný výsledek po dělení za desetinnou tečkou. Pokud nepotřebuje tak veliké číslo použijte raději typ float, šetříte tím paměť mobilního telefonu.


		// declaration and assignment of value  type double
		double x = 18.41785;
		//print formated  value
		System.out.printf("The value of x is %.3f%n", x); // 18.418
                
                // declaring more variables in single statement
                double d1 = 12.4, d2 = 564.5, d3 = 14.589;

		// double range of value
		System.out.println(Double.MIN_VALUE); // 4.9E-324
		System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308

		// is NaN  Not-a-Number
		double f = (double) Math.sqrt(-15);
		boolean bNaN = Double.isNaN(f);
		System.out.print(bNaN); // true
		
                // check if a string is a valid number in Java example
                // convert string to double Java example
		String sD = "12.8";
		double dParse = Double.parseDouble(sD);

	       // convert strings to numbers
	      String sDl = "15.48";
	      double dFromString = (Double.valueOf(sDl)).doubleValue();

// format double, float or long value to string
	DecimalFormat formatter = new DecimalFormat(".##");
	String s = formatter.format(-.5678);             // -0.57
// .###  -0.568
// .#### -0.5678
// .000000  -.567800

// -123.456
// .##  -123.46
// #.##  -123.46

// #E0  -.1E3
// ##E0  -1.2E2
//###E0  -123E0

// double to string in Java example code
Double dObj = new Double(68.5);
String str = dObj.toString();
// else 
Double dS = 11.6;
String sdouble = dS.toString();

// compare two double variables
	      Double dComp1 = 4.3;
	      if(dComp1.equals(4.3))
	  		System.out.print("true");

	// compares the two specified double values in Java example
	// int i =	compare(double d1, double d2);
	    int i =	Double.compare(11.5, 11.7); // -1 first < second
	    // 0 first == second
	    // 1 first > second
	    System.out.print(i);


297

Shared Preferences file path Android | shared-preferences-file-path-android



/data/data/<package>/shared_prefs/<package>_preferences.xml

String TAG = "MyActivityName";
File f = new File("/data/data/"+this.getPackageName()
+"/shared_prefs/"+this.getPackageName()+"_preferences.xml");
if(f.exists())
	Log.d(TAG, "exist");
else
	Log.d(TAG, "not exist");

72

While cycle loop in Java example | while-cycle-loop-in-java-example


While cycle Java basic example with array of strings.

MainClass.java

public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = { "Hello", "people", "hello", "world!" };
                int i = 0;
		while ( i < arrayOfString.length ){
			System.out.println(arrayOfString[i]);
			i++;
		}
	}
}
341

Android widget SearchView in onCreateOptionsMenu is null | android-widget-searchview-in-oncreateoptionsmenu-is-null


If your class extends AppCompatActivity
public class SearchableActivity extends AppCompatActivity


Check if linked appropriate library for:
import android.support.v4.view.MenuItemCompat;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

Check if you have to properly sett SearchView in menu xml for onCreateOptionsMenu menu_main.xml

<menu xmlns:android="//schemas.android.com/apk/res/android" xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
tools:context=".SearchableActivity">
<item

  android:id="@+id/menu_search_my"

android:title="@string/menu_search"
android:icon="@drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"

/></menu>

Check your SearchableActivity onCreateOptionsMenu
   @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") @Override

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView)
MenuItemCompat.getActionView(menu.findItem(R.id.menu_search_my));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(null!=searchManager ) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setIconifiedByDefault(false);
}
return true;
}
369

ClassCastException fragments.MainActivity must implement OnHeadlineSelectedListener | classcastexception-fragments-mainactivity-must-implement-onheadlineselectedlistener


Problem:
You have two class with similar names HeadLinesFragment and HeadMyLinesFragment with OnHeadlineSelectedListener.
Check if call correct class in MainActivity.
For example if use HeadMyLinesFragment change implement to HeadMyLinesFragment too!


public class MainActivity extends FragmentActivity 
        implements HeadLinesFragment.OnHeadlineSelectedListener { 
// wrong implements you need correct class name 
//implements  HeadMyLinesFragment.OnHeadlineSelectedListener
//............
HeadMyLinesFragment firstFragment = new HeadMyLinesFragment(); // because in code using HeadMyLinesFragment



Editace: 2011-10-22 07:28:43
Počet článků v kategorii: 397
Url:create-textview-dinamically

AD
11 Share
Share
Tweet