Pages

Sunday 15 April 2012

Button-Android Fundamental-part-1

Button:-
               This is first part of Android fundamental for new Android user.In this part we study about the Button.

Create Button:-
                           To use Button, we need to create button in sample.xml file.we can create button by two way.
1.Using .xml file
2.By java code

1.Sample.xml file:-
                              You need to drag and drop button from form widget. or you manually write code for button in sample.xml file.
Here is code for Button.

<Button 
        android:id="@+id/button1"
        android:layout_width="106dp"
        android:layout_height="90dp"
        android:textSize="15px"
        android:text="Home"
        android:drawableTop="@drawable/faq"
        android:textColor="@color/myWhiteColor"
        android:background="@drawable/examguide_btnmain"
        android:gravity="center" android:layout_marginTop="25dp"></Button>

Property Description:- 
                            1.Android-id:-  You need to set id for button for further use in .java file.we need id of any widget to allocate some work for further coding.   
               Button Home=(Button)findViewById(R.id.button1); 
      Here we assign id to Home button.
                           2.Android:text:- Text which we require to show on Button. we assign "Home" text to Button.
                           3.Android:layout_height/Android:layout_width:-Here we decide height and width of Button.
                           4.android:drawableTop:- We can assign any image to button on respective side. Like top, Bottom, Right, Left.
                           5.android:textColor:- Using this property we can assign color to Button-text.
                           6.android:gravity:- Gravity are use to set gravity for text.you can choose any gravity as per your need.
                           7.android:layout_marginTop:- We can allocate margin in left, right, top and at bottom.
                           8.android:background:-  We use this property to hide button view. using this property we able to show only text and image of button but you need to use appropriate color which match with background.

2. By Programatically:-
                                       Here is sample code for add Button in java program.

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

          //Create instance of button
          Button Home = new Button(this);
          Home .setText("Home");

         //we use linear layout to add button.
        LinearLayout layout = (LinearLayout ) View.inflate(this, R.layout.sample, null);
        //add the button to the view
       layout  .addView(Home );
       //set the layout using the inflated view
        setContentView(layout);
  
   }

3. Use of Button:-  
                            We use button for do some work after click on it.
 we use Onclick() method for that. Here is sample code which jump from one activity to second activity.

Home.setOnClickListener(new View.OnClickListener()
        {
          
            @Override
            public void onClick(View v)
            {

                 Intent intent = new Intent(getApplication(),Activity2.class);
                 startActivity(intent);

                 //Uri uri = Uri.parse("http://www.mpsc.gov.in/advertisements.jsp?head=1&status=1");
                // Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                // startActivity(intent);
 
            }
        });


Commented line are use to jump on specific web page.

4.Image:-
                    Here is image which use all above properties.
 


                  
         

Enjoy..........

SAVE TREE SAVE LIFE................
                           


 

Monday 2 April 2012

Resources NotFoundException

Error:-
         I got following error while Debugging OF my Application.
 
          Uncaught handler: thread main exiting due to uncaught exception
android.content.res.Resources$NotFoundException: String resource ID #0x1


I got this error when i am trying to fetch data from database and try to assign this data to EditText.

I had one integer return type method which return NoOfQuestion  which i want to show in EditText like this..

txtNoQuestion.setText((owq.getNoOfQuestion()));        //Incorrect        txtNoMarks.setText((owq.getMarks()));                        //Incorrect

Then i use following method and it solve my Problem.

txtNoQuestion.setText(Integer.toString(owq.getNoOfQuestion()));   //Correct                        txtNoMarks.setText(Integer.toString(owq.getMarks()));                 //correct                                



Enjoy......

SAVE TREE SAVE LIFE 

Sunday 1 April 2012

Single Time Login Page.

Introduction:-
                     Single time Login means Whenever we release Android Application in Android Market we 
need  allow login process to user only for single time. User enter his information and get login. When user open application second time then application directly jump on Main page instead of login page.
              For this process  we need to store some flag or value into database for further matching. For this, 

Process:-
                We create one static string variable in any activity which user must visit after login (Page which open after login page).
Write this code in onCreate () method of MainActivity.java
  public static final String PREFS_NAME = "MyPrefsFile";

Then we need to create object of SharedPreference in MainActivity.java which can retrieve and modify value.Only one instance of the  SharedPreference object is returned to any callers for the same name.

SharedPreferences settings = getSharedPreferences(MainActivity.PREFS_NAME, 0);

getSharedPreferences():- Retrieve and hold the contents of the preferences file ''MainActivity.PREFS_NAME".

Parameter:-
1. name(MainActivity.PREFS_NAME):- Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).

2. mode(0):- MODE_WORLD_WRITEABLE to control permissions.

Then we need to create instance of  Editor.

 SharedPreferences.Editor editor = settings.edit();

Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()
We set Boolean value to check user enter in this activity or not.
Following code for set value.

        //Set "ifLoggedIn" to true
          editor.putBoolean("hasLoggedIn", true);

          // Commit the edits!
          editor.commit();



Then we need to check this value in onCreate() method of LoginActivity.java whether user logged in or not.

SharedPreferences settings = getSharedPreferences(ExamGuideActivity.PREFS_NAME, 0);
        //Get "hasLoggedIn" value. If the value doesn't exist yet false is returned
        boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);

        if(hasLoggedIn)
        {
             startActivity(new Intent(this, FirstActivity.class ));
        }
        else
        {
             startActivity(new Intent(this, LoginActivity.class ));
        }

Enjoy......

SAVE TREE SAVE LIFE