I'm wanting to implement a custom text interface, with touch+drag selecting text and the keyboard not being raised, on the image view where user can type his own text and save it on the image view.can anyone help please?
here is my full image activity class
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
here is my xml file
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/fullimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="yourtext" />
</FrameLayout>
You could override ImageView and add a setText Method.
The text you set in that Method can be added to the View by adding it to the onDrawMethod of the View.
By catching the onTouchEvent on the CustomImageView you can move your text on the image. But you would have to have two activities. Activity a where you select the Image you want to show/edit and type the text, after you have done that, you can move to the next acitivity and show the text on the selected image.
http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/ - this should show you how to draw text to the Canvas
and this http://www.vogella.com/articles/AndroidTouch/article.html should get you started concerning touch events.
Need anything else?
Related
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);
OK I've searched and seen similar issues, tried them and no avail. I have a listView with some elements and I want to click on one element and display a detail somewhere else.
This is my listView
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedidos);
ListView listView = (ListView) findViewById(R.id.actualStoresList);
Model.initialize();
Vector<String> values = Model.stores;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
Object o = adapter.getItemAtPosition(position);
String str_text = o.toString();
Log.i("", "I have selected this: " + str_text);
}
});
CustomStringAdapter adapter = new CustomStringAdapter(this, R.layout.my_list_layout, R.id.list_content, values);
listView.setAdapter(adapter);
}
This is the my_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
The list gets displayed correctly. When I click on an element of the list (A textView) it "steals" the click event so nothing happens (the onItemClickListener is attached to the listView, not the TextView).
The textView has an small margin where, if careful, I can click just behind it, in fact, touching the listView. In this case, the event gets fired ok and I see the log.
I've tried to set the TextView android:focusable="false" but still, the TextView is "above" of the listView and always gets the click events.
How can I either make the TextView "transparent" so it actually clicks on the listView, or add a onclickListener to the TextView so I can handle its events?
Thanks!
Alejandro
Setting clickable property of TextView to false should solve this problem. Try this:
<TextView
android:id="#+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:clickable="false" />
To make items not focusable, do this:
listView.setItemsCanFocus(false);
source
I think this will solve your issue.
Also make sure the
android:textIsSelectable
property is not set to true.
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 created an xml page that holds 2 textviews and a seekbar all without ids.
The class CustomSeekBar creates these objects using the xml page as a basic structure.
You can see space for the textviews on my emulator, but I am having a hard time figuring out to set the text. Obviously I am missing something, because there is no way for the CustomSeekBar class to be able to tell which textview I want to set the text for.
How do I set the text of each individual view without giving each textview a hardcoded ID?
The reason I say without a hardcoded ID, is because if each textview is named, then when one textview's text needs to be changed, won't all the textview's texts, with that ID, change?
How would I call the specific textview ID since my customseekbar class is in a composite relationship with the activity?
Activity that calls everything.
public class ColorsActivity extends ListActivity {
/** Called when the activity is first created. */
//Array Adapter that will hold our ArrayList and display the items on the ListView
SeekBarAdaptor seekBarAdaptor;
//List that will host our items and allow us to modify that array adapter
ArrayList<CustomSeekBar> seekBarArrayList=null;
// TextView myValueText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbarlist);
//Initialize ListView
ListView lstTest= getListView();
//Initialize our ArrayList
seekBarArrayList = new ArrayList<CustomSeekBar>();
//Initialize our array adapter
seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars, seekBarArrayList);
CustomSeekBar red = new CustomSeekBar(this, "red", 1);
//CustomSeekBar blue = new CustomSeekBar(this, "blue");
//CustomSeekBar green = new CustomSeekBar(this, "green");
//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(seekBarAdaptor);
seekBarArrayList.add(red);
//seekBarArrayList.add(blue);
//seekBarArrayList.add(green);
Amarino.connect(this, "00:11:11:21:05:53");
}
}
CustomSeekBar class
public class CustomSeekBar implements SeekBar.OnSeekBarChangeListener {
Context myContext;
TextView myValue;
TextView myLabel;
SeekBar mySeekBar;
CustomSeekBar(Context context, String label, int ID){
myContext = context;
myValue = new TextView(myContext);
mySeekBar = new SeekBar(myContext);
myValue.setText(label);
mySeekBar.setOnSeekBarChangeListener(this);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
myValue.setText(progress);
Amarino.sendDataToArduino(myContext, "00:11:11:21:05:53", 'A', progress);
}
public void onStopTrackingTouch(SeekBar seekBar){
}
public void onStartTrackingTouch (SeekBar seekBar){
}
}
seekbarlist.xml holds my list view for the custom list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
seekbars.xml is the structure of each custom list item (CustomSeekBar)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBarLayout">
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="255"/>
</LinearLayout>
why would you have to name them the same ID name why not
#+id/textview1 and #+id/textview2 and then reference the two text boxes in your code I don't understand what is stopping you?
You can use IDs as #bmporter12 said. You can have duplicate IDs, provided that Android has a place to start looking from when you tell it to findViewById. So, in your adapter, in getView(), you would inflate your new row from seekbars.xml and then do row.findViewById(R.id.textView1) and row.findViewById(R.id.textView2).
If you need to set it from outside the adapter, then depending on where you're getting the signal to set a TextView, either your CustomSeekBar could ask the Activity for its entry at a particular position in the adapter or it could use the View parameter passed in an onClick callback.
So far I have an Activity where there are a selection of thumbnails, once a thumbnail is clicked it opens up the Camera Activity with the SurfaceView which is being set in the XML below:
I need a way to be able to change the SurafceView depending on which thumbail was choosen in the previous Activity I have the thumbnails set up as buttons. I have given each button the same ID as the image name it should use on the SurfaceView so is there a way to take the button ID and change the RelativeLayout background below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/overlay"
android:gravity="bottom"
android:orientation="vertical" >
<Button
android:id="#+id/takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="10px"
android:layout_marginBottom="10dp"
android:background="#drawable/capture" />
</RelativeLayout>
Java:
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
When starting up the Camera activity, put the ID of the background you want into the intent's extras bundle like so:
intent.putExtra("backgroundId", backgroundId);
startActivity(intent);
In the second activity you retrieve the background ID from your intent and assign it to the background of the root view:
#Override
public void onCreate(Bundle bundle) {
...
int backgroundId = getIntent().getIntExtra("backgroundId", 0);
viewControl.setBackgroundResouce(backgroundId);
...
}