Close Custom Dialog from another function - android

I am trying to modify the layout of a dialog and then perform some function and close the alert box as my function is over.
Layout File
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:layout_below="#+id/header"
android:paddingTop="15dp"
android:paddingBottom="15dp">
<ImageView
android:id="#+id/cam"
android:layout_width="0dp"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_cam"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="camera_listener"
/>
<ImageView
android:id="#+id/gal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_gal"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="gallery_listener"
/>
</LinearLayout>
Java File
AlertDialog.Builder myAlertDialog; // Variable declared as a class member
private void startDialog() {
LayoutInflater inflater = this.getLayoutInflater();
final View view = inflater.inflate(R.layout.activity_fab, null);
myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.show();
}
public void gallery_listener(View view) {
pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
myAlertDialog.setOnDismissListener();
}
public void camera_listener(View view) {
pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(pictureActionIntent, CAMERA_REQUEST);
}
I'm displaying two images in dialog and defining function on their click... i want to close the DIALOG as soon as corresponding function is performed.
I tried using dismiss but it is not working..!

try this in your second method,
in Main.java
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class DialogCustom extends Activity {
AlertDialog myAlertDialog;
ImageView ivOne,ivTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_custom);
ivOne=(ImageView)findViewById(R.id.cam);
ivTwo=(ImageView)findViewById(R.id.gal);
myAlertDialog = new AlertDialog.Builder(this).create();
ivOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startDialog();
}
});
ivTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
dismis();
}
});
}
});
}
private void startDialog() {
myAlertDialog.show();
}
public void dismis()
{
myAlertDialog.dismiss();
}
}
and in main.xml file.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_below="#+id/header"
android:orientation="horizontal"
android:paddingBottom="15dp"
android:paddingTop="15dp" >
<ImageView
android:id="#+id/cam"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="camera_listener"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/gal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:onClick="gallery_listener"
android:padding="10dp"
android:paddingLeft="10dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>

Alternate i found... is first create a alert dialog using builder then assign that to a dialog and use dismiss function with that
Update-
private AlertDialog dialog; // Variable with class scope
private void startDialog() {
LayoutInflater inflater = this.getLayoutInflater();
final View view = inflater.inflate(R.layout.activity_fab, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setView(view);
dialog = builder.create();
dialog.show();
}
public void gallery_listener(View view) {
pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
dialog.dismiss();
}
This has worked for me... if there is any better method plz share!

Related

Use java file for a xml layout file used in a dialog box

I need to use a .java file to perform actions for a .xml layout. I have used this layout for a dialog box. I have linked the .java file to that layout but it doesn't seem to work at all.
This is the MainActivity
package com.example.ayush.projectfive;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alrt.show();
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
This is the activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
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.example.ayush.projectfive.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
This is the dialog box layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LOGIN"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="20dp"
android:id="#+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"
android:id="#+id/button3" />
This is the .java file I'd like to link with.
package com.example.ayush.projectfive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends AppCompatActivity{
EditText et, et2;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
et = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
btn2 = (Button) findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = et.getText().toString();
String s2 = et.getText().toString();
if(s1.equals(s2)){
Toast.makeText(Second.this, "Login Successful", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Second.this,Third.class);
startActivity(i);
}
else{
}
}
});
}
}
This is the .java file I'd like to link with [...]
public class Second extends AppCompatActivity{
Okay... That's what this code does
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
If you are trying to use the views that are contained within the dialog box and respond to the buttons there to login, then you need to remove the button from the activity, add alrt.show() in the onCreate instead of onClick and move the Activity start code into the dialog's onClick handlers.
It's also worth mentioning that the object is a builder, so you can do this
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.mylayout, null);
final EditText editUsername = dialogView.findViewById(R.id.editText);
// TODO: Get other views from mylayout.xml
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("Login")
.setCancelable(false)
.setView(dialogView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String username = editUsername.getText().toString();
// TODO: Get more variables
// TODO: Verify credentials
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
})
.setNegativeButton("CANCEL", null)
.show();
And, as stated earlier, remove btn.setOnClickListener
just add alrt.show(); in your MainActivity.java e.g. here:
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.show();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}

Custom popup dialog with input field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I create popup input field for Android?
I need Xml and Java Code.
try to use this custom popup dialog code
main.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" >
<Button
android:id="#+id/buttonPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Prompt Dialog" />
<EditText
android:id="#+id/editTextResult"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
Custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Your Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>
main.java
package cm.kikani.kalpesh;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private EditText result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// components from main.xml
button = (Button) findViewById(R.id.buttonPrompt);
result = (EditText) findViewById(R.id.editTextResult);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.custom, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
You can create a custom dialog class for your case.
your_custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#android:color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_exit"
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="16sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/blue"
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="#android:color/white"
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="#android:color/blue"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
and you CustomDialog class must extend Dialog
public class CustomDialogClass extends Dialog {
public Activity activity;
public Dialog dialog;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = 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(new OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
no.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
dismiss();
}
});
}
}
and you can call it like this
CustomDialogClass customDialog =new CustomDialogClass(activity);
customDialog .show();

ImageView is not clickeble?

I am using Imageview 2 times in my MainActivity.java class by using LayoutInflater.In that activity i am inflate so many layouts by using scroll every thing works fine.But this imageview is not working properly.When user click on imageview it shows popup.It works when i am show first time inflate.It's not working second time inflate.
In my main activity contain edittext.when user enter High in edittext i want to show 2 imageview times.If user enter Low i want to show single imageview.One imageview shows top of the screen and one shows bottom of the screen.
Here my bit of code.
This is my imageview.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/attach_photo_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp" >
<TextView
android:id="#+id/attach_photo_titile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="#string/attach_photo_to_job"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/attach_photo_camera_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/attach_photo_titile_textView"
android:contentDescription="#string/app_name"
android:clickable="true"
android:src="#drawable/image_placeholder" />
</RelativeLayout>
I am using this layout in my main.xml by using .
And i am call that imageview.xml layout in My MainActivity.java by using LayoutInflater.
Here is my code:
public void getAudioRecordLayout() {
testView = testInflater.inflate(
getResources().getLayout(
R.layout.imageview), null);
mWorkDetailAttachImageView = (ImageView) findViewById(R.id.attach_photo_camera_ImageView);
mIncludeHighLayout = (LinearLayout) findViewById(R.id.include_High);
mIncludeLowLayout = (LinearLayout) findViewById(R.id.include_Low);
mIncludeRecordAudioInWorkDetailLayout.setVisibility(View.VISIBLE);
mIncludeRecordAudioInRecordOfInspectionLayout
.setVisibility(View.GONE);
mWorkDetailAttachImageView.setOnClickListener(this);
}
Edit # 1 :
When i am showing imageview second time that imageview shows inside another inflate view.
Image view OnclickListiener :
if (v == mWorkDetailAttachImageView) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
TestResultsActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
Edit # 2
MainActivity.java
package rajesh.dropdowndemo;
import java.security.PublicKey;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;
/** Textview holding the title of the droddown */
private TextView mDropdownTitle;
ImageView imagView;
LinearLayout commonIncludeLayout, includeHighLayout;
int i = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getImageViewLayout(1);
getImageViewLayout(2);
// getHighLayout();
mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);
final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);
dropDownTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
openDropdown();
} else {
closeDropdown();
}
}
});
alt0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.VISIBLE);
dropDownTextView.setText(R.string.alt0);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt0,
Toast.LENGTH_SHORT).show();
}
});
alt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt1);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt1,
Toast.LENGTH_SHORT).show();
}
});
alt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt2);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
Toast.makeText(getBaseContext(), R.string.alt2,
Toast.LENGTH_SHORT).show();
}
});
}
/**
* Animates in the dropdown list
*/
private void openDropdown() {
if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
anim.setDuration(getResources().getInteger(
R.integer.dropdown_amination_time));
mDropdownFoldOutMenu.startAnimation(anim);
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_close, 0);
mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
}
}
/**
* Animates out the dropdown list
*/
private void closeDropdown() {
if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
anim.setDuration(getResources().getInteger(
R.integer.dropdown_amination_time));
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mDropdownFoldOutMenu.setVisibility(View.GONE);
}
});
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_open, 0);
mDropdownFoldOutMenu.startAnimation(anim);
}
}
/**
*
* This represents Inflate the layout into main.xml layout
*/
public void getImageViewLayout(final int aa) {
// LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
// View view = inflater.inflate(R.layout.imageview, null);
RelativeLayout outer;
if (aa == 1)
outer = (RelativeLayout) findViewById(R.id.image_layout);
else
outer = (RelativeLayout) findViewById(R.id.image_layout1);
imagView = (ImageView) outer
.findViewById(R.id.attach_photo_camera_ImageView);
imagView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Imageview Clicked");
if (aa == 1)
imagView.setTag(1);
else
imagView.setTag(2);
LayoutInflater inflater = LayoutInflater
.from(MainActivity.this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 100);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 200);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
// get Image
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
System.out.println(imagView.getTag().toString());
String i = imagView.getTag().toString();
if (imagView.getTag(1) == i)
((ImageView) imagView.getTag(1)).setImageBitmap(thumbnail);
else if(imagView.getTag(2) != null)
((ImageView) imagView.getTag(2)).setImageBitmap(thumbnail);
} else if (resultCode == RESULT_CANCELED) {
System.out.println(imagView.getTag());
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
.show();
}
if (requestCode == 200 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bit = (Bitmap) BitmapFactory.decodeFile(picturePath);
System.out.println(imagView.getTag().toString());
String i = imagView.getTag().toString();
if (imagView.getTag() == i)
((ImageView) imagView.getTag(1)).setImageBitmap(bit);
else if(imagView.getTag(2) != null)
((ImageView) imagView.getTag(2)).setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT)
.show();
}
}
}
main.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:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/dropdown_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_background"
android:drawableRight="#drawable/icn_dropdown_open"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="Dropdown alts:" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="10dip"
android:padding="100dip"
android:text="other content" />
<LinearLayout
android:id="#+id/include_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:orientation="vertical">
<include layout="#layout/imageview"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dropdown_foldout_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="2dip"
android:background="#drawable/dropdown_background"
android:orientation="vertical"
android:padding="1dip"
android:visibility="gone" >
<TextView
android:id="#+id/dropdown_alt0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:drawableRight="#drawable/icn_dropdown_checked"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt0" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt1" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt2" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_high_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/high_layout"/>
</LinearLayout>
</RelativeLayout>
imageview.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="wrap_content"
android:orientation="vertical"
android:background="#android:color/background_dark" >
<TextView
android:id="#+id/attach_photo_titile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="5dp"
android:text="Attach Photo"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/attach_photo_camera_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/attach_photo_titile_textView"
android:contentDescription="#string/app_name"
android:clickable="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
dailog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/take_new_photo_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="Take New Photo" />
<Button
android:id="#+id/choose_from_existing_img_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Choose Existing" />
<Button
android:id="#+id/cancel_img_dialog_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</LinearLayout>
string.xml
<resources>
<string name="app_name">DropDownDemo</string>
<string name="alt0">High</string>
<string name="alt1">Low</string>
<string name="alt2">General</string>
</resources>
high_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
layout="#layout/imageview"/>
</LinearLayout>
Please any one help me.
Try this..
testView = testInflater.inflate(getResources().getLayout(R.layout.imageview), null);
mWorkDetailAttachImageView = (ImageView) testView.findViewById(R.id.attach_photo_camera_ImageView);
add in your click
mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
}
});
Or
if (v.getId() == R.id.attach_photo_camera_ImageView) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
TestResultsActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
EDIT
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
testView = inflater.inflate(R.layout.imageview, null);
EDIT 1
<?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:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/dropdown_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_background"
android:drawableRight="#drawable/icn_dropdown_open"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="Dropdown alts:" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="10dip"
android:padding="100dip"
android:text="other content" />
<LinearLayout
android:id="#+id/include_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:orientation="vertical">
<include
android:id="#+id/image_layout"
layout="#layout/imageview"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dropdown_foldout_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_textview"
android:layout_marginTop="2dip"
android:background="#drawable/dropdown_background"
android:orientation="vertical"
android:padding="1dip"
android:visibility="gone" >
<TextView
android:id="#+id/dropdown_alt0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:drawableRight="#drawable/icn_dropdown_checked"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt0" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt1" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
<TextView
android:id="#+id/dropdown_alt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/dropdown_selector"
android:gravity="center_vertical|left"
android:padding="10dip"
android:text="#string/alt2" />
</LinearLayout>
<LinearLayout
android:id="#+id/include_high_imageview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<include
android:id="#+id/image_layout1"
layout="#layout/imageview"/>
</LinearLayout>
</RelativeLayout>
JAVA
package rajesh.dropdowndemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Layout holding the droddown view */
private LinearLayout mDropdownFoldOutMenu;
/** Textview holding the title of the droddown */
private TextView mDropdownTitle;
LinearLayout commonIncludeLayout,includeHighLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getImageViewLayout(1);
getImageViewLayout(2);
mDropdownFoldOutMenu = ((LinearLayout) findViewById(R.id.dropdown_foldout_menu));
mDropdownTitle = ((TextView) findViewById(R.id.dropdown_textview));
commonIncludeLayout = (LinearLayout) findViewById(R.id.include_imageview_layout);
includeHighLayout = (LinearLayout) findViewById(R.id.include_high_imageview_layout);
final TextView dropDownTextView = (TextView) findViewById(R.id.dropdown_textview);
final TextView alt0 = (TextView) findViewById(R.id.dropdown_alt0);
final TextView alt1 = (TextView) findViewById(R.id.dropdown_alt1);
final TextView alt2 = (TextView) findViewById(R.id.dropdown_alt2);
dropDownTextView.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
if (mDropdownFoldOutMenu.getVisibility() == View.GONE) {
openDropdown();
} else {
closeDropdown();
}
}
});
alt0.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.VISIBLE);
dropDownTextView.setText(R.string.alt0);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt0, Toast.LENGTH_SHORT).show();
}
});
alt1.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt1);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
Toast.makeText(getBaseContext(), R.string.alt1, Toast.LENGTH_SHORT).show();
}
});
alt2.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
includeHighLayout.setVisibility(View.GONE);
dropDownTextView.setText(R.string.alt2);
closeDropdown();
alt0.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
alt2.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_checked, 0);
Toast.makeText(getBaseContext(), R.string.alt2, Toast.LENGTH_SHORT).show();
}
});
}
/**
* Animates in the dropdown list
*/
private void openDropdown() {
if (mDropdownFoldOutMenu.getVisibility() != View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 0, 1);
anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
mDropdownFoldOutMenu.startAnimation(anim);
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_close, 0);
mDropdownFoldOutMenu.setVisibility(View.VISIBLE);
}
}
/**
* Animates out the dropdown list
*/
private void closeDropdown() {
if (mDropdownFoldOutMenu.getVisibility() == View.VISIBLE) {
ScaleAnimation anim = new ScaleAnimation(1, 1, 1, 0);
anim.setDuration(getResources().getInteger(R.integer.dropdown_amination_time));
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mDropdownFoldOutMenu.setVisibility(View.GONE);
}
});
mDropdownTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0,
R.drawable.icn_dropdown_open, 0);
mDropdownFoldOutMenu.startAnimation(anim);
}
}
/**
*
* This represents Inflate the layout into main.xml layout
*/
public void getImageViewLayout(int aa){
LinearLayout outer;
if(aa == 1)
outer = (LinearLayout) findViewById(R.id.image_layout);
else
outer = (LinearLayout) findViewById(R.id.image_layout1);
ImageView imagView = (ImageView) outer.findViewById(R.id.attach_photo_camera_ImageView);
imagView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Imageview Clicked");
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(R.layout.add_picture_image_dialog,
null);
final AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
Button cancel = (Button) view
.findViewById(R.id.cancel_img_dialog_btn);
Button takeNew = (Button) view
.findViewById(R.id.take_new_photo_btn);
Button chooseExisting = (Button) view
.findViewById(R.id.choose_from_existing_img_btn);
takeNew.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
chooseExisting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
}
});
}
}
Did you checked view in your onClick function like below..
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.attach_photo_camera_ImageView:
//your click code
break;
}
or try below code instad of your code..
mWorkDetailAttachImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

null pointer exception in splash

I have a main activity which should load a small splash xml file for 2 or 3 seconds when the app is opened. I tried this snippet of code in the oncreate before adding it to my major project. Keep in mind, both apps worked seperately but for some reason, i get a null pointer exception when running the app..... help?
MainActivity:
package com.Depauw.dpuhelpdesk;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button knowledgeBase, submitRequest, helpme, faq, technician, call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//display the logo during 5 secondes,
new CountDownTimer(2000,1000){
public void onTick(long millisUntilFinished){}
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
Initialize();
}
private void Initialize(){
knowledgeBase = (Button) findViewById(R.id.knowledgebase1);
submitRequest = (Button) findViewById(R.id.submitrequest1);
helpme = (Button) findViewById(R.id.helpButton);
faq = (Button) findViewById(R.id.faqButton);
technician = (Button) findViewById(R.id.submitrequest2);
call = (Button) findViewById(R.id.callButton);
knowledgeBase.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent knowledgeIntent = new Intent(MainActivity.this, knowledgebase1_activity.class);
startActivity(knowledgeIntent);
}
});
submitRequest.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submitRequest_activity.class);
startActivity(requestIntent);
}
});
helpme.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Help");
builder.setMessage("This app allows you to access the IT KnowledgeBase from DePauw's website." + " " +
"If you experience any issues using our app, please send us an email to helpdesk#depauw.edu or call 765-658-4294");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
builder.setOnCancelListener(null);
}
});
builder.create().show(); // create and show the alert dialog
}
});
faq.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, activity_main_faq.class);
startActivity(requestIntent);
}
});
technician.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent requestIntent = new Intent(MainActivity.this, submit_technician_request.class);
startActivity(requestIntent);
}
});
call.setOnClickListener(new OnClickListener(){ //dialog box
public void onClick(View arg0){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Complete the call?");
builder.setMessage("Click yes to connect your call. Otherwise, click no.");
builder.setCancelable(true);
builder.setIcon(R.drawable.ic_launcher);
//saying yes completes the call. This way, we don't get accidently calls as often
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:17656584294"));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Calling a Phone Number", "Call failed", activityException);
}
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.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;
}
}
splash.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#raw/splash2" />
main_activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".basic_activity1"
tools:ignore="MergeRootFrame"
android:background="#000000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/header"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#EAC117"
android:textSize="35sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#raw/firstintentlogo"
android:layout_weight="1" android:contentDescription="#string/headLogoName"/>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/knowledgebase1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/knowledgebase1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitrequest1"
android:textColor="#EAC117" />
<Button
android:id="#+id/submitrequest2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/submitTechnician"
android:textColor="#EAC117" />
<Button
android:id="#+id/helpButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/helpButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/faqButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/faqButton"
android:textColor="#EAC117" />
<Button
android:id="#+id/callButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/callButton"
android:textColor="#EAC117" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Initialize() is getting called immediately.
This is happening before MainActivity.this.setContentView(R.layout.activity_main) is called.

drawableRight of EditText

Below is my layout:
<EditText
android:id="#+id/account_et"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawableRight="#drawable/icon_backall"
android:ems="10"
android:hint="#string/str_md_email"
android:inputType="textEmailAddress"
android:padding="10dp" >
</EditText>
I want to show the drawableRight when EditText be focused.
And hide while without focus.
Another one is that I want to set OnClickListener of drawableRight.
How can I do?
i would suggest you to add imageView separated from the EditText and align him to be on top of him with align top and align right and that you have full control so you can invisible him and setOnClickListner
<ImageView android:id="#+id/account_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/account_et"
android:layout_alignTop="#+id/account_et"
android:background="#drawable/icon_backall">
</ImageView>
Use View.OnFocusChangeListener. Putting drawable to edittext when you catch the focus, then replacing drawable with null would solve the problem.
I hope my sample will help you
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" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:inputType="textEmailAddress" >
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="43dp"
android:src="#drawable/ic_launcher"
android:visibility="invisible" >
</ImageView>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
this is the class file in which drawable options are performed
MainActivity.java
package com.example.doubtedittext;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText etext;
private ImageView imageView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext = (EditText) findViewById(R.id.editText1);
imageView1 = (ImageView) findViewById(R.id.imageView1);
etext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imageView1.setVisibility(View.VISIBLE);
} else {
imageView1.setVisibility(View.INVISIBLE);
}
}
});
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
the operation you required successfully performed above.....
Use like belwo
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{}
else {}
}
});

Categories

Resources