Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Saturday, 20 October 2018

Make back button working in action bar in Android.

Recently I was trying to use working back button on my one of the project, but whatever I was done previously was not working. It was a crash every time with error message -


I am newbie to Android, but I was done back activity few day back but after updating my Android Studios and APIs nothing was working. I was also uses  inflated menu in my project, and debugger always stuck in onOptionsItemSelected(MenuItem item) method which I was used for option menu. I also tried many unsuccessful lines of code. Finally I got this solution for my problem -

1 .  In  onCreate(Bundle savedInstanceState) method use -

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

2 .  Override methos in your activity class

@Overridepublic boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}


3 . In onOptionsItemSelected(MenuItem item) method use -

switch (item.getItemId()) {  
       case android.R.id.home:
        Intent i = new Intent(this, Your_Parent_Activity.class);
        intent.addFlags(i.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return true;
}


Friday, 7 September 2018

How to customize/decorate a button in Android Studio?

How can we customize button in Android Studio?. It can be a background color, its shape, its fonts, image, etc. It can be accomplished by using the background property of the button that should be drawable.

Let see how we can do it by example-

First, create a button on an activity.

<Button    android:id="@+id/button"    android:layout_width="wrap_content"  
  android:layout_height="wrap_content"    android:text="Button" 
   tools:layout_editor_absoluteX="45dp"    tools:layout_editor_absoluteY="31dp" />


Now create a drawable resource file. To create this expand your app in project directory structure. Right click on res directory, then select New ----> Android resource file. Now put the file name (
I have used button1.xml) then Ok.



Now in this resource file (button1.xml), write the code for button customization.
See below the code-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
 <!--shape of button [You can use here rectangle, oval, line, Ring] -->
<solid android:color="#eeffeeee" /> <!--color of button -->
<corners android:bottomRightRadius="38dp" 
   android:bottomLeftRadius="38dp"  
  android:topRightRadius="38dp"   
 android:topLeftRadius="38dp"/> <!--corners of button -->
</shape>

Now in button, add the background property like -

android:background="@drawable/button1" 

button1 is drawable resource file name that you have created in res ---> drawable folder.

Some time we require image as well as test on button then -

Copy your image in res ---> drawable folder. Make sure that the file name should only contain characters and digits and file name should start only from small letter alphabets.

Now use another property in your button -

android:drawableLeft="@drawable/icon"

Where icon is image file name, which you have copied in res --> drawable.

Now See the complete activity code and structure.





Here is the complete reference of resource file (copied from
https://developer.android.com/guide/topics/resources/drawable-resource#StateList:-

 <?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="float"
android:centerY="float"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
 </shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>





Tuesday, 20 February 2018

Android Database Connectivity with Firebase

Firebase is a mobile platform that helps you quickly develop high-quality apps. Firebase is made up of complementary features that you can mix-and-match to fit your needs including database, with Google Analytics for Firebase at the core. 

In this post, I will demonstrate that how to connect you android app to Firebase Database. I also assume that you have enough working knowledge of Android and Android Studio.

Prerequisites-

  • Latest Android Studio
  • Android Services plugins should be installed in IDE
  • Gmail account
Start a project in Android Studio. (I have used 3.0.1) .

Now Go to Firebase Console and log in using your gmail id and password and click on Add Project.


Then give a name of your package and your country here and then Continue.

After clicking on Continue, You will be on Firebase Console. Then select here "Add Firesbase to your Android app" in the centre of page.


A window (Add Firebase to your project) will appear. Here is 3 step process.

1. Register App -

Input package name (e.g com.example.arvindsbi.sbischooldevices;) whatever you have created in Android Studio and then click on Register App.



2. Download config file -

Here you will have to download google-services.jason file on your system and click Continue. After downloading, paste this file into your app root directory. (Hero you will find instructions to use it)


3. Add Firebase SDK -

After downloading and configuring jason file, you will have make some changes in your gradle file. You will have to add some dependencies in your app level and project level build.gradle file (as instructed by Firebase).



 

Now in app level build.gradle file, add the following line in dependencies -
compile com.google.firebase:firebase-core:11.8.0'
Sometimes this line also required
compile 'com.firebase:firebase-client-android:2.5.0' 


Now in project level build.gradle file, add the foolowing dependencies -
classpath 'com.google.gms:google-services:3.2.0'
After updating your both gradle file click on sync now.
After doing all these things your app is now prepared for connectivity with Firebase database.
Now you require Java code and xml code to make connectivity.
Important- It should be assured that Android Support should be installed in Android Studio.
To check or install Android Support Go to 

Tools ===>> Android ===>> SDK Manager   then select Plugins in left pane and then tick Android 
Support in center pane if not cheked


Here is the full code of both files -
Xml code -
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.arvindsbi.sbischooldevices.FirebaseDemo"> <EditText android:id="@+id/myname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginEnd="72dp" android:layout_marginRight="72dp" android:layout_marginTop="64dp" android:ems="10" android:inputType="textPersonName" android:hint="Name" tools:layout_editor_absoluteX="84dp" tools:layout_editor_absoluteY="44dp" /> <EditText android:id="@+id/myage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/myname" android:layout_alignStart="@+id/myname" android:layout_below="@+id/myname" android:layout_marginTop="43dp" android:ems="10" android:hint="Age" android:inputType="textPersonName" /> <Button android:id="@+id/button13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/myage" android:layout_alignStart="@+id/myage" android:layout_below="@+id/myage" android:layout_marginLeft="36dp" android:layout_marginStart="36dp" android:layout_marginTop="58dp" android:text="Add to Firebase" /> </RelativeLayout>
In Java file you required -
1. import com.firebase.client.Firebase;
2. Then declare Firbase class
public Firebase fb;
3. In onCreate method initialise the Firebase and connect using URL
Firebase.setAndroidContext(this);
fb=new Firebase("https://sbxxxxx-fxxxx.firebaseio.com/");
4. In onClick method
Firebase nameChild=fb.child("Name");
nameChild.setValue(myname.getText()+"");
Firebase ageChild=fb.child("Age");
ageChild.setValue(myage.getText()+"");
Here is full Java code -
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.firebase.client.Firebase;

public class FirebaseDemo extends AppCompatActivity {
public EditText myname,myage;
private Button mybutton;
public Firebase fb;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firebase_demo);

        Firebase.setAndroidContext(this);

        fb=new Firebase("https://sbixxxxxl-fxxxx.firebaseio.com/");

        mybutton=(Button)findViewById(R.id.button13);
        myname=(EditText) findViewById(R.id.myname);
        myage=(EditText) findViewById(R.id.myage);
        mybutton.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                Firebase nameChild=fb.child("Name");
                nameChild.setValue(myname.getText()+"");
                Firebase ageChild=fb.child("Age");
                ageChild.setValue(myage.getText()+"");
                Toast.makeText(getBaseContext(),"Details Added",
                Toast.LENGTH_SHORT).show();
            }
        });
    }
}
After doing all these things don't forget to give internet permission to AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET"/>



Now run your app enter data -



Go to  Firebase database and see your entered data -

(Note:- Sometime you can not see your data in Google Chrome, means it will show blank, if so then change your browser to Mozilla Firefox or Internet Explorer and loging to see your data. I don't why it is so? But I have faced this and resolved like this. )


Quick Look up -

  1. Firebase account login
  2. Add project
  3. Add your project package
  4. Download config file
  5. Paste this file in your app root directory
  6. Change in both app level and project level build.gradle file
  7. Check android support plugins
  8. Write code in your project
  9. Give internet permission to your project
  10. Run project
  11. See the data in Firebase


Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...