OnTouchListener on Activity never calls - android

I used this code, but when i click on activity at runtime, it never hits in OnTouch() method. Can someone guide me what i am doing wrong? Should i need to setcontentview of this activity? Actually i want the coordinates of activity where user touch during execution.
public class TouchTestAppActivity extends Activity implements OnTouchListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.touch);
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
// TODO Auto-generated method stub
String test = "hello";
}
}

UPDATE:
You need to do this :
In your XML layout file, you need an ID for the root view: android:id="#+id/myView"
In youer onCreate() method, write this:
LinearView v= (LinearView) findViewById(R.id.myView);
v.setOnTouchListener(this);
Assuming that your root view is a LinearView

You should the onTouchListener to relevant GUI components.
For example:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.touch);
TextView someView = (TextView)findViewById(R.id.some_view_from_layout_xml);
someView.setOnTouchListener(this); // "this" is the activity which is also OnTouchListener
}

You have to add a the listener using setOnTouchListener() otherwise who will give call to your onTouch method. any listener works on any view. so you have to add the listener to the view eg button.setOnTouchListener(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.yourxml, null);
setCOntentView(vi);
vi.setOnTouchListener(this);

In onCreate, you need to set the content view (just uncommenting the second line should probably work) and then you need to set your OnTouchListener (your activity) as the onTouchListener for a view in your application.
Let's say you've got a view in your layout called "MainView"; it would look something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.MainView);
view.setOnTouchListener(this);
}
This article has a good example:
http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-2-building-the-touch-example/1763

Related

Dynamically Add and Remove Views from Layout

I'm working on one demo project in that I had create one XML file containing some views like ImageView, EditText. I'm loading this XML file on FrameLayout at runtime. At one point I want to remove all that views and again want to display them, I used removeView() method on button click but it does not work for me,,Please tell me the right way to do it..
public class Demo extends Fragment implements OnClickListener, OnTouchListener{
//Declaration of framelayout
FrameLayout f;
//Declaration of imageview
ImageView imageview;
View view, framelayoutview;
File file;
EditText etcardname, EditTextUserName,EditTextUsesrMobNumber,EditTextUsesrEmailID,EditTextUsesrAddress;
TextView dialogtesting;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_modify_card ,container, false);
framelayoutview = inflater.inflate(R.layout.frame_layout_data ,container, false);
captureImageInitialization();
Initialize();
return view;
}
// Initialization of all views components
private void Initialize() {
f=(FrameLayout)view.findViewById(R.id.framelayout);
Button btneditcardreset=(Button)view.findViewById(R.id.buttonresetcard);
btneditcardreset.setOnClickListener(this);
Bundle bundle = this.getArguments();
int myInt;
myInt = bundle.getInt("position");
imageview=(ImageView)framelayoutview.findViewById(R.id.imageViewicon);
EditTextUserName=(EditText)framelayoutview.findViewById(R.id.modifycardeditTextusername);
EditTextUsesrMobNumber=(EditText)framelayoutview.findViewById(R.id.editTextmobilesnumber);
EditTextUsesrEmailID=(EditText)framelayoutview.findViewById(R.id.editTextemailid);
EditTextUsesrAddress=(EditText)framelayoutview.findViewById(R.id.editTextaddress);
imageview.setOnTouchListener(this);
EditTextUserName.setOnTouchListener(this);
EditTextUsesrMobNumber.setOnTouchListener(this);
EditTextUsesrEmailID.setOnTouchListener(this);
EditTextUsesrAddress.setOnTouchListener(this);
f.addView(framelayoutview);
}
#Override
public void onClick(View v){
if(v.getId()==R.id.buttonresetcard){
if(framelayoutview.getParent()!=null){
f.removeAllViews();
}
f.addView(framelayoutview);
}
}
Sorry to all I forgot to tell that I provided OnTouchListener so that I can move the views anywhere in Layout. So when I press reset button all Views should get move back to their original location means where they were at on first load..
you can use
imageview.setVisibility(View.GONE);
And
imageview.setVisibility(View.VISIBLE);
on particular event
If you had added your views dynamically to your FrameLayout then you can remove them.
Otherwise, if you showing your views from XML which are exist inside the frameLayout XML then you can't remove them but you can hide them from showing by setting setVisibility(View.GONE) or setVisibility(View.INVISIBLE).
Try this-
#Override
public void onClick(View v){
if(v.getId()==R.id.buttonresetcard){
if(framelayoutview.getParent()!=null){
f.setVisibility(View.GONE);
}
f.addView(framelayoutview);
}
}

How to inflate a costum-view on a view

I got an custom class. Which works great.
public class FocusGameView extends SurfaceView implements Runnable
At the activity itself I want to put the 'FocusGameView' on a view that I already created on the xml file.
so I tried to use the 'inflate' like this:
public class FocusGame extends Activity {
FocusGameView fgv;
View v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fgv= new FocusGameView(this);
v=(View) findViewById(R.id.frame_focus_game);
LayoutInflater mInflater;
mInflater = LayoutInflater.from(fgv.getContext());
v = mInflater.inflate(R.layout.activity_focus_game, null);
setContentView(R.layout.activity_focus_game);
}
The result of this code is opening the activity and set the layout. without put the custom view on the view itself.
I really hope you could help me with that.
Thanks in advance;
Yaniv.
You can only add a view to a view group, not a view, so lets image your View v is a RelativeLayout:
public class FocusGame extends Activity {
FocusGameView fgv;
RelativeLayout v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focus_game);
v=(View)findViewById(R.id.frame_focus_game);
fgv= new FocusGameView(this);
v.addView(fgv); //You only need to add the view to a parent to make it appear
}

How to change frame layout background and text in other method, which we have added in ocreate() method of the same activity class?

/**How to change frame layout background and text in other method onclick listener, which we have added in ocreate() method of the same activity class? */
public class MainActivity extends Acitvity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mainView); **//Main view**
final FrameLayout rootLayout = (FrameLayout)findViewById (R.id.mainViewRootLayout);
FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.mainribbonLayout);
TextView ribbonText = (TextView)findViewById(R.id.mainribbonLayoutText);
ribbonText.setText("MainRibbonViewBeforeClick");
LayoutInflater inflater = (LayoutInflater)getSystemService (context.Layout_Inflater_service);
addRow(rootLayout);
}
}
/*Here is the calling method in same class which adds one more text view, if we click on text view the ribbon text of oncreate method has to be changed*/
public void addRow(FrameLayout rootLayout)
{
Rect rect = new Rect();
rootLayout.getDrawingRect(rect);
FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.subRibbonLayout);
TextView subText = (TextView)findViewById(R.id.subribbonLayoutText);
subText.setText("ClicktoChangeTheRibbon");
subText.setOnclickListener(new onClickListener() **//Setting up the listener
{
public vid onclick(View v){
FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.mainribbonLayout);
TextView ribbonText = (TextView)findViewById(R.id.mainribbonLayoutText);
ribbonText.setText("SubRibbonAfterClick");
/*Here I need to implement a code that changes the ribbon text which I have added in onCreate method*/
}
});
You should create
FrameLayout ribbonLayout
TextView ribbonText
above the public void onCreate(Bundle savedInstanceState) line.
It will create variable lifetime for whole activity. SO in activity anywhere you will able to access it.
Currently your created objects are available only for onCreate method.

Which lifecycle method will be invoked when Activity is showing?

Sometimes I need to do some operations(e.g. changing layout) when the activity is just showing. What I do now is using post():
public class MyActivity extends Activity {
#Override
public void onCreate() {
...
container.post(new Runnable(){
resize(container);
});
}
}
Is there any lifecycle method like onCreate can be used to simplify the code, that I don't need to call post?
#Override
public void onX() {
resize(container);
}
I think you mean do something after the UI is displayed.
Using a global layout listener has always worked well for me. It has the advantage of being able to remeasure things if the layout is changed, e.g. if something is set to View.GONE or child views are added/removed.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
// set a global layout listener which will be called when the layout pass is completed and the view is drawn
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// at this point, the UI is fully displayed
}
}
);
setContentView(mainLayout);
http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html

creating a xml layout from a custom layout. is it possible?

MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle(R.string.app_name);
setContentView(new SampleView(this));
}
}
SampleView.java:
public class SampleView extends View {
#Override
protected void onDraw(Canvas canvas) {
if (certaincondition = true) {
//add elements to canvas etc
} else {
//How do I do the below? The layout is defined in xml.
//I do not want to use Intent. Please help me
//create a layout from resource R.layout.idAbout and transfer control.
}
}
}
Use a layout inflater:
View newRootViewElement;
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRootViewElement= li.inflate(R.layout.idAbout, null);
You can inflate a layout using
View.inflate(getContext(), R.layout.idAbout, viewParent);
where viewParent is a ViewParent that will be the parent of the inflated view (and can be null).
But what are you trying to do? It's more than a little odd to start a new activity or to modify the view hierarchy from within onDraw(). You might want to post a runnable to a Handler that will do what you want on the next cycle of the event loop. To start a new activity (such as displaying “About” info for the app) you should take a look at the Intent class.

Categories

Resources