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 Button and TextView dynamically - Android sample


AD MOB


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
            TextView textView = new TextView(this);
            textView.setText("Text View ");
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            layout.addView(textView, p);

            Button buttonView = new Button(this);
            buttonView.setText("Button");
            buttonView.setOnClickListener(mThisButtonListener);
            layout.addView(buttonView, p);
        
    }
    private OnClickListener mThisButtonListener = new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Hello !",
                    Toast.LENGTH_LONG).show();
        }
    };
}

397LW NO topic_id




AD

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


53

Timer stopwatch based on Java Calendar class Android example | timer-stopwatch-java-calendar-android-example


Start Stop Stopwatch Timer Android example source code.


private static long mStartTime = 0L;
Calendar cal;
TextView hTextViewVypis = (TextView)findViewById(R.id.idTextVypis);

void start(){
            cal = Calendar.getInstance();
            mStartTime = cal.getTimeInMillis();
}

void stop(){
     prinOutStopWatchTime();
}


    private void prinOutStopWatchTime() {
    	final long start = mStartTime;
    	
    	cal = Calendar.getInstance();
    	long stopTime = cal.getTimeInMillis();
    	long millis = stopTime - start;
    	long milisekundy = millis % 1000;
    	int seconds = (int) (millis / 1000);
    	int minutes = seconds / 60;
    	seconds     = seconds % 60;
    	int hour = minutes / 60;
    	hour = hour % 60;
    	
    	if (seconds < 10) {
    		hTextViewVypis.setText(hour +  ":" + minutes + ":0" + seconds + ":" + milisekundy);
    	} else {
    		hTextViewVypis.setText(hour  +  ":" + minutes + ":" + seconds + ":" + milisekundy);            
    	}
    	
    }




382

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());
 }

}



154

Create own custom Title Bar TitleBar with a Icon Android example | create-own-custom-title-titlebar-android-example


onCreate in MainActivity.java

    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main.xml);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}


You can add to titlebar different Views by xml file.
custom_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Some text" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
       
        android:src="@drawable/ic_launcher" />
    
</RelativeLayout>





custom-title-titlebar-android
7

Show keyboard in WebView Android example | webview-keyboard


Show keyboard if WebView input have focus Android apps development example source code.

WebView webview;

//in onCreate

	webview = (WebView) findViewById(R.id.idWebviewPda);
	webview.getSettings().setJavaScriptEnabled(true);
	webview.loadUrl("//android.okhelp.cz");
	
	webview.requestFocus(View.FOCUS_DOWN);
    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                    break;
            }
            return false;
        }
    });

385

EditText.setOnKeyListener not working on maxLines EditText | edittext-setonkeylistener-not-working-on-maxlines-edittext


Check this in XML file if use singleLine
singleLine change to maxLines (1 if singleLine)
declare inputType


        android:maxLines="1"
        android:inputType="text"



Editace: 2011-10-22 07:54:35
Počet článků v kategorii: 397
Url:create-button-dynamically-android-sample

AD
11 Share
Share
Tweet