I am trying to show a dialog box where ever an ImageButton is pressed, and then choose either pictures from gallery or take some from camera.
I am already struggling to select an image and insert it into the right image view.
Right now, clicking ImageButton (android:id="#+id/imageSelect") will bring up the dialog box, then select choose from gallery, but it insert the image into (android:id="#+id/imageSelect1) which should be inserted into (android:id="#+id/imageSelect).
And when I select (android:id="#+id/imageSelect1) it just overwrite the image, so (android:id="#+id/imageSelect) remains empty
I have a feeling that something is wrong in the SelectPhotoDialog Class.
Doing with without the Dialog box works fine, but I would like to have that option to choose from camera or gallery.
Here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 1" />
<ImageButton
android:id="#+id/imageSelect"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#android:color/white"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 2" />
<ImageButton
android:id="#+id/imageSelect1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#android:color/white"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Image 3" />
<ImageButton
android:id="#+id/imageSelect2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#android:color/white"
android:scaleType="centerCrop"
/>
<EditText
android:id="#+id/input_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName"
android:padding="7dp"
android:singleLine="true"
android:textSize="14sp" />
<EditText
android:id="#+id/input_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Description"
android:inputType="textMultiLine"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="#+id/input_price"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Price"
android:inputType="numberDecimal"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="#+id/input_country"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Country"
android:inputType="textCapWords"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="#+id/input_state_province"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="State/Province/Region"
android:inputType="textCapWords"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="#+id/input_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="City"
android:inputType="textCapWords"
android:padding="7dp"
android:textSize="14sp" />
<EditText
android:id="#+id/input_email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Your Contact Email"
android:inputType="textEmailAddress"
android:padding="7dp"
android:textSize="14sp" />
<Button
android:id="#+id/btn_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:background="#color/colorPrimary"
android:text="Post"
android:textColor="#android:color/white" />
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
My post fragment file:
private void init(){
mSelectImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: opening dialog to choose new photo");
SelectPhotoDialog dialog = new SelectPhotoDialog();
dialog.show(getFragmentManager(), getString(R.string.dialog_select_photo));
dialog.setTargetFragment(PostFragment.this, 1);
}
});
mSelectImage1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: opening dialog to choose new photo");
SelectPhotoDialog dialog1 = new SelectPhotoDialog();
dialog1.show(getFragmentManager(), getString(R.string.dialog_select_photo1));
dialog1.setTargetFragment(PostFragment.this, 2);
}
});
SelectPhotoDialog file:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_selectphoto, container, false);
TextView selectPhoto = (TextView) view.findViewById(R.id.dialogChoosePhoto);
selectPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
}
});
TextView selectPhoto1 = (TextView) view.findViewById(R.id.dialogChoosePhoto1);
selectPhoto1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE1);
}
});
TextView takePhoto = (TextView) view.findViewById(R.id.dialogOpenCamera);
takePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: starting camera.");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
return view;
}
Complete PostFragment file:
https://pastebin.com/PJXB3cq1
Complete SelectPhotoDialog
https://pastebin.com/fF2Jj986
IniversalImageLoader file
Universal image loader
I'm not sure, but i don't think there is enough information... i can't find the UniversalImageLoader that is referenced, i imagine that it handles the actual setting of the image, as setImage implies.
#Override
public void getImagePath(Uri imagePath) {
Log.d(TAG, "getImagePath: setting the image to imageview");
// THIS LINE UniversalImageLoader.setImage(imagePath.toString(), mSelectImage);
//assign to global variable
mSelectedBitmap = null;
mSelectedUri = imagePath;
}
Update to Answer
OK, So here's how i usually perform the dialog you are using
private static final int READ_REQUEST_CODE = 42;
public void performFileSearch()
{
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.setType("*/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData)
{
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Uri uri = null;
if (resultData != null)
{
uri = resultData.getData();
String back = uri.toString();
// DO SOMETHING WITH STRING
}
}
}
SO THEN YOUR CODE WOULD BE
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_selectphoto, container, false);
TextView selectPhoto = (TextView) view.findViewById(R.id.dialogChoosePhoto);
selectPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
performFileSearch()
}
});
TextView selectPhoto1 = (TextView) view.findViewById(R.id.dialogChoosePhoto1);
selectPhoto1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: accessing phones memory.");
performFileSearch();
}
});
TextView takePhoto = (TextView) view.findViewById(R.id.dialogOpenCamera);
takePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: starting camera.");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
return view;
}
// I don't think that this is the cause, just because everything seems to flow through until the part that set's the image... The dialog will return a uri to the image, but another code is setting the image in the wrong place,
ImageView X = (ImageView) findViewById(R.id.ImageViewID);
X.setBackgroundResource ?
Or something similar, is what im looking for, i think it may be causing the problem.
Scratch that
I found the ImageLoader, and everything seems ok ..
Constructor Detail:
ImageLoader
public ImageLoader(RequestQueue queue, ImageLoader.ImageCache imageCache)Constructs a new ImageLoader.
// Parameters:queue - The RequestQueue to use for making image requests.imageCache - The cache to use as an L1 cache.
Have you tried another form of setting the image ?
Related
I just do my project but when I test my app I found that then I touch the screen by using more than one fingers my app may start two or three different activities.
The activities all go to the back stack. Is this a bug in Android framework? But I can't reappear this condition, it just happened.
So, have you guys ever have this problem? Please come and discuss with me. If you do; Thanks.
Supply:
And here is My xml file , when I click the different RelativeLayout at the same time , it happened.
I tried this afternoon , but this condition is not appear anymore. Now I am confusing.
<LinearLayout android:layout_width="match_parent"
android:orientation="vertical"
android:background="#color/appDefaultSingleBlockBackground"
android:layout_height="wrap_content">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true"
android:background="#drawable/mine_bg"
android:id="#+id/mine_goto_personal_info_btn"
>
<TextView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:id="#+id/mine_nick_and_avatar"
android:drawablePadding="10dp"
android:layout_marginLeft="15dp"
style="#style/TextTitle"
android:textColor="#color/appDefaultSingleBlockBackground"
android:text=""/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/venue_maxbutton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/appDefaultSingleBlockBackground"
android:clickable="true"
android:id="#+id/mine_goto_interesting_venue"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_marginLeft="15dp"
style="#style/TextNomal"
android:drawableLeft="#drawable/information_attentionbutton"
android:text="#string/myAtentionVenue"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/venue_maxbutton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/dividerdefault"
/>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/appDefaultSingleBlockBackground"
android:clickable="true"
android:id="#+id/mine_goto_setting"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_marginLeft="15dp"
style="#style/TextNomal"
android:drawableLeft="#drawable/information_setbutton"
android:text="#string/setting"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/venue_maxbutton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/dividerdefault"
/>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/appDefaultSingleBlockBackground"
android:clickable="true"
android:id="#+id/mine_goto_youhuijuan"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_marginLeft="15dp"
style="#style/TextNomal"
android:drawableLeft="#drawable/information_discountbutton"
android:text="#string/youhuijuan"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/venue_maxbutton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/dividerdefault"
/>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/appDefaultSingleBlockBackground"
android:clickable="true"
android:id="#+id/mine_goto_my_rest_money"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_marginLeft="15dp"
style="#style/TextNomal"
android:drawableLeft="#drawable/information_balancebutton"
android:text="#string/myRest"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/venue_maxbutton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/dividerdefault"
/>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/appDefaultSingleBlockBackground"
android:clickable="true"
android:id="#+id/mine_goto_secure"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:drawablePadding="10dp"
android:layout_marginLeft="15dp"
style="#style/TextNomal"
android:drawableLeft="#drawable/information_accountssafebutton"
android:text="#string/secure"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/venue_maxbutton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/dividerdefault"
/>
</LinearLayout>
and this is my java code .
private void init(View ret) {
final TextView txtAvatar = (TextView) ret.findViewById(R.id.mine_nick_and_avatar);
final RelativeLayout gotoPersonal = (RelativeLayout) ret.findViewById(R.id.mine_goto_personal_info_btn);
RelativeLayout gotoInterest = (RelativeLayout) ret.findViewById(R.id.mine_goto_interesting_venue);
RelativeLayout gotoSetting = (RelativeLayout) ret.findViewById(R.id.mine_goto_setting);
RelativeLayout gotoYouhuijuan = (RelativeLayout) ret.findViewById(R.id.mine_goto_youhuijuan);
RelativeLayout gotoMyRestMoney = (RelativeLayout) ret.findViewById(R.id.mine_goto_my_rest_money);
RelativeLayout gotoSecure = (RelativeLayout) ret.findViewById(R.id.mine_goto_secure);
gotoPersonal.setTag("gotoPersonal");
gotoInterest.setTag("gotoInterest");
gotoSetting.setTag("gotoSetting");
gotoYouhuijuan.setTag("gotoYouhuijuan");
gotoMyRestMoney.setTag("gotoMyRestMoney");
gotoSecure.setTag("gotoSecure");
try {
Object o = SharedPreferenceUtils.readInfo(mActivity, ConstData.UserInfo[1]);
if (o != null && !"null".equals(o)) {
txtAvatar.setText((String) o);
} else {
o = SharedPreferenceUtils.readInfo(mActivity, ConstData.UserInfo[10]);
if (o != null && !"null".equals(o))
txtAvatar.setText("KD" + o);
}
} catch (Exception e) {
}
Object o1 = SharedPreferenceUtils.readInfo(mActivity, ConstData.UserInfo[2]);
if (o1 != null)
MyApplication.downloader.download("http://" + o1, new ImageDownloadStateListener() {
#Override
public void loading() {
}
#Override
public void loadSuccess(Bitmap bitmap, String url) {
try {
bitmap = Tools.transforCircleBitmap(bitmap);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
gotoPersonal.measure(0, 0);
int measuredHeight = gotoPersonal.getMeasuredHeight();
LogHelper.print("==height" + measuredHeight);
drawable.setBounds(0, 0, measuredHeight / 5 * 4, measuredHeight / 5 * 4);
txtAvatar.setCompoundDrawables(drawable, null, null, null);
} catch (Exception e) {
//no nothing
}
}
#Override
public void loadFailed() {
}
});
gotoPersonal.setOnClickListener(this);
gotoInterest.setOnClickListener(this);
gotoSetting.setOnClickListener(this);
gotoYouhuijuan.setOnClickListener(this);
gotoMyRestMoney.setOnClickListener(this);
gotoSecure.setOnClickListener(this);
}
#Override
public void onResume() {
initActionBar();
super.onResume();
MobclickAgent.onPageStart(getClassName()); //统计页面
}
#Override
public void onPause() {
super.onPause();
MobclickAgent.onPageEnd(getClassName());
}
private String getClassName() {
String canonicalName = this.getClass().getCanonicalName();
String[] split = canonicalName.split("\\.");
return split[split.length - 1];
}
private void initActionBar() {
MyActivity activity = (MyActivity) mActivity;
activity.setActionBarLeftImg(new ColorDrawable(Color.TRANSPARENT), false);
activity.setActionBarRightImg(new ColorDrawable(Color.TRANSPARENT));
activity.setOnActionBarLeftClickListener(null);
activity.setOnActionBarRightClickListener(null);
activity.setActionBarTitle("个人中心");
}
#Override
public void onClick(View v) {
Object tag = v.getTag();
if (tag != null) {
String str = (String) tag;
if (mActivity == null) {
return;
}
if (!((MyActivity) (mActivity)).isLogin) {
Intent intent = new Intent(mActivity, LoginActivity.class);
startActivity(intent);
return;
}
if (!TextUtils.isEmpty(str))
if ("gotoPersonal".equals(str)) {
Intent intent = new Intent(mActivity, PersonalInfoActivity.class);
startActivity(intent);
} else if ("gotoInterest".equals(str)) {
Intent intent = new Intent(mActivity, VenueListActivity.class);
intent.putExtra("isInterests", true);
startActivity(intent);
} else if ("gotoSetting".equals(str)) {
Intent intent = new Intent(mActivity, SettingActivity.class);
startActivity(intent);
} else if ("gotoYouhuijuan".equals(str)) {
Intent intent = new Intent(mActivity, FavorableActivity.class);
startActivity(intent);
} else if ("gotoSecure".equals(str)) {
Intent intent = new Intent(mActivity, SecureActivity.class);
startActivity(intent);
} else if ("gotoMyRestMoney".equals(str)) {
Intent intent = new Intent(mActivity, MyRestActivity.class);
startActivity(intent);
}
}
}
You need to define the launch mode. There are at least two ways of solving this by either using the manifest file (hint: singleTop) or by using Intent flags (hint: FLAG_ACTIVITY_SINGLE_TOP).
Good luck!
Simpliest approach would be disable control that starts your Activity (button for example) after 1 click, and enable it later with some condition or action. Try it.
You can use this approach.......
((Button)findViewById(R.id.someButton)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button)findViewById(R.id.someButton)).setEnabled(false);
}
});
Happy coding
i am asking you for help. As you see from the picture it is a result that i should have, but, at the moment i have information orinted on my left corner. what i am doing wrog?
MainActivity.java
public class MainActivity extends ListActivity {
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
TextView mTvSDate;
TextView mTvSName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivityForResult(intent, 1);
}
};
ImageButton addBtn = (ImageButton) findViewById(R.id.addBtn);
addBtn.setOnClickListener(listener);
Log.d("Suceess1","Sucess1");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Defining the ArrayAdapter to set items to ListView
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
// Getting reference to TextView tv_sage of the layout file activity_student
// mTvSDate = (TextView) findViewById(R.id.editDate);
// Getting reference to TextView tv_sname of the layout file activity_student
mTvSName = (TextView)findViewById(R.id.editName);
Log.d("Suceess5","Sucess5");
// Fetching data from a parcelable object passed from MainActivity
NoteElement drug = getIntent().getParcelableExtra("drug");
// MyAdapter adapter = new MyAdapter(this, generateData());
Log.d("Suceess6","Sucess6");
list.add(mTvSName.getText().toString());
mTvSName.setText(drug.mSName);
adapter.notifyDataSetChanged();
}
#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;
}
}
SecondElementActivity.java
public class SecondElementActivity extends Activity{
EditText mEtSDate;
EditText mEtSName;
Button btnSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_element);
// Getting a reference to EditText et_sname of the layout activity_main
mEtSName = (EditText)findViewById(R.id.editName);
// Getting a reference to EditText et_sage of the layout activity_main
mEtSDate = (EditText)findViewById(R.id.editDate);
// Getting a reference to Button btn_ok of the layout activity_main
btnSave = (Button)findViewById(R.id.btnSave);
// Setting onClick event listener for the "OK" button
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Creating an instance of NoteElement class with user input data
NoteElement drug = new NoteElement(
mEtSDate.getText().toString(),
mEtSName.getText().toString());
// Creating an intent to open the activity MainActivity
Intent intent = new Intent(getBaseContext(), MainActivity.class);
// Passing data as a parecelable object to MainActivity
intent.putExtra("drug",drug);
// Opening the activity
startActivity(intent);
}
});
}
}
Parcelable class NoteElement.java
public class NoteElement implements Parcelable{
String mSDate;
String mSName;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* Storing the NoteElement data to Parcel object
**/
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mSDate);
dest.writeString(mSName);
}
/**
* A constructor that initializes the NoteElement object
**/
public NoteElement(String sDate, String sName){
this.mSDate = sDate;
this.mSName = sName;
}
/**
* Retrieving NoteElement data from Parcel object
* This constructor is invoked by the method createFromParcel(Parcel source) of
* the object CREATOR
**/
private NoteElement(Parcel in){
this.mSDate = in.readString();
this.mSName = in.readString();
}
public static final Parcelable.Creator<NoteElement> CREATOR = new Parcelable.Creator<NoteElement>() {
#Override
public NoteElement createFromParcel(Parcel source) {
return new NoteElement(source);
}
#Override
public NoteElement[] newArray(int size) {
return new NoteElement[size];
}
};
}
And my activity_main have such a .xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Student version -->
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<TextView
android:id="#+id/tv_sdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<!-- List -->
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/addBtn" />
</RelativeLayout>
activity_second.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" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Date -->
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/textView1"
android:layout_marginRight="36dp"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/editDate"
android:text="#string/date" />
<EditText
android:id="#+id/editDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editName"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="date"
android:hint="#string/str_hnt_date" />
<!-- Name -->
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="#+id/editName"
android:layout_alignLeft="#+id/date"
android:text="#string/name" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/date"
android:ems="10"
android:hint="#string/str_hnt_name" />
<!-- Dosage -->
<TextView
android:id="#+id/dosage"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText3"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/notes"
android:text="#string/dosage" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editName"
android:layout_below="#+id/editName"
android:ems="10"
android:hint="#string/str_hnt_dosage" />
<!-- Notes -->
<TextView
android:id="#+id/notes"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_alignRight="#+id/name"
android:text="#string/notes" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:ems="10"
android:hint="#string/str_hnt_notes"/>
<!-- buttons: Save and Selete-->
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/notes"
android:layout_marginTop="20dp"
android:text="#string/btnSave" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnSave"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="#+id/addBtn"
android:layout_toRightOf="#+id/btnSave"
android:text="#string/btnDelete" />
</RelativeLayout>
Okay so you have two activities total right?
Here's the deal: Look at your image. Activity 1 starts activity 2 right? And you're expecting to get information from your activity 2 back to activity 1, correct? Alright.
First thing to learn:
startActivity(intent);
This method states that you will just initiate an activity but expect nothing back from it. So even if you want to send information back to activity 1 THROUGH activitiy 2 it will not work. Instead you must do this:
startActivityForResult(intent, 1);
The second parameter is an integer that can help you differentiate between different activity calls, it is not important for you right now.
Now, because you say "ForResult" in your method above, in your MainActivity now you must implement this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// here you will work with the code.
// data is your intent data sent from activity 2
// where you say this in your own code:
// Passing data as a parecelable object to MainActivity
// intent.putExtra("drug",drug);
}
Now the last thing to note:
In SecondElementActivity.java where you have this:
// Opening the activity
startActivity(intent);
It is wrong. You know why? because you're saying that you want to start a new activity. But in Android you already have a parent for this activity, which is activity 1. So your activity 1 called 2, when you end activity 2 it will go back to 1. So, replace that line for this:
setResult(RESULT_OK, intent);
finish();
EDIT:
Also I don't know if this is correct, I don't do it this way, so here is my fix:
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivity(intent);
}
};
When you say you want to make a new intent you should pass two parameters:
Intent intent = new Intent(this, SecondElementActivity.class);
The second parameter is the name of the class you want to call.
You can try like this :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/hello_world"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_centerHorizontal="true"
android:text="mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- List -->
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp" />
</RelativeLayout>
Follow This tutorial for populating ListView just change adapter and row.xml from that tutorial
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_sdate "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="5dp"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tv_sdate "
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
http://hmkcode.com/android-custom-listview-items-row/
I have made a small program in which i have used one button and a WebView. WebView visibility is set GONE
and when i press the button 1st time i want to set visibility to visible and when i press the button 2nd time i want the visibility to be GONE. It should do the same thing consecutively. I have tried to make it work using if ..else and with switch .Its strange that if you click the button a lot of times (depending , it can be 3 or 7 or 9 or even more times) the code start to work.
Please help me.
Here is my code:
public class MyMenu extends Activity {
Button info;
WebView webView;
int see=0 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_menu);
info = (Button) findViewById(R.id.info);
webView = (WebView) findViewById(R.id.webView1);
webView.setVisibility(View.GONE);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///android_asset/odigies.html");
// make listener for odigies button
info.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (see == 0) {
webView.setVisibility(View.VISIBLE);
see = 1;
} else {
webView.setVisibility(View.GONE);
see = 0;
}
}
});
}
//send app with sms
public void send(View v){
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.https://play.google.com/store/apps/details?id=o.tzogadoros");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
//send app with email
public void email(View v){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.fromParts("mailto",
"", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Εφαρμογή Lotto Android");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.Δίνει τυχαίους αριθμούς για τα τυχαιρά παιχνίδια. ");
if (emailIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(getApplicationContext(),
"Παρακαλώ παραμετροποίησε τον λογοριασμό email σου", Toast.LENGTH_LONG)
.show();
} else {
// Secondly, use a chooser will gracefully handle 0,1,2+ matching
// activities
startActivity(Intent.createChooser(emailIntent,
"Διάλεξε το email σου"));
}
}
// go to tzoker activity
public void tzoker(final View view) {
startActivity(new Intent(this, Tzoker.class));
}
// go to kino activity
public void kino(final View view) {
startActivity(new Intent(this, Kino.class));
}
// go to lotto activity
public void lotto(final View view) {
startActivity(new Intent(this, MainActivity.class));
}
// go to proto activity
public void proto(final View view) {
startActivity(new Intent(this, Proto.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
}
and the 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="match_parent"
android:background="#eaf39b"
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=".MyMenu" >
<ScrollView
android:id="#+id/vertical_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eaf39b"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eaf39b"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="lotto"
android:src="#drawable/lottoicon" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="tzoker"
android:src="#drawable/tzokericon" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="kino"
android:src="#drawable/kinoicon" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="proto"
android:src="#drawable/protoicon" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menuicon" />
</LinearLayout>
<Button
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="#string/Menuodigies"
android:textSize="22sp"
android:textStyle="bold" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/sendsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="send"
android:text="#string/sms"
android:textSize="22sp"
android:textStyle="bold" />
<Button
android:id="#+id/sendemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="email"
android:text="#string/email"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Finally when the webview appear there is no zoom controls,why?
Thanks for your time.
Is it better with that ?
#Override
public void onClick(View v) {
if (webView.getVisibility==View.GONE) {
webView.setVisibility(View.VISIBLE);
} else {
webView.setVisibility(View.GONE);
}
}
I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)
so if the title sounds confusig, this is what actually happens. In the Activity I have a ImageView and it has a defalut picture. This defalut picture should be shown as long as the user picks another picture from gallery. When he does that, the picture which he picked should be shown as long as he doesn't delete it or picks another one. If he deletes it, the default picture should be shown again until he picks new picture from the gallery.
I have successfuly loaded the picture from the gallery but it stays in the imageview only until the applications restarts. After which the default picture will be shown again. Is there any way to fix this?
This is my code
package com.pumperlgsund.activities;
imports...
public class MainActivity extends FragmentActivity implements
OnHeadlineSelectedListener, View.OnClickListener {
private static int LOAD_IMAGE = 1;
private String selectedImagePath;
private ImageView imgUser;
private TextView txtName;
private TextView txtScore;
private TextView txtValue;
private UserDataController controller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/ComicNeueSansID.ttf");
controller = new UserDataController(this);
imgUser = (ImageView) findViewById(R.id.imgUser);
imgUser.setOnClickListener(this);
txtName = (TextView) findViewById(R.id.txtUserName);
txtName.setTypeface(tf);
txtName.setText(controller.getUserName());
txtScore = (TextView) findViewById(R.id.txtScore);
txtScore.setTypeface(tf);
txtValue = (TextView) findViewById(R.id.txtValue);
txtValue.setTypeface(tf);
txtValue.setText("" + controller.getScore());
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgUser:
onImageClicked(v);
break;
default:
break;
}
}
private void onImageClicked(View view) {
showPopupMenu(view);
}
private void showPopupMenu(View view) {
PopupMenu popupMenu = new PopupMenu(this, view);
popupMenu.getMenuInflater().inflate(R.menu.popupmenu,
popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.item1) {
//TODO: ...
return true;
} else if (item.getItemId() == R.id.item2) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Load Image"), LOAD_IMAGE);
return true;
} else {
return false;
}
}
});
popupMenu.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == LOAD_IMAGE) {
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();
imgUser.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
layout
ImageView has id imgUser, located in RelativLayout user_area_content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/backgroundColor"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<LinearLayout
android:id="#+id/static_area"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/user_area"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/user_area_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="#+id/imgUser"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:src="#drawable/ic_img_user" />
<TextView
android:id="#+id/txtUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="#id/imgUser"
android:text="#string/placeholder"
android:textColor="#color/blue"
android:textSize="16sp" />
<TextView
android:id="#+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtUserName"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="#id/imgUser"
android:text="#string/score"
android:textColor="#color/blue"
android:textSize="16sp" />
<TextView
android:id="#+id/txtValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtUserName"
android:layout_marginLeft="8dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="#id/txtScore"
android:text="#string/placeholder"
android:textColor="#color/blue"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<fragment
android:id="#+id/headlines_fragment"
android:name="com.pumperlgsund.fragments.HeadlinesFragment"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginTop="16dip"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/dynamic_frame"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="16dip"
android:layout_weight="2"
android:orientation="horizontal" />
Thx for help!
The default image is 'shown again' every time the view are (re)built i.e. every time the Activity is created, when the application is started, but also on device rotation. You should save the path to the selected image (in onActivityResult()) and retrieve it in onCreate(). The simplest way to save the path is probably to use a SharedPreferences.
If you want to restrict the selection to local images, add the EXTRA_LOCAL_ONLY extra to the intent.