Open top menu






Exaplain:- How to prevent Activity going to sleep mode Android ?


Stop the activity in pause Mode .



@Overridepublic void onResume() {
    super.onResume();
    getWindow().addFlags(WindowManager.LayoutParams.
               FLAG_KEEP_SCREEN_ON);
}

@Overridepublic void onPause() {
    super.onPause();
    getWindow().clearFlags(WindowManager.LayoutParams.
              FLAG_KEEP_SCREEN_ON);
}





Read more








Example:-Prevent the android device Screen going to sleep (foreground) ?

First you can given Manifest Permission

<uses-permission android:name="android.permission.WAKE_LOCK" />


DeviceStayWake .java


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;

public class DeviceStayWake extends Activity {

    protected PowerManager.WakeLock mWakeLock;

    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag");
        this.mWakeLock.acquire();
    }

    @Override    public void onDestroy() {
        this.mWakeLock.release();
        super.onDestroy();
    }
}






Read more


Explain- How to used Decimal Integer Value in android?

The NumberFormatException occurred when we are  add Decimal value in EditText and Decimal type String Value Convert  in integer.


java.lang.NumberFormatException: Invalid int: "171."

String d=9.90;
int value=new Double(d).intValue();


try....

String amount=171.234;

txt_toatalcost.setText(amount);

amountadult= new Double(txt_toatalcost.getText().toString()).intValue();


//Avoid this...specialy when you used decimal type value.
// amountadult= Integer.parseInt(textTotalAmount.getText().toString());


Thank you for watching... 
Read more


Example:-

Restrict edittext single line, means edittext have a single line.The User Press " enter key" the cursor should not get down second line.




android:maxLines="1"
android:singleLine="true"



<EditText    
android:id="@+id/openorder_searchViewtext"    
android:layout_width="match_parent"    
android:layout_height="48dp"    
android:layout_weight="1"   
android:background="@drawable/edit_text_gradient"    
android:drawableEnd="@drawable/search_icon"   
android:drawablePadding="10dp"    
android:drawableRight="@drawable/search_icon"    
android:ellipsize="end"    
android:hint="@string/Search"    
android:maxLines="1"    
android:paddingLeft="15dp"    
android:scrollHorizontally="true" />
Read more



Example:-Dialog Working With Fragment in Android?





dialog_fragment.xml.
..................................................................................................................................................................
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:id="@+id/edit_name"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:layout_gravity="center"
   
android:orientation="vertical">

    <
TextView
       
android:id="@+id/lbl_your_name"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Enter user name" />

    <
EditText
       
android:id="@+id/username"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:imeOptions="actionDone"
       
android:inputType="text" />

</
LinearLayout>


Activity_main.xml
..................................................................................................................................................................

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.project.example.dialogwithfragment.MainActivity">


    <Button
       
android:text="showCustomFragment"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true"
       
android:layout_marginTop="88dp"
       
android:id="@+id/showCustomFragment" />

    <Button
       
android:text="showAlertDialogFragment"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/showCustomFragment"
       
android:layout_alignRight="@+id/showCustomFragment"
       
android:layout_alignEnd="@+id/showCustomFragment"
       
android:layout_marginTop="43dp"
       
android:id="@+id/showAlertDialogFragment"
       
android:layout_alignLeft="@+id/showCustomFragment"
       
android:layout_alignStart="@+id/showCustomFragment" />
</RelativeLayout>

 ..................................................................................................................................................................

AlertDialog.java
..................................................................................................................................................................
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;



public class AlertDialog_Fragment extends DialogFragment {
    @Override
   
public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                // set dialog icon
               
.setIcon(android.R.drawable.stat_notify_error)
                // set Dialog Title
               
.setTitle("Alert dialog fragment example")
                // Set Dialog Message
               
.setMessage("This is a message")

                // positive button
               
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "Pressed OK", Toast.LENGTH_SHORT).show();
                    }
                })
                // negative button
               
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "Cancel", Toast.LENGTH_SHORT).show();
                    }
                }).create();
    }
}

 ..................................................................................................................................................................

Dialog_Fragment.java
..................................................................................................................................................................


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;



public class Dialog_Fragment  extends DialogFragment {
    @Override
   
public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                // set dialog icon
               
.setIcon(android.R.drawable.stat_notify_error)
                // set Dialog Title
               
.setTitle("Alert dialog fragment example")
                // Set Dialog Message
               
.setMessage("This is a message")

                // positive button
               
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "Pressed OK", Toast.LENGTH_SHORT).show();
                    }
                })
                // negative button
               
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(), "Cancel", Toast.LENGTH_SHORT).show();
                    }
                }).create();
    }
}

 ..................................................................................................................................................................


My_DialogFragment .java
..................................................................................................................................................................


import android.app.DialogFragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;



public class My_DialogFragment extends DialogFragment implements TextView.OnEditorActionListener {

    private EditText mEditText;




    public interface UserNameListener {
        void onFinishUserDialog(String user);
    }

    // Empty constructor required for DialogFragment
   
public My_DialogFragment() {

    }

    @Override
   
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment, container);
        mEditText = (EditText) view.findViewById(R.id.username);

        // set this instance as callback for editor action
       
mEditText.setOnEditorActionListener(this);
        mEditText.requestFocus();
        getDialog().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        getDialog().setTitle("Please enter username");

        return view;
    }

    @Override
   
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // Return input text to activity
       
UserNameListener activity = (UserNameListener) getActivity();
        activity.onFinishUserDialog(mEditText.getText().toString());
        this.dismiss();
        return true;
    }
}
 ..................................................................................................................................................................


MainActivity .java
..................................................................................................................................................................



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener, My_DialogFragment.UserNameListener {

    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn= (Button)findViewById(R.id.showCustomFragment);
        btn.setOnClickListener(this);
        Button btn1= (Button)findViewById(R.id.showAlertDialogFragment);
        btn1.setOnClickListener(this);
    }

    @Override
   
public void onFinishUserDialog(String user) {
        Toast.makeText(this, "Hi, " + user, Toast.LENGTH_SHORT).show();     }

    public void onClick(View view) {
        // close existing dialog fragments
       
FragmentManager manager = getFragmentManager();
        Fragment frag = manager.findFragmentByTag("fragment_edit_name");
        if (frag != null) {
            manager.beginTransaction().remove(frag).commit();
        }
        switch (view.getId()) {
            case R.id.showCustomFragment:
                My_DialogFragment editNameDialog = new My_DialogFragment();
                editNameDialog.show(manager, "fragment_edit_name");
                break;
            case R.id.showAlertDialogFragment:
                AlertDialog_Fragment alertDialogFragment = new AlertDialog_Fragment();
                alertDialogFragment.show(manager, "fragment_edit_name");
                break;
        }
    }
}





Read more