I'm new to Android and wonder how could I show a user a simple message after LongClick in one of the following buttons. The idea is to show the user the password he is using on each of the sites after a LongClick.
Any ideas?
public class MainMenu extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
setTitle("Main Menu");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void openWeb1(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ljmu.ac.uk/")));
}
public void openWeb2(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.bbc.co.uk")));
}
public void openWeb3(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com")));
}
public void openWeb4(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.adidas.com")));
}
public void openWeb5(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
}
public void addNew(View v){
}
}
The XML code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/GridView1"
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="net.androidbootcamp.myunimanager.MainMenu" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android1:id="#+id/btnLJMU"
android1:layout_width="290dp"
android1:layout_height="76dp"
android1:drawableRight="#drawable/profile"
android1:onClick="openWeb1"
android1:text="Liverpool JMU" />
<Button
android1:id="#+id/btnBBC"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="#drawable/contacts"
android1:onClick="openWeb2"
android1:text="BBC News" />
<Button
android1:id="#+id/btnFACEBOOK"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="#drawable/courses"
android1:onClick="openWeb3"
android1:text="facebook" />
<Button
android:id="#+id/btnADIDAS"
android:layout_width="112dp"
android:layout_height="0dp"
android:drawableRight="#drawable/calendar"
android1:onClick="openWeb4"
android1:text="adidas" />
<Button
android1:id="#+id/btnSTACKOVERFLOW"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="#drawable/notes"
android1:onClick="openWeb5"
android1:text="StackOverFlow" />
<Button
android1:id="#+id/btnADDNEW"
android1:layout_width="match_parent"
android1:layout_height="78dp"
android1:onClick="addNew"
android1:drawableRight="#drawable/myuni"
android1:text="Add New" />
</LinearLayout>
</ScrollView>*
Have a LongClickListener for the button. Inside of it, you can execute the corresponding action by showing a pop up.
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Check the following to learn about how to create pop up messages.
Hope this helps!
Related
I have made an android app that changes images in imageview after 5secs.However the images are superimposed on the image that is selected when imageview is created.I don't want that image.Kindly help,here is the code
MainActivity.java
public class MainActivity extends Activity {
Handler handler = new Handler();
int count;
int images[] = { R.drawable.c1,
R.drawable.c12,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7 };
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
handler.postDelayed(changeImage, 5000);
}
Runnable changeImage = new Runnable() {
#Override
public void run() {
if (count >6) {
handler.removeCallbacks(changeImage);
} else {
if (count >= 6)
count = 0;
AlphaAnimation animation1 = new AlphaAnimation(0.5f, 1.0f); //here is a bit of animation for ya ;)
animation1.setDuration(5000);
animation1.setStartOffset(1300); //time for that color effect
animation1.setFillAfter(true);
imageView.startAnimation(animation1);
imageView.setBackgroundResource(images[count++]);
handler.postDelayed(changeImage, 5000);
}
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
main.xml
<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.change.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="206dp"
android:layout_marginLeft="44dp"
android:layout_toRightOf="#+id/textView1"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Rather than using
imageView.setBackgroundResource(images[count++]);
try
imageView.setImageResource(images[count++]);
I added imageView.setImageDrawable(null) and it worked.
I was just trying to get to know well how to change activities and i made a simple example and yet nothing.
The thing is, i dont know how to go back to the main activity after changing to the second activity.
here is MainActivity.java
public class MainActivity extends ActionBarActivity implements OnClickListener{
private String DEBUG_TAG1 = "tag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pauseButton = (Button) findViewById(R.id.pause_button);
pauseButton.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
if (v.getId() == R.id.pause_button)
{
Log.d(DEBUG_TAG1,"pause");
Intent i = new Intent(this, SecondActivity.class);
Log.d(DEBUG_TAG1,"intent created");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
protected void onStop() {
super.onStop();
Log.d(DEBUG_TAG1,"on stop");
}
protected void onResume() {
super.onResume();
Log.d(DEBUG_TAG1,"on resume");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
here is SecondActivity.java
public class SecondActivity extends ActionBarActivity implements OnClickListener{
private String DEBUG_TAG2 = "tag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button pauseButton2 = (Button) findViewById(R.id.pause_button2);
pauseButton2.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
if(v.getId() == R.id.pause_button2)
{
//here i have to get back to the MainActivity
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here are the XML files:
activity_main.xml
<LinearLayout 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="net.iversoncru.changeact.MainActivity" >
<Button
android:id="#+id/pause_button"
android:layout_gravity="center"
android:text="||"
android:textColor="#ff0000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
</LinearLayout>
and activity_second.xml
<LinearLayout 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"
android:background="#000000"
tools:context="net.iversoncru.changeact.SecondActivity" >
<Button
android:id="#+id/pause_button2"
android:layout_gravity="center"
android:text="||"
android:textColor="#aa0000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
</LinearLayout>
can anyone tell me how?
I noticed that, when SecondActivity starts, OnStop() is called on MainActivity.
How do i use OnResume()?
Also i read that maybe i have to save the state of MainActivity to be able to be back to it...
Im a little bit lost.
#Override
public void onClick(View v)
{
if(v.getId() == R.id.pause_button2)
{
//here i have to get back to the MainActivity
finish();
}
}
when you switch to the next activity OS pushes current activity on the stack and brings forward the requested activity.
finish() method from activity class will announce to the system that you are done with your work in current activity and restore my previous activity on the stack.
I want to adjust editText input type such that first I want to show number only on keybord with a button for switching keyborad from numbers keypad to qwerty .I have find many similar question related to this but nothing has worked out.
I have tried to do this by setting ime action button.Don't know whether it is right or not but it is not working for me.Is there any other alternative for this?
Here is the code :
xml:
<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.helloword.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText android:id="#+id/ed"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:imeOptions="actionGo|actionNext|actionSend|actionDone"
android:imeActionLabel="abc"
android:inputType="number"
android:imeActionId="#+id/my_ime"/>
</RelativeLayout>
Activity:
public class MainActivity extends ActionBarActivity {
EditText ed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText)findViewById(R.id.ed);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
ed.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg1==R.id.my_ime)
{
Log.e("inside", "inside");
ed.setInputType(InputType.TYPE_CLASS_TEXT);
}
return false;
}
});
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am new to android development and I cannot understand this error, I am making a simple stop watch app, it has a Chronometer and 3 buttons (Start, stop, reset) I define all of these in the main java file correctly with their correct tags.
Its strange because I tested the app before and my app was working fine, but then I re-positioned the buttons in the GUI, and now it is saying that I am not casting the chronometer correctly.
06-06 19:11:07.488: E/AndroidRuntime(21511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shitstopwatch/com.example.shitstopwatch.ShittyClockActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Chronometer
here is the main java file:
public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
and here is the activity XML with the ID tags:
<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.shitstopwatch.ShittyClockActivity$PlaceholderFragment" >
<Chronometer
android:id="#+id/mainChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="#string/chronometer"
android:textSize="#dimen/BigText" />
<Button
android:id="#+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mainChronometer"
android:layout_below="#+id/mainChronometer"
android:layout_marginTop="63dp"
android:text="#string/startButton" />
<Button
android:id="#+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/startButton"
android:layout_below="#+id/startButton"
android:layout_marginTop="16dp"
android:text="#string/resetButton" />
<Button
android:id="#+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/resetButton"
android:layout_below="#+id/resetButton"
android:layout_marginTop="20dp"
android:text="#string/stopButton" />
</RelativeLayout>
Clean your project (e.g., Project > Clean in Eclipse).
Sometimes, the R constants get out of sync with the resource entries in the APK file, causing this error.
Simple timepicker function is failing with Fatal Exception - NullPointerException and java.lang.reflect.InvocationTargetException on following line :
int hour = time_picker.getCurrentHour();
Fragment_main.xml :
<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.calapp1.MainActivity$PlaceholderFragment" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
<TextClock
android:id="#+id/textClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextClock" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:text="Show Time"
android:onClick="setTime"
/>
MainActivity.java :
public class MainActivity extends Activity {
TimePicker time_picker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time_picker = (TimePicker) findViewById(R.id.timePicker1);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void setTime(View v)
{
int hour = time_picker.getCurrentHour();
int minute = time_picker.getCurrentMinute();
try{
Toast.makeText(getBaseContext(), "select time is "+hour, Toast.LENGTH_LONG).show();
}
catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(), "problem", Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Can you please assist?
Thank you,
Yogesh.
You timepicker is in the fragment layout and not in the activity layout. Activity onCreate() is too early to find it in the activity view hierarchy.
Move the findViewById() to fragment onCreateView() and change it to rootView.findViewById().
Please be noted that you have your timepicker in Fragment_main.xml and you're trying it in the onCreate where the contentview is set as setContentView(R.layout.activity_main); so please move the line to onCreateView() and use rootView.findViewById(). That will srely solve the issue.