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
}
});
Related
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!
MainActivity.java
package com.pKLabs.RgpvDigest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import cssyllabus.MainCS;
import static com.pKLabs.RgpvDigest.R.layout.activity_main;
public class MainActivity extends ActionBarActivity implements
OnItemClickListener, AnimationListener {
private DrawerLayout drawerLayout;
private ListView listView;
private ActionBarDrawerToggle drawerListner;
TextView t, t1, t2, t3, t4, t5;
Animation rotation, anim;
private MyAdapter myAdapter;
private static final String AD_UNIT_ID = "ca-app-pub-8935153656417807/4125750777";
private static final String TAG = "MainActivity";
private InterstitialAd iAd;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60); // You can manage the blinking time with this
// parameter
anim.setStartOffset(30);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(2);
iv = (ImageView) findViewById(R.id.imageView1);
rotation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.button_rotate);
rotation.setRepeatCount(Animation.INFINITE);
rotation.setAnimationListener(MainActivity.this);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv.startAnimation(rotation);
t1.startAnimation(anim);
t2.startAnimation(anim);
t3.startAnimation(anim);
t4.startAnimation(anim);
t5.startAnimation(anim);
}
});
t = (TextView) findViewById(R.id.rgpvtext);
t.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv.startAnimation(rotation);
t1.startAnimation(anim);
t2.startAnimation(anim);
t3.startAnimation(anim);
t4.startAnimation(anim);
t5.startAnimation(anim);
}
});
Typeface tf = Typeface.createFromAsset(getApplicationContext()
.getAssets(), "segoe.ttf");
TextView t = (TextView) findViewById(R.id.rgpvtext);
t.setTypeface(tf);
t.setTypeface(tf, tf.BOLD);
iAd = new InterstitialAd(this);
iAd.setAdUnitId(AD_UNIT_ID);
iAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
}
#Override
public void onAdFailedToLoad(int errorCode) {
}
});
loadInterstitial();
listView = (ListView) findViewById(R.id.drawerlist);
myAdapter = new MyAdapter(this);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.dopen, R.string.dclose);
drawerLayout.setDrawerListener(drawerListner);
getSupportActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
t1 = (TextView) findViewById(R.id.compu);
t1.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
t1.setTypeface(tf);
t1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Semester.class);
startActivityForResult(intent, 0);
}
});
t2 = (TextView) findViewById(R.id.calc);
t2.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
t2.setTypeface(tf);
t2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Calculator.class);
startActivityForResult(intent, 0);
}
});
t3 = (TextView) findViewById(R.id.iv);
t3.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
t3.setTypeface(tf);
t3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
InterviewQuestion.class);
startActivityForResult(intent, 0);
}
});
t4 = (TextView) findViewById(R.id.prog);
t4.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
t4.setTypeface(tf);
t4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Programs.class);
startActivityForResult(intent, 0);
}
});
t5 = (TextView) findViewById(R.id.abo);
t5.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
t5.setTypeface(tf);
t5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
startActivityForResult(intent, 0);
}
});
}
private void loadInterstitial() {
// TODO Auto-generated method stub
AdRequest adRequest = new AdRequest.Builder().build();
iAd.loadAd(adRequest);
System.out.println("Ad loaded success");
}
public void showInterstitial() {
if (iAd.isLoaded()) {
iAd.show();
System.out.println("Ad shown success");
} else {
Log.d(TAG, "Interstitial ad is not loaded yet");
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListner.onConfigurationChanged(newConfig);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListner.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("share"); // your desired title here
item.setIcon(R.drawable.share_icon); // your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
try
{ Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "RGPV Digest");
String sAux = "Looking to score high in RGPV ?\n Try out this app :\n\n";
sAux = sAux + "https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName();
i.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(i, "Share via"));
}
catch(Exception e)
{ //e.toString();
}
return true;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerListner.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position == 0) {
Uri uri = Uri
.parse("https://2ce74af0d8de8783b91cdd315eeba0340a4b9277.googledrive.com/host/0B2sDUzNeIK0KODB0ZDRMVjFPaFE/Jobs.html");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, MainCS.class);
startActivityForResult(intent, 0);
} else if (position == 2) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","laboratorypk#gmail.comm", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "Project Help");
intent.putExtra(Intent.EXTRA_TEXT,"Please write a short description of your project and technology you are supposed to use.");
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
// }
else if (position == 3) {
Intent intent = new Intent(this, Careerguid.class);
startActivityForResult(intent, 0);
}
else if (position == 4) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","laboratorypk#gmail.comm", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "PC Games");
intent.putExtra(Intent.EXTRA_TEXT,"Hey! I need PC Games pls share the list of Games that you have.");
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
else if (position == 5) {
Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));
}
}
else if (position == 6) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","laboratorypk#gmail.comm", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "Query/Feedback");
intent.putExtra(Intent.EXTRA_TEXT,"");
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
drawerLayout.closeDrawers();
selectItem(position);
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listView.setItemChecked(position, true);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
showInterstitial();
super.onDestroy();
}
private Toast toast;
private long lastBackPressTime = 0;
#Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to exit",
Toast.LENGTH_LONG);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
drawerLayout.closeDrawers();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
class MyAdapter extends BaseAdapter {
private Context context;
String[] navlist;
int[] images = { R.drawable.job, R.drawable.facebook, R.drawable.project,
R.drawable.career, R.drawable.games, R.drawable.rate, R.drawable.mail };
public MyAdapter(Context context) {
this.context = context;
navlist = context.getResources().getStringArray(R.array.navdra);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return navlist.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return navlist[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.customrow, parent, false);
}
else {
row = convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView11);
ImageView titleImageView = (ImageView) row
.findViewById(R.id.imageView1);
titleTextView.setText(navlist[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
Here is activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ab" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="135dp"
android:layout_height="135dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/rgpvicon" />
<TextView
android:id="#+id/rgpvtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="6dp"
android:clickable="true"
android:text="RGPV Digest"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#drawable/texteff"
android:textSize="36sp" />
<TextView
android:id="#+id/compu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rgpvtext"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="25dp"
android:clickable="true"
android:text="Computer Science"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/texteff"
android:textSize="24sp" />
<TextView
android:id="#+id/calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/compu"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:text="Calculator"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/texteff"
android:textSize="24sp" />
<TextView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/calc"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:text="Interview Question"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/texteff"
android:textSize="24sp" />
<TextView
android:id="#+id/prog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/iv"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:text="Programs"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/texteff"
android:textSize="24sp" />
<TextView
android:id="#+id/abo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/prog"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:text="About"
android:paddingBottom="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#drawable/texteff"
android:textSize="24sp" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
<ListView
android:id="#+id/drawerlist"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#4ABC96"
android:paddingTop="6dp" />
</android.support.v4.widget.DrawerLayout>
LOG:
Process: com.pKLabs.RgpvDigest, PID: 1557
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pKLabs.RgpvDigest/com.pKLabs.RgpvDigest.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.pKLabs.RgpvDigest.MainActivity.onCreate(MainActivity.java:146)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
07-23 07:04:44.900 409-763/? W/ActivityManager﹕ Force finishing activity com.pKLabs.RgpvDigest/.MainActivity
07-23 07:04:45.080 409-428/? D/dalvikvm﹕ GC_FOR_ALLOC freed 312K, 54% free 5989K/12960K, paused 98ms, total 101ms
07-23 07:04:45.410 1557-1574/? D/dalvikvm﹕ DexOpt: --- BEGIN 'ads1355421311.jar' (bootstrap=0) ---
07-23 07:04:45.510 409-428/? I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
Not able to figure out what am i doing wrong, i updated android studio and downloaded API 22 and after that i got many errors in My gradle too. I some how fixed that but it is run time error how can i fix please help me out.
Is your activity added in the manifest?
use getSupportActionBar() instead of getActionBar().
Use
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Instead of
getActionBar().setDisplayHomeAsUpEnabled(true);
and run the app again.
Im searching this since yesterday. I've got on many Activities some piece of code which displays user name and login. I dont want to copy and paste code in layout into every Activity, but I want something just like user controls in .NET. I've read a lot of topic about custom controls but or I don't understand it or its not possible do to this (I dont belive in that option)
Yes it is possible. Look at this sample. It is a custom Numeric Keyboard or a custom View or a custom user control. Customize it and create your own user control:
import java.util.ArrayList;
import org.mabna.order.R;
import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
// this NumericKeyboard works only for EditTexts which
// have a tag with "usesNumericKeyboard" key and value of "true"
// use tag with "ignoreMeForNumericKeyboardTouchListener" key and
// value "true" for controls you do not want to set its touchListener
public class NumericKeyboard extends LinearLayout implements OnTouchListener {
private View mainView = null;
private EditText currentEditText;
private Button btn0;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private ImageButton btnBackSpace;
private ImageButton btnDeleteAll;
public NumericKeyboard(Context context) {
super(context);
}
public NumericKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void initialize() {
LayoutInflater layoutInflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater
.inflate(R.layout.view_numeric_keyboard, this);
btn0 = (Button) view.findViewById(R.id.btn0);
btn1 = (Button) view.findViewById(R.id.btn1);
btn2 = (Button) view.findViewById(R.id.btn2);
btn3 = (Button) view.findViewById(R.id.btn3);
btn4 = (Button) view.findViewById(R.id.btn4);
btn5 = (Button) view.findViewById(R.id.btn5);
btn6 = (Button) view.findViewById(R.id.btn6);
btn7 = (Button) view.findViewById(R.id.btn7);
btn8 = (Button) view.findViewById(R.id.btn8);
btn9 = (Button) view.findViewById(R.id.btn9);
btnBackSpace = (ImageButton) view.findViewById(R.id.btnBackSpace);
btnDeleteAll = (ImageButton) view.findViewById(R.id.btnDeleteAll);
btn0.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "0");
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "1");
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "2");
}
});
btn3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "3");
}
});
btn4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "4");
}
});
btn5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "5");
}
});
btn6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "6");
}
});
btn7.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "7");
}
});
btn8.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "8");
}
});
btn9.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "9");
}
});
btnBackSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "<");
}
});
btnDeleteAll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editCurrentText(v, "D");
}
});
if (mainView == null && !this.isInEditMode()) {
setTouchListenerForChildViews();
this.setVisibility(GONE);
}
}
protected void editCurrentText(View v, String character) {
if (currentEditText != null) {
if (character.compareTo("<") == 0) {
String text = currentEditText.getText().toString();
if (text.length() == 0) {
} else if (text.length() == 1) {
currentEditText.setText("");
} else {
text = text.substring(0, text.length() - 1);
currentEditText.setText(text);
}
} else if (character.compareTo("D") == 0) {
currentEditText.setText("");
} else {
String text = currentEditText.getText().toString();
text += character;
currentEditText.setText(text);
}
}
}
// #Override
// protected void onLayout(boolean changed, int l, int t, int r, int b) {
//
// super.onLayout(changed, l, t, r, b);
// }
public void setTouchListenerForChildViews() {
final Activity act = (Activity) getContext();
mainView = act.getWindow().getDecorView()
.findViewById(android.R.id.content);
if (mainView == null)
return;
ArrayList<View> queue = new ArrayList<View>();
queue.add((View) mainView);
while (!queue.isEmpty()) {
View v = queue.remove(0);
if (v instanceof EditText && v.getTag(R.id.usesNumericKeyboard) == Boolean
.valueOf(true)) {
((EditText) v).setInputType(InputType.TYPE_NULL);
((EditText) v).setCursorVisible(true);
v.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
{
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.currentEditText = (EditText) v;
numericKeyboard.setVisibility(View.VISIBLE);
}
}
}
});
}
if (v instanceof NumericKeyboard) {
} else {
if (v.getTag(R.id.ignoreMeForNumericKeyboardTouchListener) != Boolean
.valueOf(true)) {
v.setOnTouchListener(this);
}
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View vChild = vg.getChildAt(i);
queue.add(vChild);
}
}
}
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v instanceof EditText && v.getTag(R.id.usesNumericKeyboard) == Boolean
.valueOf(true)) {
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.currentEditText = (EditText) v;
numericKeyboard.setVisibility(View.VISIBLE);
}
} else {
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.setVisibility(View.GONE);
}
}
return false;
}
static ArrayList<NumericKeyboard> arrNumericKeyboard =
new ArrayList<NumericKeyboard>();
public static void registerNumericKeyboard(
NumericKeyboard numericKeyboard) {
numericKeyboard.initialize();
arrNumericKeyboard.add(numericKeyboard);
}
public static void unregisterNumericKeyboard(
NumericKeyboard numericKeyboard) {
arrNumericKeyboard.remove(numericKeyboard);
}
public static void showForEditText(EditText editText)
{
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.setVisibility(View.VISIBLE);
numericKeyboard.currentEditText = editText;
}
}
public static void hide()
{
for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
numericKeyboard.setVisibility(View.GONE);
}
}
}
its layout as view_numeric_keyboard.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background06"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn7"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="7" >
</Button>
<Button
android:id="#+id/btn8"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="8" >
</Button>
<Button
android:id="#+id/btn9"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="9" >
</Button>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn4"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="4" >
</Button>
<Button
android:id="#+id/btn5"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="5" >
</Button>
<Button
android:id="#+id/btn6"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="6" >
</Button>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="1" >
</Button>
<Button
android:id="#+id/btn2"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="2" >
</Button>
<Button
android:id="#+id/btn3"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="3" >
</Button>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btnBackSpace"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="fitCenter"
android:src="#drawable/backspace"
android:text="-" />
<Button
android:id="#+id/btn0"
android:layout_width="50dip"
android:layout_height="50dip"
android:text="0" />
<ImageButton
android:id="#+id/btnDeleteAll"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="fitCenter"
android:src="#drawable/remove02"
android:text="-" />
</LinearLayout>
</LinearLayout>
using it in your Activity layout:
<org.mabna.order.ui.NumericKeyboard
android:id="#+id/numericKeyboard1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</org.mabna.order.ui.NumericKeyboard>
have a look at the code below...
i am setting the onclick listener on the textview and the ontouch listener but none of them is working...
what seems to be the problem>....?
AlertDialog.Builder builder;
AlertDialog a;
TextView text,txtNewUser ;
ImageView image;
View layout;
Button ok;
String pswrd;
LayoutInflater inflater;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final Context mContext = this;
Dialog dialog = new Dialog(mContext);
inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
ok = (Button) findViewById(R.id.buttonOK);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
layout = inflater.inflate(R.layout.custom_dialog_private_space,
(ViewGroup) findViewById(R.id.layout));
text = (TextView) dialog.findViewById(R.id.tvDesc);
txtNewUser = (TextView) dialog.findViewById(R.id.tvNewUser);
image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.lock_symbol_android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
a = builder.create();
a.show();
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
return true;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
here is the code of textview from the xml of the dialog layout
<?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:id="#+id/layout" >
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvNewUser"
android:text="New User?"
android:layout_gravity="top|left"
android:clickable="true"
/>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/tvDesc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Enter password"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="100"
android:inputType="textPassword" >
</EditText>
<Button
android:id="#+id/buttonOK"
android:layout_width="100dp"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:text="OK" />
</LinearLayout>
Instead of infalter use window class
return false in on Touch listener
set contenet view to your main activity
package com.collabera.labs.sai;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HomePage extends Activity {
AlertDialog.Builder builder;
AlertDialog a;
TextView text, txtNewUser;
ImageView image;
Button ok;
String pswrd;
public Window mWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_private_space);
ok = (Button) findViewById(R.id.buttonOK);
final Context mContext = this;
Dialog dialog = new Dialog(HomePage.this);
dialog.setContentView(R.layout.custom_dialog_private_space);
dialog.setTitle("Password");
dialog.setCancelable(true);
dialog.show();
mWindow = dialog.getWindow();
Log.w(" dialog is " + mWindow.toString(), "-------");
text = (TextView) mWindow.findViewById(R.id.tvDesc);
txtNewUser = (TextView) mWindow.findViewById(R.id.tvNewUser);
image = (ImageView) mWindow.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
}
I think.. you should also return
return super.onTouchEvent(event);
from onTouch function.. instead of true because true means the event is consumed by touch and other actions are not called for this event.. in this case onClick
this is the way i used to make imageview clickable:
public class Main extends Activity **implements OnClickListener** {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView profile_btn=(ImageView) findViewById(R.id.x_Btn);
x_btn.setOnClickListener(this) ;
ImageView food_btn=(ImageView) findViewById(R.id.y_Btn);
ybtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.x_Btn:
//do some thing;break;
case R.id.y_Btn:
//do some thing;break;
}
}
}
In toast both cases you have written clicked so change to touch in onTouch listener and return false at onTouch Listener:
public class HomePage extends Activity {
private TextView txtNewUser;
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = getApplicationContext();
txtNewUser = (TextView) findViewById(R.id.tvNewUser);
txtNewUser.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
Toast.makeText(mContext, "Touched ", Toast.LENGTH_SHORT)
.show();
Log.v("AS", "Touched");
return false;
}
});
txtNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
Log.v("AS", "Clicked");
}
});
}
Can some one tell me were i am going wrong. i have three activity's that i want to connect togever. This code is my first java file and first xml file. i think my onclick code some where is not right. my end result is that all 3 activity connect with 3 image buttons...thank
java 1.code
package my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
);
}
}
xml.code
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bt" />
<ImageButton
android:id="#id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:onClick="Act2"/>
You have to put the startActivity() into the actual onClick() method.
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}
Change
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
to
ImageView myImage = (ImageView) findViewById(R.id.imageButton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new intent(Newhopeactivity.this, Act2.class);
startActivity(intent);
}
}