Starting an activity when tapping on a widget - android

I'm developing an android widget and i would like to start a certain activity when i tap on a certain area of my widget, and another one when tapping somewhere else.
How can i do this ?
Thank you!

Create 2 Linear Layouts in places wherever you want to touch, and let them be blank (No child).
Then add android:clickable = "true" in linear layouts. and now add clicklisteners to thes two layouts and start Activity..
Something like this..
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutId);
layout.setOnClickListener(new OnClickListener(){
protected void onClick(View v){
//start Activity
}
});

Related

Is it possible to open a new layout(.xml) file with button click?

I have a MainActivity.java file with a single layout. I created a new layout that is simply called layout2, and want to call it somehow in the MainActivity.java file, to switch between the 2 layouts with a btn click, but without any .java class file (like in Activities: .java file goes with his own single layout).
For short:
An Activity open (just) a new layout file.
Thanks for any answers.
ImageButton ibtt;
ImageButton ib = (ImageButton)findViewById(R.id.ibtt);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity.setContentView(R.layout.secondlayout);
}
});
Not entirely sure what you are trying to do but a Viewflipper sounds something you might like. Define a viewflipper with as many children as you want in the xml. The children can be separate layouts each. Use the<include> tag. Plenty of examples with viewflipper on StackOverflow. I prefer using a statekeeper along with one always.
EDIT: Here's an example How to use view flipper with three layouts?

Adding editText on button click

I have a layout (activity_main.xml) that has some TextView and EditText elements and a button.
I have some stuff happen when the button is clicked, but I want to add a new EditText element directly below and existing element after the button has been clicked.
How can I make this happen please?
Usually i just play around with the Visibility of the elements. Example:
on OnCreate: EditText1.setVisibility(GONE);
and then on your OnClickListener Implementation:
private OnClickListener onShow = new OnClickListener() {
public void onClick(View v) {
EditText1.setVisibility(VISIBLE);
}
}
This will hide your EditText (or whatever element you want) in the creation of the activity, and then show them again when you pressed the button.
The above mentioned method is easier and it seems suffice enough for most of my projects. However, if we really want to add elements dynamically, there is a way.
We can basically add any element dynamically to our xml layout. But we need an element (container) in our xml layout for holding our added element later. Example, we use an empty LinearLayout with android:id="#+id/container". With this in mind, it means we can build everything dynamically from scratch and setContentView(ourView), where ourView is the root element with other child elements added.
Example:
EditText newElement = new EditText(this);
newElement.setLayoutParams(params);
LinearLayout container = (LinearLayout)findViewById(R.id.container);
container.addView(newElement);
Note: This is just a pseudocode and not a complete code.

How to use whole screen as button in android?

How can i get an onClick response when the user touches any part of the screen?
Place a Button inside the layout manager.
and specify
fill_parent
for width and height.
and remove the border of the button.
this will work.
For Example you have ah RelativeLayout means,
RelativeLayout mLyout=(RelativeLayout)findViewById(R.id.layout01);
relativeclic1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// Here do your Task
}
});
You can cut off the touch events before they are ever dispatched by overriding dispatchTouchEvent:
https://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)
You can get the statistics of the touch -- first, repeated, etc -- from the MotionEvent passed.

how to zoom-in interface? Android development

In my Android application, I have an activity with three layouts: A left layout, a middle layout, and a right layout.
The right layout is the main one. I want the right layout to zoom into fullscreen when I click a button. If you have the specific code,it's better.
Thanks very much!
If I got your question right:
Your button's OnClickListener should look like something like this:
OnClickListener listener = OnClickListener() {
public void onClick(View v){
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
};
This by itself will not get your right layout to fullscreen. You'll need to add something the below to your right layout for it to "auto fullscreen":
android:layout_width = "0dp";
android:layout_weight = "1";
if you don't do that, you'll have to resize the right layout from the "listener" above.
Hmm, the question is vague, but if I understand correctly, what you want to do is hide the left and center layouts when a button is clicked, so that the right layout becomes full-screen?
Without more details, it's not easy to give you a more precise answer (please post your current layout), but I would do something like:
// this code in your Activity:
// this method is bound to button onCLick (android:onClick="clickButton")
public void clickButton(View view) {
findViewById(R.id.left_layout).setVisibility(View.INVISIBLE);
findViewById(R.id.center_layout).setVisibility(View.INVISIBLE);
}
And to revert back to a 3-layout display, you do the opposite (View.VISIBLE)

Change from one layout view to another and going back?

want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.

Categories

Resources