Google+

Tuesday, May 17, 2011

Hint Not Displaying When changing Alignment in EditText

This issue is due to the choice of adding gravity to center the text. There is a known bug in the SDK that, as of 2.3, it looks like is still not fixed. This bug hides the hint text when android:singleLine and any custom value of android:gravity are set together. Your choice at this juncture really is to remove one or the other. Some workaround options:
  1. Remove the android:gravity declaration and have the text left-aligned.
  2. Keep the declaration and remove android:singleLine. In this case, you will probably have to override the Editor Action in order to get the default Next or Done button on the soft keypad.
  3. replace parameter android:singleLine to android:maxLine
  4. Remove android:gravity, and set layout_width="wrap_content". Then appropriately center the entire widget in the parent layout. This may look a little goofy with the standard EditText background as it will grow and shrink. You may have to set this to null and make the background part of a parent container.
Also, don't forget to star the bug :)
and leave the comments







Source : stackoverflow.com

Tuesday, May 10, 2011

Hide Tabbar on Soft Keyboard Automatically open in Android

Add the following attribute in menifest file while declaring the activity

<activity android:name=".ActivityName" android:windowSoftInputMode="adjustPan">


This is better idea to use attribute in menifest file rather using setOnFocusChangeListener() for every Edit Text



Don't Forget to leave Comments



Thursday, May 5, 2011

Passing Data & Returning Data between Activity in Android

Parent Activity where we Start to send & get Result

Steps : 1
  • From the ParentActivity we are calling intents
    Intnet inent = new Intent(this, CallingActivity.class);
  • Passing Values using "ComingFrom" as a Key Value with "Parent Activity" as Value
  • Use startActivityForResult(intnet, requestCode) Method by passing Intent Object & any RequestCode. //

// ParentActivity.java

public class ParentActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent_layout);

        Button btn = (Button) findViewById(R.id.getResultBtn);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ParentActivity.this,
                        CallingActivity.class);
                int requestCode = 1;
                intent.putExtra("ComingFrom", "This is Parent Activity Data");
                startActivityForResult(intent, requestCode);
            }
        });
    }

    // find the Step 4 Below
}





Steps : 2          CallingActivity.java (getting values of Parent Activity)
  • This activity called by ParentActivity.java
  • Create an Intent Object and assign with getIntent() method which return Intent Object of Called Activity.
    Intnet inent = getIntent();

  • Use getExtras() method get values sent by ParentActivity
    getExtras() = retrun the bundle of all extras previously added with putExtra(), or null if none have been added.
  • then call the method of getString(KeyValue) method with KeyValue which created by ParentActivity.
    This method will return the value of Key.
  • Display Value wherever you want.

Steps : 3          CallingActivity.java (sending values to Parent Activity)
  • on the Button Click we have to pass the value of EditText to parent Activity
  • on Click Event we Create the new Intent Object
    Intnet i = new Intent();
  • same step to sending value
    i.putExtras(KeyValue, Value)
  • Now as we need to send this data to called Activity we use setResult() method
    setResult(RESULT_OK, IntentObject);
    RESULT_OK = Standard activity result: operation succeeded.
  • finish() the Calling Activity





// CallingActivity.java


public class CallingActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child_layout);

        TextView resultDataTv = (TextView) findViewById(R.id.resultData);

        Intent intent = getIntent();
        String str = intent.getExtras().getString("ComingFrom");
        resultDataTv.setText(str);

        Button sendResultBtn = (Button) findViewById(R.id.sendResultBtn);
        sendResultBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                EditText et = (EditText) findViewById(R.id.dataTxt);
                String str = et.getText().toString();

                Intent i = new Intent();
                i.putExtra("childResult", str);
                setResult(RESULT_OK, i);
                finish();
            }
        });
    }
}


Steps : 4          ParentActivity.java (getting Result from Calling Activity)
  • Here we have to Catch the result of Calling Activity
  • First Overwrite the method onActivityResult(int requestCode, int resultCode, Intent data) in ParentActivity.java
  • Check for the resultCode & requestCode (You Can use Switch as Well)
  • get the result using data.getStringExtra(KeyValue) - Retrieve extended data from the intent.
  • Set resulted text wherever you want.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode==1) {

            String str = data.getStringExtra("ComingFrom");
            TextView tv = (TextView) findViewById(R.id.resultDataFrmChild);
            tv.setText(str);
            Toast.makeText(this, str, Toast.LENGTH_SHORT)
                    .show();
        }
    }

Google+