Tuesday, 28 April 2015

Set Text Color for textView Android

You can use
  textView1.setTextColor(getResources().getColor(R.color.mycolor))
or
  textview1.setBackgroundColor(Color.parseColor("#ffffff"));
or
 textview1.setBackgroundColor(Color.RED);
or
 textView1.setBackgroundColor(R.color.black);

Sunday, 26 April 2015

Getting Started with Android Studio

mipmap Folders in android studio


It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density.




What is the short cut to Auto import all in Android Studio?

For Windows/Linux, you can go to File -> Settings -> Editor -> Auto Import -> Java and make the following changes:
  • change Insert imports on paste value to All
  • markAdd unambigious imports on the fly option as checked


Android Studio short cuts 

Go to class CTRL + N
Go to file CTRL + Shift + N
Navigate open tabs ALT + Left-Arrow; ALT + Right-Arrow
Look up recent files CTRL + E
Go to line CTRL + G
Navigate to last edit location CTRL + SHIFT + BACKSPACE
Go to declaration CTRL + B
Go to implementation CTRL + ALT + B
Go to source F4
Go to super Class CTRL + U
Show Call hierarchy CTRL + ALT + H
Search in path/project CTRL + SHIFT + F
Programming Shortcuts:-
Reformat code CTRL + ALT + L
Optimize imports CTRL + ALT + O
Code Completion CTRL + SPACE
Issue quick fix ALT + ENTER
Surround code block CTRL + ALT + T
Rename and Refractor Shift + F6
Line Comment or Uncomment CTRL + /
Block Comment or Uncomment CTRL + SHIFT + /
Go to previous/next method ALT + UP/DOWN
Show parameters for method CTRL + P
Quick documentation lookup CTRL + Q
Delete a line CTRL + Y
View declaration in layout CTRL + B


Thursday, 23 April 2015

How to remove red exclamation mark on eclipse project


  1. Right click on the "Project name"
  2. Select "Build path"
  3. Then select "Configure Build Path"
  4. Click on "Libraries"
  5. Remove all the libraries which were referring to old path
  Then, the exclamation symbol on the "Project name" was removed.

Tuesday, 14 April 2015

How to use Custom Toast in Android

Custom Toast

// *********** Creating Custom Toast ***********

            public void CustomToast(String msg) {
                       
                        ViewGroup parent = (ViewGroup) findViewById(R.id.container);
                       
                        LayoutInflater inflater = getLayoutInflater();
                       
                        View customToastroot = inflater.inflate(R.layout.toast, parent , false);

                        Toast customtoast = new Toast(BookNow.this);
                       
                        TextView tv_tost = (TextView)customToastroot.findViewById(R.id.tv_tost);
                       
                        tv_tost.setText(msg);
                        customtoast.setView(customToastroot);
                   customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
                        customtoast.setDuration(Toast.LENGTH_LONG);

                        customtoast.show();
            }

you just have to call the method CustomToast() where ever you need .

Saturday, 11 April 2015

Storing Values in Android


Intent


How to use putExtra() and getExtra()


Use this to "put" the file...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("name", "value");

Then, to retrieve the value try ...

Bundle b = getIntent().getExtras();
if(b!=null){
String j = b.getString("name");
}

Example :-

first Screen.java
text=(TextView)findViewById(R.id.tv1);
edit=(EditText)findViewById(R.id.edit);
button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        String s=edit.getText().toString();
        Intent i=new Intent(MainActivity.this, newclass.class);
        i.putExtra("name", s);
        startActivity(i);
    }
});

Second Screen.java
public class newclass extends Activity
{
    private TextView Textv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.intent);
        Textv = (TextView)findViewById(R.id.tv2);
        Bundle b = getIntent().getExtras();
        if(b!=null)
        {
            String j =(String) b.get("name");
        }
    }

}


Shared Preferences


Use this to "put" the file...

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor editor = prefs.edit();
editor.putString(PREF_NAME, "");
editor.commit();

Then, to retrieve the value try ...

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(YouActivity.this);

String servername = settings.getString("sharedPreferencesKey", "defaultValue");
server.setText(servername);

Friday, 10 April 2015

Problems you might face with in Eclipse

Eclipse won't show files in package explorer

You just need to click 'File' -> 'Import' -> 'General' -> 'Existing Projects into Workspace' and select the missing projects in the window that appeared.

Thursday, 9 April 2015

On default Android Back Button Customiztion

 How To Override the “Back” button so it doesn't Finish() my  Activity?



public void onBackPressed() {
   Intent setIntent = new Intent(Intent.ACTION_MAIN);
   setIntent.addCategory(Intent.CATEGORY_HOME);
   setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(setIntent);
}

Android Back Button to specific activity

public void onBackPressed() {

  super.onBackPressed();

  startActivity(new Intent(ThisActivity.this, NextActivity.class)); 

  finish();

  }

How to disable back button in android

@Override
public void onBackPressed() {
    // do nothing.
}

 

 

 

“A configuration with this name already exists” error in eclipse run configurations

To Remove Run Configuration Android Application Name NOT IN USE

GOTO :-

${WORKSPACE}/.metadata/.plugins/org.eclipse.debug.core/.launches
And restart Eclipse