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.

How to programmatically set the button style - Android



How set rounded corners and own styles ActivityMy.java

Button btn.setBackgroundResource(R.drawable.back_button_answer);



\res\drawable\back_button_answer.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="//schemas.android.com/apk/res/android" android:shape="rectangle" >
   <corners android:radius="10dip" />
   <!-- background -->
   
   <gradient
                android:startColor="#D6D7D6"
                android:centerColor="#E2E2E2"
                android:centerY="0.75"
                android:endColor="#D6D7D6"
                android:angle="270"
        />
   
   <stroke android:width="2dip" android:color="#fff"/>
</shape>

397LW NO topic_id



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


146

Rotate a image bitmap picture Android example | rotate-a-image-bitmap-android-example


Rotate a bitmap Android source code.

public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}


	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
			canvas.drawColor(Color.YELLOW);
            
          // Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
			
		    //  you need to insert a image flower_blue into res/drawable folder 
			Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
			 Matrix mat = new Matrix();
			                mat.postRotate(90);
			                Bitmap bmpRotate = Bitmap.createBitmap(bmp, 0, 0,
			  bmp.getWidth(), bmp.getHeight(),
			  mat, true);
		   int h = bmp.getHeight();
           
           canvas.drawBitmap(bmp, 10,10, paint);
           canvas.drawBitmap(bmpRotate, 10,10 + h + 10, paint);
           
		}

	}
}




rotate bitmap android
384

Convert Activity to Fragment Step by Step | convert-activity-to-fragment-step-by-step


public class Main extends Activity {

private TextView mTextView;
private Activity mAct;
private Intent mIntent;

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main_layout);
  mTextView = findViewById(R.id.mTextView);
  mAct = getActivity();
  mIntent = getIntent();
 }

}


to:
public class Main extends Fragment{

 private TextView mTextView;
 private FragmentActivity mFrgAct;
 private Intent mIntent;
 private LinearLayout mLinearLayout;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View root = inflater.inflate(R.layout.fragment_main, null);
        return root;
    }

 public void onViewCreated(View view, Bundle savedInstanceState) {
          // you can add listener of elements here
          /*Button mButton = (Button) view.findViewById(R.id.button);
            mButton.setOnClickListener(this); */
          mTextView = view.findViewById(R.id.mTextView);
          mLinearLayout = (LinearLayout)view;
 }
 public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
          mFrgAct = getActivity();
          mIntent = mFrgAct.getIntent(); //  Intent intent = new Intent(getActivity().getIntent());
 }

}



254

How get bitmap dimension without loading Android example | how-get-bitmap-dimension-without-loading-android-example


Bitmap Width, Height without memory allocation:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
// now opts.outWidth and opts.outHeight are the dimension of the
// bitmap, even though Bitmap is null
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
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

82

Switch Statement Java basic example | switch-statement-java-basic-example


Switch statement with numbers and array of strings Java example.


public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = { "One", "Two", "Three", "Four" };

		int i = 2;
		switch (i) {
			case 1: {
				System.out.println(arrayOfString[i]);
				break;
			}
			case 2: {
				System.out.println(arrayOfString[i]);
				break;
			}
			case 3: {
				System.out.println(arrayOfString[i]);
				break;
			}
			default: {
				System.out.println("Enter a valid value.");
			}
		} // END of switch
	}
}
/*
 * Three
 */




Possible:

case 1:
 System.out.println(arrayOfString[i]);
break;

// i love this notation 
case 1:{
 System.out.println(arrayOfString[i]);
}break;

case 1:{
 System.out.println(arrayOfString[i]);
 break;
}



Editace Jana Žákovská : 2012-07-25 20:54:27
Počet článků v kategorii: 397
Url:how-to-programmatically-set-the-button-style-android
Share
Share
Tweet