package com.example.checkedit;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String PREFS_NAME = "CheckBoxState";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customAlertdialog();
}
public void customAlertdialog(){
//Inflate layout
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.yourview, (ViewGroup) findViewById(R.id.yourview_root));
// set data in textview
TextView text1 = (TextView)dialoglayout.findViewById(R.id.text1);
final CheckBox che = (CheckBox)dialoglayout.findViewById(R.id.checkBox1);
text1.setText("Terms and Conditions");
text1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "textview msg", Toast.LENGTH_SHORT).show();
}
});
//display alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout).setTitle("Your title")
.setCancelable(false)
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences settings = getSharedPreferences(
PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("skipMessage", String.valueOf(che.isChecked()));
// Commit the edits!
editor.commit();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String skipMessage = settings.getString("skipMessage", "NOT checked");
System.out.println("ischeck is "+che.isChecked());
if (!skipMessage.equals("true")) {
alert.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String PREFS_NAME = "CheckBoxState";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customAlertdialog();
}
public void customAlertdialog(){
//Inflate layout
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.yourview, (ViewGroup) findViewById(R.id.yourview_root));
// set data in textview
TextView text1 = (TextView)dialoglayout.findViewById(R.id.text1);
final CheckBox che = (CheckBox)dialoglayout.findViewById(R.id.checkBox1);
text1.setText("Terms and Conditions");
text1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "textview msg", Toast.LENGTH_SHORT).show();
}
});
//display alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout).setTitle("Your title")
.setCancelable(false)
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SharedPreferences settings = getSharedPreferences(
PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("skipMessage", String.valueOf(che.isChecked()));
// Commit the edits!
editor.commit();
dialog.cancel();
}
});
AlertDialog alert = builder.create();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String skipMessage = settings.getString("skipMessage", "NOT checked");
System.out.println("ischeck is "+che.isChecked());
if (!skipMessage.equals("true")) {
alert.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
yourview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/yourview_root" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="Do not show again" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="TextView1" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text1"
android:layout_marginTop="20dp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/yourview_root" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="Do not show again" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="TextView1" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text1"
android:layout_marginTop="20dp" />
</RelativeLayout>
No comments:
Post a Comment