How to configure a image pop up for android using eclipce? - android

I'm working with a list of ImageViews, while I would like to each one be clicked on, an image appear as a pop up.
Currently got a good result, but a lot of work, I created a new xml file for each image, so using onclicklistener, every time I click on ImageView, open another layout xml, but that I would like just open a pop up image.
Can anyone help me?
Here is may xml:
res/layout/main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="353dp"
android:layout_marginTop="150dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup2" />
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup3" />
<Button
android:id="#+id/button4"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup4" />
<Button
android:id="#+id/button5"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="#string/popup5" />
</LinearLayout>
</ScrollView>
</FrameLayout>
res/layout/aperitivos
<ImageView
android:id="#+id/ivcarnes"
android:layout_width="364dp"
android:layout_height="410dp"
android:layout_gravity="center"
android:contentDescription="#string/panecestino"
android:src="#drawable/panecestinoim" />
<Button
android:id="#+id/btclose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="85dp"
android:layout_marginTop="150dp"
android:text="#string/close"
android:textStyle="bold"/>
</FrameLayout>
Here is my code:
Main.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class Main extends Activity {
Button button1, button2, button3, button4, button5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)
findViewById(R.id.button1);
button2 = (Button)
findViewById(R.id.button2);
button3 = (Button)
findViewById(R.id.button3);
button4 = (Button)
findViewById(R.id.button4);
button5 = (Button)
findViewById(R.id.button5);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Arg0) {
Intent testegrafico = new
Intent(Main.this,Aperitivos.class);
Main.this.startActivity(testegrafico);
Main.this.finish();
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
Aperitivos.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class Aperitivos extends Activity {
Button btclose;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aperitivos);
btclose = (Button)
findViewById(R.id.btclose);
btclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Arg0) {
Intent testegrafico = new
Intent(Aperitivos.this,Main.class);
Aperitivos.this.startActivity(testegrafico);
Aperitivos.this.finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

package br.com.example.locandaristorante;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
public class Main extends Activity {
Button button1, button2, button3, button4, button5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button)
findViewById(R.id.button1);
button2 = (Button)
findViewById(R.id.button2);
button3 = (Button)
findViewById(R.id.button3);
button4 = (Button)
findViewById(R.id.button4);
button5 = (Button)
findViewById(R.id.button5);
button1.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
View popupView = new View(Main.this);
final PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Drawable drawable = getResources().getDrawable(R.drawable.panecestinoim);
popupWindow.setBackgroundDrawable(drawable);
popupWindow.setHeight(600);
popupWindow.setWidth(400);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
popupWindow.dismiss();
return false;
}
});
popupWindow.showAtLocation(findViewById(R.id.button1), Gravity.CENTER, 0, 0);
}
});
}}

Related

Button onClick, Cycle through Colors/Backgrounds

I am using Android Studio
I'm wanting to change the color of my button, with each click.
To cycle it through around 10 colors and start over again in a continuous loop.
For example I'm using setBackgroundResource(#drawable/oval)
oval = blue circle button
oval2 = red circle button
oval3 = green circle button and so on.
So far I have it that button1 starts as oval(blue) and onClick turns into oval2(red)
So my question is, how to add another click to change it to oval3(green) and then cycle it back to the start oval(blue)?
MainActivity.java
package com.example.shadowz.buttononclick;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
// Code Break
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button1) {
button1.setBackgroundResource(R.drawable.oval2);
}
}
});
// Code Break
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button2) {
button2.setBackgroundResource(R.drawable.oval3);
}
}
});
// Code Break
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button3) {
button3.setBackgroundResource(R.drawable.oval4);
}
}
});
// Code Break
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button4) {
button4.setBackgroundResource(R.drawable.oval5);
}
}
});
// Code Break
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == button5) {
button5.setBackgroundResource(R.drawable.oval6);
}
}
});
}
}
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="match_parent"
android:layout_height="match_parent"
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="com.example.shadowz.buttononclick.MainActivity"
android:id="#+id/backgroundLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="#+id/button1"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button1"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button2"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button2"
android:background="#drawable/oval"
android:layout_alignTop="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="#+id/button3"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button3"
android:background="#drawable/oval"
android:singleLine="false"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button4"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button4"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:id="#+id/button5"
android:layout_width="140dp"
android:layout_height="140dp"
android:text="button5"
android:background="#drawable/oval"
android:padding="#dimen/abc_action_bar_content_inset_material"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="78dp" />
</RelativeLayout>
You can do this by following:
Create integer-array in resources:
<integer-array name="backgrounds">
<item>#drawable/oval1</item>
<item>#drawable/oval2</item>
<item>#drawable/oval3</item>
<item>#drawable/oval4</item>
<item>#drawable/oval5</item>
</integer-array>
Then create special OnClickListener class inside your class code
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
And then set that listener to each button:
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
This should result in the following MainActivity code:
package com.example.shadowz.buttononclick;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.TintableBackgroundView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button colorChangeButton;
private TextView basicText;
private RelativeLayout background;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Drawable oval1;
Drawable oval2;
Drawable oval3;
Drawable oval4;
Drawable oval5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout) findViewById(R.id.backgroundLayout);
basicText = (TextView) findViewById(R.id.button1);
colorChangeButton = (Button) findViewById(R.id.button1);
// Code Break
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button1.setOnClickListener(new MyClickListener(this));
button2.setOnClickListener(new MyClickListener(this));
button3.setOnClickListener(new MyClickListener(this));
button4.setOnClickListener(new MyClickListener(this));
button5.setOnClickListener(new MyClickListener(this));
}
private static class MyClickListener implements View.OnClickListener {
private int mBackgroundIndex = 0;
private final TypedArray mBackgrounds;
public MyClickListener(Context context) {
mBackgrounds = context.getResources().obtainTypedArray(R.array.backgrounds);
}
#Override
public void onClick(View v) {
mBackgroundIndex++;
if (mBackgroundIndex >= mBackgrounds.length()) {
mBackgroundIndex = 0;
}
v.setBackgroundResource(mBackgrounds.getResourceId(mBackgroundIndex, 0));
}
#Override
protected void finalize() throws Throwable {
mBackgrounds.recycle();
super.finalize();
}
}
}

How to add multiple addListenerOnButton in android studio

This is Activity_main.xml for an android app
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sanzuup150"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sanzuuq150"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sanzuur150"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I'm very new to android developing so I dont know how to create addListenerOnButton for imageButton2 and imageButton3 by different url. Please help me......
Ths is MainActivity.java
package com.android.screation;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sanzuu.blogspot.com"));
startActivity(browserIntent);
}
});
}
}
A listener should be put on each imageButton.
Each imageButton is an item in the xml then should be (let us say) linked to in the java using the id. Therefore the ids in the same xml should be unique. Always link the element to its reference in xml before using or else the code will null pointer exception.
package com.android.screation;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
ImageButton imageButton2;
ImageButton imageButton3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton1); // refer to the id in the xml
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
//add the listener to the specific button
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "This is button 1",
Toast.LENGTH_LONG).show();
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sanzuu.blogspot.com"));
startActivity(browserIntent);
}
});
imageButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//what imageButton2 is supposed to do
Toast.makeText(getApplicationContext(), "This is button 2",Toast.LENGTH_LONG).show();
}
});
imageButton3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// what imageButton3 is supposed to do here
Toast.makeText(getApplicationContext(), "This is button 3",Toast.LENGTH_LONG).show();
}
});
}
}
If you need any more help please feel free to ask.

Eclipse Android Checkbox

I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Resource cannot be resolved or is not a field?

I copy pasted everything from a tutorial, 3 of them saved, but weird thing happened.
It said my Chronometer and button cannot be resolved or is not a field.
StopWatch.java
package com.example.stopwatch2;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class StopWatch extends Activity {
Chronometer mChronometer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);Button button;
mChronometer = (Chronometer) findViewById(R.id.chronometer);
// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_stop_watch, menu);
return true;
}
}
activity_stop_watch.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Chronometer android:id="#+id/chronometer"
android:format="#string/chronometer_initial_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="30dip"
android:paddingTop="30dip"
/>
<Button android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start">
<requestFocus />
</Button>
<Button android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop">
</Button>
<Button android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset">
</Button>
</LinearLayout>
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="hello">Hello World, StopWatch!</string>
<string name="app_name">StopWatch</string>
<string name="chronometer_initial_format">Initial format:<xliff:g id="initial-format">%</xliff:g></string>
</resources>
Can anyone tell me what is wrong here?
Try cleaning your project. Right click on your project and say 'clean'. Then try running your app again.
Also, another tip. Don't use the same variable names for all the buttons. Use different names like this:
// Watch for button clicks.
buttonStart = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
buttonStop = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
buttonReset = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
Your code will be easier to manage and debug in the long run.

Submit button access denied cannot get any result

I have a problem with my android project. I cannot get access to my button. I have attach my class code==http://pastebin.com/A5ZTBkhd. Please anyone help me.
Here is my code -
package com.droid.androiddoctor;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidDoctorMainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_android_doctor_main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnSubmit:
String e="hello";
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
break;
}
}
}
.Here is my code.when i push submit nothing happens. and the xml file is====
<?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" >
<Button
android:id="#+id/btnSubmit"
androi:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit" />
</LinearLayout>
You did not set the listener for your widgets at all. Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
button1.setOnClickListener(this)
}
I would suggest you to go through Android's UI Guide.
I wouldn't implement OnClickListener like that, try to add it like this in your class:
public class AndroidDoctorMainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
Button button2 = (Button) findViewById(R.id.btnExit);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for click on this button
}});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for this button
//or if you just want to exit from activity, just call:
finish();
}});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
}
}

Categories

Resources