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.

Iterate over each Entry in Map Java Android


entry put iterate Map HashMap Java Android

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("key27", 27);
for (Map.Entry<String, Integer> entry : map.entrySet())
{
 String str = entry.getKey();
 int n = entry.getValue();
}

397LW NO topic_id



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


245

Cross Button in EditText Android | cross-button-in-edittext-android


Cross Button in EditText Android for deleting clearing text in EditText Example source code:
Example allow delete text in EditText by cross button, or do Button click performance.

main.xml type your package name and class
Put into drawable folder cross and ok image.

       <cz.okhelp.wiktionary.CustomEditText
            android:id="@+id/editTextZadejSlovo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:ems="10"
            android:hint="TypeAndPressGreen"
            android:singleLine="true"
            android:lines="1"
            android:maxLines="1" 
            android:drawableLeft="@drawable/cross"
            android:drawableRight="@drawable/ok" />
<!--button is invisible 0 height 0 width for performance click on button in EditText-->
        <Button
            android:id="@+id/btnGO"
            android:layout_width="0sp"
            android:layout_height="0sp"
            android:layout_weight="0"
            android:text="GO" />




CustomEditText.java

package cz.okhelp.wiktionary; // your package name

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.EditText;

public class CustomEditText extends EditText
{
  private Drawable dLeft,dRight;
  private Rect lBounds,rBounds;
  private static Button btnOk;

  public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public CustomEditText(Context context) {
    super(context);
  }

  @Override
  public void setCompoundDrawables(Drawable left, Drawable top,
      Drawable right, Drawable bottom)
  {
   if(left !=null) {
    	dLeft = left;
    }
   if(right !=null){
      dRight = right;
    } 
    
    super.setCompoundDrawables(left, top, right, bottom);
  }

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_ENTER) {
    	btnOk.requestFocus();
    	btnOk.performClick();

		}
		return super.onKeyUp(keyCode, event);
	}
  
  @Override
  public boolean onTouchEvent(MotionEvent event)
  {
  	final int x = (int)event.getX();
  	final int y = (int)event.getY();

  	if(event.getAction() == MotionEvent.ACTION_UP && dLeft!=null) {
  		lBounds = dLeft.getBounds();
  		
  		int n1 = this.getLeft();
  		int n2 = this.getLeft()+lBounds.width();
  		int n3 = this.getPaddingTop();
  		int n4 = this.getHeight()-this.getPaddingBottom();
  		// leva strana
  		if(    x>=(this.getLeft()) 
  				&& x<=(this.getLeft()+lBounds.width())
  				&& y>=this.getPaddingTop() 
  				&& y<=(this.getHeight()-this.getPaddingBottom()))
  		{
  			this.setText("");
  			event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
  		}
  	}
  	if(event.getAction() == MotionEvent.ACTION_UP && dRight!=null)
    {
      rBounds = dRight.getBounds();
      int n1 = this.getRight()-rBounds.width();
      int n2 = this.getRight()-this.getPaddingRight();
      int n3 = this.getPaddingTop();
      int n4 = this.getHeight()-this.getPaddingBottom();
      // prava strana
      if(x>=(this.getRight()-rBounds.width()) && x<=(this.getRight()-this.getPaddingRight())
      		&& y>=this.getPaddingTop() && y<=(this.getHeight()-this.getPaddingBottom()))
      {
      	btnOk.requestFocus();
      	btnOk.performClick();
      	event.setAction(MotionEvent.ACTION_CANCEL);//use this to prevent the keyboard from coming up
      }
    }
    
    return super.onTouchEvent(event);
  }

  @Override
  protected void finalize() throws Throwable
  {
    dRight = null;
    rBounds = null;
    super.finalize();
  }
	public void setBtnOk(Button btnOk) {
		this.btnOk = btnOk;
	}
	public Button getBtnOk() {
		return btnOk;
	}
}  



YourActivity.java

//onCreate
        Button  mBtnGO = (Button)findViewById(R.id.btnGO);
        CustomEditText mEditZadani = (CustomEditText)this.findViewById(R.id.editTextZadejSlovo);
        mEditZadani.setBtnOk(mBtnGO);
        mBtnGO.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
             // do stuff for signInButtonClick
          }
        });


//android.okhelp.cz/wiktionary-aplikace-pro-android/

wiktionary

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>


264

Font Family change on View dynamically Android Example | font-family-change-on-view-android-example


TypeFace, setTypeface, font, font family, array of String, Button create dynamically

@Override
protected void onStart() {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
  48,
  55
);	
Typeface typeFace = Typeface.MONOSPACE;	
String [] ar = {"A","B","C","D","E","F","G","H","J","K"};
for (int i = 0; i < ar.length; i++) {
Button buttonView = new Button(this);
buttonView.setText(Html.fromHtml("<b>"+ar[i]+"</b>"));
buttonView.setTextColor(Color.BLUE);
buttonView.setTextSize(27.f);
buttonView.setTypeface(typeFace,Typeface.BOLD);
buttonView.setOnClickListener(mThisButtonListener);
if(i%2==0)
	mLayoutButtons.addView(buttonView, p);
else
	mLayoutButtonsNextRow.addView(buttonView, p);
	
_listOfButtons.add(buttonView);
}

	super.onStart();
}

//////// xml file
<TextView
    android:id="@+id/text01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:fontFamily="Arial"
 />
250

Resize ImageView Image in View Android Example | resize-imageview-image-in-view-android-example


How to resize an image in ImageView Android source code.
Resize imageview layout and image will resize too:

	public void resizeImageView(int width, int height) {
		final ImageView picture1 = (ImageView)findViewById(R.id.imageView1);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
		picture1.setLayoutParams(layoutParams);
	}

369

View getId is null Android | view-getid-is-null-android


If you create a button or view programmatically with OnClickListener you can set a tag key before button in parent layout is added.
And get correct button by this tag getTag() instead getId() in OnClickListener etc.

Button button = new Button(getApplicationContext());
int idOfButton = button.getId(); // return -1

button.setTag("my_button");
String sTag = (String) button.getTag(); // return "my_button"



Editace: 2013-03-02 12:06:08
Počet článků v kategorii: 397
Url:iterate-over-each-entry-in-map-java-android
Share
Share
Tweet