I am trying to generate a simple popup on click of a button, but due to some reason it is not working. Below is my code:
public class Product extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.product);
String fontPath = "fonts/georgia.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
Button Particle =(Button)findViewById(R.id.button1);
Button MDF=(Button)findViewById(R.id.button2);
Button Laminates=(Button)findViewById(R.id.button3);
Button Ply =(Button)findViewById(R.id.button4);
Button Floor=(Button)findViewById(R.id.button5);
Button Door=(Button)findViewById(R.id.button6);
Button EdgeBand=(Button)findViewById(R.id.button7);
Button ModFur=(Button)findViewById(R.id.button8);
Particle.setTypeface(tf);
MDF.setTypeface(tf);
Laminates.setTypeface(tf);
Ply.setTypeface(tf);
Floor.setTypeface(tf);
Door.setTypeface(tf);
EdgeBand.setTypeface(tf);
ModFur.setTypeface(tf);
/* Particle.startAnimation(animScale);
MDF.startAnimation(animScale);
Laminates.startAnimation(animScale);
Ply.startAnimation(animScale);
Floor.startAnimation(animScale);
Door.startAnimation(animScale);
EdgeBand.startAnimation(animScale);
ModFur.startAnimation(animScale); */
Particle.setOnClickListener(Par);
}
private View.OnClickListener Par = new View.OnClickListener(){
public void onClick(View v){
openNewDialog();
}
};
private void openNewDialog() {
new AlertDialog.Builder(this).setItems(R.array.pop,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface,int i){
if(i==0){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
if(i==1){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
}});
}
In the Strings.xml file:
<array name="pop">
<item name="easy_label">Key Features</item>
<item name="medium_label">Advantages</item>
</array>
The layout file for the said activity in xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/universalbg"
android:orientation="vertical" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="#string/btn2"
android:textColor="#FFFFFF"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
<ScrollView
android:layout_marginTop="10dip"
android:layout_marginBottom="03dip"
android:id="#+id/Scroll"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn2"
android:textColor="#900606" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn3"
android:textColor="#900606" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button3"
android:layout_below="#+id/button3"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn4"
android:textColor="#900606" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_below="#+id/button4"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn5"
android:textColor="#900606" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button5"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn6"
android:textColor="#900606" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button6"
android:layout_below="#+id/button6"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn7"
android:textColor="#900606" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button7"
android:layout_below="#+id/button7"
android:layout_marginTop="20dp"
android:background="#drawable/insidebut"
android:text="#string/BTn8"
android:textColor="#900606" />
</ScrollView>
You need to call show() in order to actually display the dialog.
private void openNewDialog() {
new AlertDialog.Builder(this).setItems(R.array.pop,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface,int i){
if(i==0){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
if(i==1){
Intent about = new Intent(Product.this,AboutUs.class);
startActivity(about);
}
}}).show(); // Here
}
Related
I am trying to make custom dialog in which i want to make selection of textview and image visible if user is going to selection .
in below code i have given three text views and four image view, want that if user is going to select text view or image view it should be text color change and on image it should be tick mark should be appear.
like this attached image .
attached image
As per attached image i want to make selection if user select image it should appear as a tick mark and if user selects any text views then tick should below of the text views and color also changed.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.cust);
dialog.setTitle("Your title");
dialog.setCancelable(false);
dialog.show();
TextView d_btn_ok = (TextView) dialog.findViewById(R.id.btnOk);
TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel);
d_btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
d_btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
TextView tvMsg11 =(TextView) dialog.findViewById(R.id.tvMsg11);
TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
image1.setImageDrawable(getResources().getDrawable(R.drawable.one));
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
image2.setImageDrawable(getResources().getDrawable(R.drawable.two));
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
image3.setImageDrawable(getResources().getDrawable(R.drawable.three));
ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);
image4.setImageDrawable(getResources().getDrawable(R.drawable.four));
}
}
cust.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="430dp"
android:orientation="vertical"
android:background="#ffffff"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textColor="#293448"
android:text="Update your water intake" />
<TextView
android:id="#+id/tvMsg1"
android:layout_below="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="21.5dp"
android:textSize="16sp"
android:textColor="#293448"
android:text="How many glasses of water do you
drink daily?" />
<LinearLayout
android:id="#+id/linearImage21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvMsg1"
android:weightSum="3">
<TextView
android:id="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="23.5dp"
android:layout_height="wrap_content"
android:text="4 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg111"
android:layout_toRightOf="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="6 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg1111"
android:layout_toRightOf="#+id/tvMsg111"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="8 Glasses"
android:textSize="14sp"/>
</LinearLayout>
<TextView
android:id="#+id/container"
android:layout_below="#+id/linearImage21"
android:layout_marginTop="48dp"
android:layout_marginLeft="21.5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Your container size?" />
<LinearLayout
android:id="#+id/linearImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/container"
android:weightSum="4">
<ImageView
android:id="#+id/image1"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/custom_button_background"
android:checked="true"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- <LinearLayout
android:id="#+id/linearImag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/linearImage1"
android:weightSum="2">
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearImage1"
android:orientation="horizontal"
android:layout_marginTop="28dp"
android:weightSum="2"
>
<TextView
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_marginLeft="86.5dp"
android:textSize="16sp"
android:text="CANCEL"
android:textAllCaps="true"
android:textColor="#293448" />
<TextView
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="48.5dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_marginRight="6dp"
android:background="#fff"
android:text="SUBMIT"
android:textAllCaps="true"
android:textColor="#21c8d7" />
</LinearLayout>
</RelativeLayout>
After apply above code
I am getting like this image
this image
We can do it easily as per attached image
Made visible when need of Highlighter under text views or adjacent of image.
or Made invisible when not need or pressed That's it .
Wrote code for same which is working .
text color change on the basis of text selection :
text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#00b200"></item>
<item android:state_focused="true" android:color="#00b200"/>
<item android:state_selected="true" android:color="#000000"/>
<item android:color="#d3d3d3"></item>
</selector>
For Dialog :-
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.cust);
dialog.setTitle("Your title");
dialog.setCancelable(false);
dialog.show();
TextView d_btn_ok = (TextView) dialog.findViewById(R.id.btnOk);
TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel);
d_btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
d_btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
final TextView tvMsg1111=(TextView) dialog.findViewById(R.id.tvMsg1111);
final TextView tvMsg11 = (TextView) dialog.findViewById(R.id.tvMsg11);
ImageView tick5=(ImageView) dialog.findViewById(R.id.tick5);
ImageView tick6=(ImageView) dialog.findViewById(R.id.tick6);
ImageView tick7=(ImageView) dialog.findViewById(R.id.tick7);
tvMsg11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
//tvMsg11.setImageDrawable(getResources().getDrawable(R.drawable.tick));
tvMsg111.setSelected(false);
tvMsg1111.setSelected(false);
tvMsg11.setSelected(true);
tick5.setVisibility(View.VISIBLE);
tick6.setVisibility(View.INVISIBLE);
tick7.setVisibility(View.INVISIBLE);
}
});
// final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
tvMsg111.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvMsg11.setSelected(false);
tvMsg1111.setSelected(false);
tvMsg111.setSelected(true);
tick6.setVisibility(View.VISIBLE);
tick7.setVisibility(View.INVISIBLE);
tick5.setVisibility(View.INVISIBLE);
}
});
//final TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);
tvMsg1111.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvMsg11.setSelected(false);
tvMsg111.setSelected(false);
tvMsg1111.setSelected(true);
tick7.setVisibility(View.VISIBLE);
tick6.setVisibility(View.INVISIBLE);
tick5.setVisibility(View.INVISIBLE);
}
});
/*TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);*/
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
ImageView tick1=(ImageView)dialog.findViewById(R.id.tick1);
ImageView tick2=(ImageView)dialog.findViewById(R.id.tick2);
ImageView tick3=(ImageView)dialog.findViewById(R.id.tick3);
ImageView tick4=(ImageView)dialog.findViewById(R.id.tick4);
image1.setImageDrawable(getResources().getDrawable(R.drawable.one));
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick1.setVisibility(View.VISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
// ImageView image21 = (ImageView) dialog.findViewById(R.drawable.four);
// for replacing image on click of image
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick2.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image2.setImageDrawable(getResources().getDrawable(R.drawable.two));
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick3.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image3.setImageDrawable(getResources().getDrawable(R.drawable.three));
ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);
image4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick4.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image4.setImageDrawable(getResources().getDrawable(R.drawable.four));
cust.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="430dp"
android:orientation="vertical"
android:background="#ffffff"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textColor="#293448"
android:text="Update your water intake" />
<TextView
android:id="#+id/tvMsg1"
android:layout_below="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="21.5dp"
android:textSize="16sp"
android:textColor="#293448"
android:text="How many glasses of water do you
drink daily?" />
<LinearLayout
android:id="#+id/linearImage21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvMsg1"
android:weightSum="3">
<TextView
android:id="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="23.5dp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"
android:text="4 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg111"
android:layout_toRightOf="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#drawable/text_color"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
android:text="6 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg1111"
android:layout_toRightOf="#+id/tvMsg111"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"
android:text="8 Glasses"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/lineartick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="2dp"
android:layout_below="#+id/linearImage21"
android:weightSum="3">
<ImageView
android:id="#+id/tick5"
android:layout_width="16dp"
android:layout_weight="1"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_marginRight="12dp"
android:layout_height="10dp" />
<ImageView
android:id="#+id/tick6"
android:layout_width="16dp"
android:layout_weight="0.7"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_marginLeft="1dp"
android:layout_toRightOf="#+id/image1"
android:layout_height="10dp" />
<ImageView
android:id="#+id/tick7"
android:layout_width="16dp"
android:layout_weight="1.3"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_height="10dp" />
</LinearLayout>
<TextView
android:id="#+id/container"
android:layout_below="#+id/linearImage21"
android:layout_marginTop="48dp"
android:layout_marginLeft="21.5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#293448"
android:alpha="80"
android:text="Your container size?" />
<LinearLayout
android:id="#+id/linearImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/container"
android:weightSum="4">
<ImageView
android:id="#+id/image1"
android:layout_width="0dp"
android:layout_weight="1"
android:checked="true"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick1"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image1"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick2"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image2"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick3"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image3"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick4"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_marginRight="25dp"
android:layout_toRightOf="#+id/image4"
android:layout_height="13dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:layout_below="#+id/linearImage1"
android:weightSum="4">
<TextView
android:id="#+id/onne"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="150 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/twoe"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="250 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="500 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/foure"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginRight="6dp"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="1 LTR"
android:textAllCaps="true"
android:textSize="14sp"/>
</LinearLayout>
<View
android:id="#+id/viewp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:alpha="4"
android:background="#000000"
android:layout_below="#+id/linearscale"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearscale"
android:orientation="horizontal"
android:layout_marginTop="28dp"
android:weightSum="2"
>
<TextView
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_marginLeft="86.5dp"
android:textSize="16sp"
android:text="CANCEL"
android:textAllCaps="true"
android:textColor="#293448" />
<TextView
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="48.5dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_marginRight="6dp"
android:background="#fff"
android:text="SUBMIT"
android:textAllCaps="true"
android:textColor="#21c8d7" />
</LinearLayout>
</RelativeLayout>
I made a stopwatch using chronometer with four buttons but when i use the visibility modes to make stop and pause button appear they overlap.... Pls explain why...Below is the code.....
Assume the layout file with buttons in relative layout...
public class StopWatchFragment extends Fragment {
Chronometer chronometer;
Button startStopWatch;
Button stopStopWatch;
Button resetStopWatch;
Button pauseStopWatch;
Button resumeStopWatch;
private long lastPause;
//RelativeLayout relativeLayout;
private int check = 0;
public StopWatchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stopwatch_layout,container,false);
chronometer = (Chronometer) rootView.findViewById(R.id.stopwatch);
startStopWatch = (Button) rootView.findViewById(R.id.startStopWatch);
stopStopWatch = (Button) rootView.findViewById(R.id.stopStopWatch);
resetStopWatch = (Button) rootView.findViewById(R.id.resetStopWatch);
pauseStopWatch = (Button) rootView.findViewById(R.id.pauseStopWatch);
resumeStopWatch = (Button) rootView.findViewById(R.id.resumeStopWatch);
relativeLayout = (RelativeLayout) rootView.findViewById(R.id.parentRelativeLayout);
pauseStopWatch.setVisibility(View.GONE);
//final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayout.getLayoutParams());
startStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
startStopWatch.setVisibility(View.GONE);
pauseStopWatch.setVisibility(View.VISIBLE);
if(check == 1){
resumeStopWatch.setClickable(true);
}
else{
resumeStopWatch.setClickable(false);
}
//params.setMargins(16,16,16,16);
//pauseStopWatch.setLayoutParams(params);
}
});
pauseStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
check = 1;
lastPause = SystemClock.elapsedRealtime();
chronometer.stop();
}
});
resumeStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
chronometer.start();
resumeStopWatch.setClickable(false);
}
});
stopStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
resumeStopWatch.setClickable(false);
}
});
resetStopWatch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.stop();
resumeStopWatch.setClickable(false);
startStopWatch.setVisibility(View.VISIBLE);
pauseStopWatch.setVisibility(View.GONE);
}
});
return rootView;
}
}
This is the layout file pls refer for this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
This is the way buttons work when click on start button
You are hiding the button, but you are still listening with the OnClickListener. Try adding:
pauseStopWatch.setOnClickListener(null);
Then, when you make your button visible:
pauseStopWatch.setOnClickListener(whatever);
Try this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/parentRelativeLayout"
android:layout_height="match_parent"
android:padding="#dimen/activity_horizontal_margin">
<Chronometer
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textAlignment="center"
android:id="#+id/stopwatch"
android:layout_margin="16dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/stopwatch">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/startStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:visibility="gone"
android:id="#+id/pauseStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:background="#drawable/buttonshape"
android:textSize="24sp" />
<Button
android:id="#+id/stopStopWatch"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/startStopWatch"
android:background="#drawable/buttonshape"
android:text="Stop"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/resetStopWatch"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Reset"
android:textSize="24sp" />
<Button
android:id="#+id/resumeStopWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshape"
android:text="Resume"
android:textSize="24sp"
android:layout_marginTop="16dp"
android:layout_alignParentRight="true"
android:layout_below="#id/resetStopWatch"/>
</LinearLayout>
</LinearLayout>
In the above, the pause button will be set to Gone. You make it visible later when user clicks start button
How to generate custom dialog box in android like this,
I want just like this.
How to generate it. please give me suggestion.
i have used below code for dialog box, what is the problem in my code?
I have not identify it. please share me any Idea.
<?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:background="#android:color/transparent">
<RelativeLayout
android:id="#+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/btn_white"
android:paddingBottom="#dimen/thirty_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/ten_dp">
<TextView
android:id="#+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/ten_dp"
android:text="Quit LEarning?"
android:textSize="#dimen/twenty_sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/twenty_dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:id="#+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video" />
</LinearLayout>
</RelativeLayout>
Please share me any Idea.
Thanks.
Simple, 1st need to create an newcustom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="250dp"
android:layout_gravity="center"
android:layout_marginLeft="55dp"
android:layout_marginRight="55dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="165dp"
android:layout_gravity="center"
android:layout_marginEnd="60dp"
android:layout_marginStart="60dp"
app:cardCornerRadius="8dp"
app:cardElevation="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="Quit Earning?"
android:textColor="#android:color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. quit Earning?"
android:textSize="18dp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/frmNo"
android:layout_marginRight="45dp"
android:layout_marginTop="75dp">
<android.support.design.widget.FloatingActionButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#android:color/transparent"
app:backgroundTint="#color/fab2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="6dp"
android:text="No"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/frmOk"
android:layout_marginLeft="50dp"
android:layout_marginTop="75dp">
<android.support.design.widget.FloatingActionButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#android:color/transparent"
app:backgroundTint="#color/fab1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="6dp"
android:text="Ok"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
</FrameLayout>
Then, in java file (in activity) paste this code
public class ViewDialog {
public void showDialog(Activity activity) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.newcustom_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
FrameLayout mDialogNo = dialog.findViewById(R.id.frmNo);
mDialogNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
FrameLayout mDialogOk = dialog.findViewById(R.id.frmOk);
mDialogOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
dialog.show();
}
}
Finally you can call it wherever you want.
ViewDialog alert = new ViewDialog();
alert.showDialog(CustomDialogActivity.this);
Inside your dailog.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:background="#android:color/white">
<RelativeLayout
android:id="#+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/btn_white"
android:paddingBottom="#dimen/thirty_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/ten_dp">
<TextView
android:id="#+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/ten_dp"
android:text="Quit LEarning?"
android:textSize="#dimen/twenty_sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/twenty_dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
<Button
android:id="#+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
</LinearLayout>
</RelativeLayout>
open dimens.xml and add code mentioned below
<dimen name="thirty_dp">30dp</dimen>
<dimen name="ten_dp">10dp</dimen>
<dimen name="twenty_sp">20sp</dimen>
<dimen name="twenty_dp">20dp</dimen>
<dimen name="sixteen_sp">16sp</dimen>
open drawable and create btn_white.xml add code mentioned blow
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp"></corners>
<solid android:color="#android:color/white"></solid>
</shape>
open mainactivity.java and add the code mentioned below
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dailog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
Button videoButton = (Button) dialog.findViewById(R.id.btn_video);
// if decline button is clicked, close the custom dialog
videoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />
<Button
android:id="#+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="#android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>
You can change your button by using
android:src=#drawable/image
You have to extends Dialog and implements OnClickListener
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}
Call Dialog
CustomDialogClass cdd=new CustomDialogClass(Activity.this);
cdd.show();
Maybe try this method
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="#color/colorPrimary"
app:cardCornerRadius="16dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_all"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorButtonNormal"
android:text="Tebrikler!"
android:layout_gravity="center_horizontal"
android:textSize="36sp"
android:padding="8dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnDialogCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Çıkış"
android:textColor="#FFF"
android:layout_marginRight="5dp"
android:background="#drawable/dialog_button_background"
android:layout_gravity="center_horizontal"/>
<Button
android:id="#+id/btnDialogOk"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Devam"
android:textColor="#FFF"
android:layout_marginLeft="5dp"
android:background="#drawable/dialog_button_background"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
main.java
Dialog dialog = new Dialog(context, R.style.CustomDialog);
LayoutInflater layoutInflater = LayoutInflater.from(context);
CardView cardView = (CardView) layoutInflater.inflate(R.layout.dialog, null);
dialog.setContentView(cardView);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
dialogBtnCancel();
private void dialogBtnCancel(){
mBtnDialogCancel = dialog.findViewById(R.id.btnDialogCancel);
mBtnDialogOk = dialog.findViewById(R.id.btnDialogOk);
mBtnDialogCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
mBtnDialogOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
}
I want to provide lock screen, so every time user opens my app, he will be forced to enter pin. So I am looking for Android's default pin lock layout (as shown on image), which should work from Android 4.0. Can you tell me where to find layout of this or how to implement it properly?
I git below project from github and little changed it to provide a GUI like your image :
https://github.com/chinloong/Android-PinView
so create a project and in mainActivity insert this codes:
public class PinEntryView extends Activity {
String userEntered;
String userPin="8888";
final int PIN_LENGTH = 4;
boolean keyPadLockedFlag = false;
Context appContext;
TextView titleView;
TextView pinBox0;
TextView pinBox1;
TextView pinBox2;
TextView pinBox3;
TextView statusView;
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button buttonExit;
Button buttonDelete;
EditText passwordInput;
ImageView backSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appContext = this;
userEntered = "";
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_layout);
//Typeface xpressive=Typeface.createFromAsset(getAssets(), "fonts/XpressiveBold.ttf");
statusView = (TextView) findViewById(R.id.statusview);
passwordInput = (EditText) findViewById(R.id.editText);
backSpace = (ImageView) findViewById(R.id.imageView);
buttonExit = (Button) findViewById(R.id.buttonExit);
backSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
passwordInput.setText(passwordInput.getText().toString().substring(0,passwordInput.getText().toString().length()-2));
}
});
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Exit app
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
appContext.startActivity(i);
finish();
}
}
);
//buttonExit.setTypeface(xpressive);
buttonDelete = (Button) findViewById(R.id.buttonDeleteBack);
buttonDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true)
{
return;
}
if (userEntered.length()>0)
{
userEntered = userEntered.substring(0,userEntered.length()-1);
passwordInput.setText("");
}
}
}
);
titleView = (TextView)findViewById(R.id.time);
//titleView.setTypeface(xpressive);
View.OnClickListener pinButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
if (keyPadLockedFlag == true)
{
return;
}
Button pressedButton = (Button)v;
if (userEntered.length()<PIN_LENGTH)
{
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered="+userEntered);
//Update pin boxes
passwordInput.setText(passwordInput.getText().toString()+"*");
passwordInput.setSelection(passwordInput.getText().toString().length());
if (userEntered.length()==PIN_LENGTH)
{
//Check if entered PIN is correct
if (userEntered.equals(userPin))
{
statusView.setTextColor(Color.GREEN);
statusView.setText("Correct");
Log.v("PinView", "Correct PIN");
finish();
}
else
{
statusView.setTextColor(Color.RED);
statusView.setText("Wrong PIN. Keypad Locked");
keyPadLockedFlag = true;
Log.v("PinView", "Wrong PIN");
new LockKeyPadOperation().execute("");
}
}
}
else
{
//Roll over
passwordInput.setText("");
userEntered = "";
statusView.setText("");
userEntered = userEntered + pressedButton.getText();
Log.v("PinView", "User entered="+userEntered);
//Update pin boxes
passwordInput.setText("8");
}
}
};
button0 = (Button)findViewById(R.id.button0);
//button0.setTypeface(xpressive);
button0.setOnClickListener(pinButtonHandler);
button1 = (Button)findViewById(R.id.button1);
//button1.setTypeface(xpressive);
button1.setOnClickListener(pinButtonHandler);
button2 = (Button)findViewById(R.id.button2);
//button2.setTypeface(xpressive);
button2.setOnClickListener(pinButtonHandler);
button3 = (Button)findViewById(R.id.button3);
//button3.setTypeface(xpressive);
button3.setOnClickListener(pinButtonHandler);
button4 = (Button)findViewById(R.id.button4);
//button4.setTypeface(xpressive);
button4.setOnClickListener(pinButtonHandler);
button5 = (Button)findViewById(R.id.button5);
//button5.setTypeface(xpressive);
button5.setOnClickListener(pinButtonHandler);
button6 = (Button)findViewById(R.id.button6);
//button6.setTypeface(xpressive);
button6.setOnClickListener(pinButtonHandler);
button7 = (Button)findViewById(R.id.button7);
//button7.setTypeface(xpressive);
button7.setOnClickListener(pinButtonHandler);
button8 = (Button)findViewById(R.id.button8);
//button8.setTypeface(xpressive);
button8.setOnClickListener(pinButtonHandler);
button9 = (Button)findViewById(R.id.button9);
//button9.setTypeface(xpressive);
button9.setOnClickListener(pinButtonHandler);
buttonDelete = (Button)findViewById(R.id.buttonDeleteBack);
//buttonDelete.setTypeface(xpressive);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
//App not allowed to go back to Parent activity until correct pin entered.
return;
//super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_pin_entry_view, menu);
return true;
}
private class LockKeyPadOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for(int i=0;i<2;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
statusView.setText("");
//Roll over
passwordInput.setText("");
;
userEntered = "";
keyPadLockedFlag = false;
}
#Override
protected void onPreExecute() {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
}
then create main_layout.xml file and insert below xml codes:
<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:background="#drawable/image_background"
>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/backspace"
android:layout_above="#+id/view"
android:layout_marginBottom="10dp"
android:layout_alignRight="#+id/view"
android:id="#+id/imageView" />
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#FFF"
android:layout_above="#+id/numericPad"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:id="#+id/view" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numericPad"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="20dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:shrinkColumns="*"
android:stretchColumns="*"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button1"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button2"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button3"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="3"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button4"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="4"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button5"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="5"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button6"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="6"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/button7"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="7"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button8"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="8"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button9"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="9"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/buttonExit"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Exit"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/button0"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
<Button
android:id="#+id/buttonDeleteBack"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Delete"
android:textSize="25sp"
android:textColor="#ffffff"
android:padding="6dip"
android:layout_margin="3dip"
android:background="#android:color/transparent"
>
</Button>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableRow>
</TableLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText"
android:layout_alignBottom="#+id/imageView"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Password"
android:id="#+id/statusview"
android:layout_below="#+id/time"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="23:17"
android:id="#+id/time"
android:textSize="100sp"
android:layout_marginTop="64dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
note : userPin variable is your password and you can change it.
in below bock in activity class you should insert your code that you want execute if user enter correnct password (e.g start a new activity )
if (userEntered.equals(userPin))
{
statusView.setTextColor(Color.GREEN);
statusView.setText("Correct");
Log.v("PinView", "Correct PIN");
finish();
}
Note: add below line to main activity node in mainfist.xml file
android:theme="#android:style/Theme.NoTitleBar"
so your Activity node must be like :
<activity
android:name="com.example.MainActivity"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I've look in the android documentation on how to implement alertdialog with a xml layout
here is the documentation
when i run the program and click the todo button the program crashes can anyone help?
here's my source code:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StartMoving extends Activity implements OnClickListener {
Button todo;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start_moving);
todo = (Button) findViewById(R.id.bTodo);
todo.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bTodo:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.to_do_list, null);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
break;
default:
break;
}
}
}
here's the xml file named to_do_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<ScrollView
android:id="#+id/svTips"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="87" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTipsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:text="#string/tips"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvtmb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/two_months_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbSAP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_sort_and_purge"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_sap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_sap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_research"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_r_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_r_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvtmbCAMB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tmb_create_a_moving_builder"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvtmb_camb_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/tmb_camb_content" />
<TextView
android:id="#+id/tvswb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/six_weeks_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_ordersupplies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_order_supplies"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_os_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_os_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_use_it_or_lose_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_use_it_or_lose_it"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_uioli_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_uioli_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvswb_take_measurement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/swb_take_measurement"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvswb_tm_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/swb_tm_content" />
<TextView
android:id="#+id/tvomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/one_month_before"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_choose_your_mover_and_confirm_the_arragements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_choose_your_mover_and_confirm_the_arragements"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_cymacta_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_cymacta_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_begin_packing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_begin_paking"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_bp_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_bp_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_label"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_l_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_l_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_separate_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_separate_values"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_sv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_sv_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_do_a_change_of_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_do_a_change_of_address"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_dacoa_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_dacoa_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvomb_notify_important_parties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omb_notify_all_important_parties"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tvomb_naip_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/omb_naip_content" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="13"
android:orientation="horizontal" >
<Button
android:id="#+id/bReturntoTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go back to top" />
<Button
android:id="#+id/bgoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Go to" />
</LinearLayout>
</LinearLayout>
this is what the logcat shows
use
Context mContext = v.getApplicationContext();
instead of
Context mContext = getApplicationContext();
or
builder = new AlertDialog.Builder(StartMoving.this);
EDIT :
You are reading button
todo = (Button) findViewById(R.id.bgoto); <-----------
^^^^^
todo.setOnClickListener(this);
AlertDialog.Builder diag = new AlertDialog.Builder(this);
diag.setTitle("DIALOG TITLE")
.setMessage("YOUR MESSAGE")
.setCancelable(false)
.setNegativeButton("WHAT EVER THE BUTTON TEXT",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enter code here
//do what ever the button in the dialog is clicked
dialog.cancel();
}
});
diag.show();
Easiest way to have a alert dialog
and dont forget to change the Button id in java to R.id.goto
if your are using alertDialog in TabHost then you have to write..
final AlertDialog alertDialog = new AlertDialog.Builder(**getParent()**).create();
//alertDialog.setTitle("Alert....");
alertDialog.setMessage("Your Text");
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
finish();
}
});
alertDialog.show();
It seems that I've been reading the button from the Context and not from the alertDialog. The solution was to create a View that will be set as View for the AlertDialog and declare a button and set it as a child for the View.
final View view;
LayoutInflater inf = LayoutInflater.from(StartMoving.this);
view = inf.inflate(R.layout.rename, null);
final EditText newname = (EditText) view.findViewById(R.id.etNewRoomName);
final Button todo = (BUtton) view.findViewById(R.id.bTodo);
new AlertDialog.Builder(StartMoving.this)
.setView(view)
.setTitle("Rename")
.setMessage("Enter new name for room " + currentRoom)
.show();