I want to create some sort of logging procedure in my Android app.
I have managed to detect where user is pressing on the screen (three times) and to create logging sequence from that.
On the end, I want to show 3 images which represent selected logging sequence (from my drawable folder)
I have dynamically set drawable ID and it works when I place image on main layout.
But if I place image inside custom dialog, I get force close.
From LogCat I see following: java.lang.NullPointerException at image1.setImageResource(iIdSlike);
If I show drawable ID as a text it's OK and ID is the same on main and custom dialog layout.
I get force close even if I set image like this (not dynamically):
image1.setImageResource(R.drawable.s11);
Why I can't show image on custom dialog?
This is my dijalog.xml (custom dialog layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imgDrugi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/TextView01"
android:layout_below="#+id/TextView01"
android:layout_marginRight="27dp"
android:layout_marginTop="51dp"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:text="#+id/TextView01" />
</RelativeLayout>
And this is my code:
public class MyWorkLogiranje extends Activity implements OnClickListener
{
#Override
public boolean onTouchEvent(MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
.
.
.
//calculations to get string sPrvi
sPrvi="s31"; //For testing purposes sPrvi set manually
//Image and text on main layout (it works)
iIdSlike = getResources().getIdentifier(sPrvi, "drawable", getPackageName());
ImageView image = (ImageView) findViewById(R.id.imgPrvi);
image.setImageResource(iIdSlike);
TextView text1 = (TextView)findViewById(R.id.txtPrvi);
text1.setText("iIdSlike: " + iIdSlike);
//Prikaz dijaloga
final Dialog dialog = new Dialog(MyWorkLogiranje.this);
dialog.setContentView(R.layout.dijalog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
dialog.getContext();
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("iIdSlike: " + iIdSlike);
ImageView image1 = (ImageView) findViewById(R.id.imgDrugi);
image1.setImageResource(R.drawable.iIdSlike);
//image1.setImageResource(R.drawable.s11);
//Podešavanje dugmeta
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
Can anyone help me with this please?
I'm scratching my head for two days about this.
Thank you.
Your findViewById is failing. So you are getting NullPointerException.
ImageView image1 = (ImageView) findViewById(R.id.imgDrugi);
should be
ImageView image1 = (ImageView) dialog.findViewById(R.id.imgDrugi);
Try this:
image1.setImageDrawable(getResources().getDrawable(iIdSlike));
Before this add this in your dialog code:
ImageView image1 = (ImageView) dialog.findViewById(R.id.imgDrugi);
You can use this way:
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
or
setImageDrawable(getResources().getDrawable(R.drawable.icon));
or
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
imageview.setImageResource(id);
Related
What I'm trying to do is when an Imageview item being preesed, then a Dialog will open up and show the inage in a large scale.
Now first of all this is the XML layout of the dialog -
<?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" >
<ImageView
android:id="#+id/imageView_large_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bkgnd_pressed" />
</LinearLayout>
Now the code to the Dialog is as follows -
Dialog settingsDialog = new Dialog(MainChat.this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_large
, null));
ImageView ivLarge = (ImageView) findViewById(R.id.imageView_large_pic);
String pathpic = v.getTag().toString();
ivLarge.setImageURI(Uri.parse(pathpic));
settingsDialog.show();
Now I tried to dubuging this code, and it get stuck at this line - ivLarge.setImageURI(Uri.parse(pathpic));
The logcat gives me that this is java.lang.NullPointerException error.
Thanks for any kind of help
You are looking in the wrong place for the ImageView
ImageView ivLarge = (ImageView) findViewById(R.id.imageView_large_pic);
is looking in the layout you inflate in setContentView(). Instead, you need to look in the Dialog's layout. Try instead
ImageView ivLarge = (ImageView) settingsDialog .findViewById(R.id.imageView_large_pic);
I have a thumbnail image and when user clicks on that image,
it will popup (a window or dialog??) to show the big image.
The background should be transparent for the (window or dialog??).
Is there any tutorial??
yes you can set full screen dialog to show image like below
final Dialog nagDialog = new Dialog(backgroundsActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
ivPreview.setBackgroundDrawable(dd);
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
where preview_image is xml contains only ImageView and close button.
Here the xml used for showing the image full screen
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/close"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
use a dialog with background color transparent, no-title theme and custom layout having just one ImageView
FINAL EDIT: Okay well a total hack but at the moment I'm like "meh" it works. All I did to fix the issue was add android:lines="10" to the TextView and it showed everything like in 2.2 and ICS/JB. Total hack because it's not abstract at all but whatever :P..Thanks to everyone who helped!
I'm having trouble displaying text in a custom dialog with Gingerbread (API 10). The only text shown is the first line as shown here. In Froyo, ICS, and JB it displays with every line of text shown. I believe it's an XML thing, but I'm not sure. Where am I going wrong?
Edit: What I have tried:
-Changing the RelativeLayout to LinearLayout
-Adding in ScrollView
-Putting my string on one line
-using requestLayout() and forceLayout()
-putting the dialog functions in a separate class
-taking out the margins in my button
-using \n instead of HTML
-AlertDialog
-inputType and singleLine XML attributes on my TextView
-I think there's one or two more that I forget..
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:padding="5dp"/>
<View
android:id="#+id/bar"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#CCCCD0"
android:layout_below="#+id/dia_text"/>
<Button
android:id="#+id/dialogbuttongotit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dialog_confirm"
android:textSize="20dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/bar"/>
</RelativeLayout>
Here is the Code:
final Context context = this;
public void addListenerOnRectHelpButton() {
img = (ImageButton) findViewById(R.id.rect_img);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//create a new dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialogs);
dialog.setTitle("Rectangular Area");
// set the custom dialog text
TextView text = (TextView) dialog.findViewById(R.id.dia_text);
String dialog_rect_txt = "<u>Area of a Rectangular Channel</u><br />" +
"Height x Width (H x W)<br />--Example:<br />" +
"Height: 3ft, Width 5ft<br />" +
"H x W = 3ft x 5ft = 15ft<sup>2</sup><br />";
text.setText(Html.fromHtml(dialog_rect_txt));
Button dialogButton = (Button) dialog.findViewById(R.id.dialogbuttongotit);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
Having you tried calling requestLayout() on the TextView?
...
text.setText(Html.fromHtml(dialog_rect_txt));
text.requestLayout();
...
Why not just use an AlertDialog instead of building your own?
Dialog dlg = new AlertDialog.Builder(this).setTitle(
"Rectangular Area").setMessage(dialogRectText).setPositiveButton(
"Got It", clickListener).create();
dlg.show();
I have an application with an input text where the users have to insert an information and a button "+" beside to input text.
I would like to make my form dynamic in a way that when a user pushes on "+" button appears dynamically another text input and another "+" button beside this one, the process is repeated in the same way.
I created and xml file, sample_content:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/attempt"
android:layout_alignParentBottom="true"
android:text="TextView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="36dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:text="+" />
<EditText
android:layout_width="229dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/addKey"
android:background="#drawable/inputtext_corner"
android:ems="10"
android:textSize="18sp" >
<requestFocus />
</EditText>
</RelativeLayout>
and in my Activity, AddDeviceActivity I put:
inflater = LayoutInflater.from(AddDeviceActivity.this);
Button addKey = (Button) findViewById(R.id.addKey);
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});
But this solution doesn't work because when I add the first input text and the first button, I don't know how to make the second button work in my AddDeviceActivity dynamicly
Just wondering whether you can do this:
Have your activity implement OnClickListener and add this method to your activity:
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
canvas.addView(childView);
((Button)childView.findViewById(R.id.addKey)).setOnClickListener(AddDeviceActivity.this);
}
And then change your initial code to use
addKey.setOnClickListener(this);
instead of an anonymous inner class.
I haven't tested this, but don't see why it wouldn't work.
check out this, pass null instead of canvas object in inflate() method
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, null, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});
I have a transparent button image with white border and without label. I want to create a button which will have its label, its background transparent image and want to set a background color to button dynamically.
The transparent button image:
The Button should look like this after setting its button image with label and background color :
I have tried to implement this by using FrameLayout, TextView and Button. As in framelayout the first child will be textview and second will be button. Setting a transparent btn image to button and textview will have label and background color which has to set dynamically. I am almost able to do this but textview size should be little smaller than button image, this thing i have to calculate dynamically. currently textview background color sometimes goes outside the button and also round shape is not coming.
The xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dip" >
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="#FFFFFFFF"
android:id="#+id/btn_bacground"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"
android:layout_marginRight="1dip"
android:layout_marginLeft="0dip" />
<Button
android:id="#+id/button_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/btn_transparent_img"/>
</FrameLayout>
Edit:
Setting the background color, size and label to Textview. background image to Button.
buttonClick = (Button) buttonClickFrame.findViewById(R.id.button_img);
buttonBg = (TextView) buttonClickFrame.findViewById(R.id.btn_bacground);
buttonBg.setBackgroundColor(Color.parseColor(BG_COLOR));
buttonBg.setText("Click");
buttonPhone.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_transparent_img));
setBackgroundDimentions(buttonClick, buttonBg);
buttonClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onPhoneClick(v);
}
});
private void setBackgroundDimentions(Button btn, TextView bckView) {
final Button mBtn = btw;
final TextView mBckView = bckView;
ViewTreeObserver vto = mBtn.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int finalHeight = mBtn.getMeasuredHeight();
int finalWidth = mBtn.getMeasuredWidth();
mBckView.setWidth(finalWidth - 14);
mBckView.setHeight(finalHeight - 15);
return true;
}
});
}
Please suggest me how can i achieve this.
why don't you use Image Button.!
like this:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_transparent_img"
android:background="#000000"
/>
use imageButton
add your transparent image as src
change background colour according to your need
Refer this link for further help: