This question already has answers here:
Custom toast on Android: a simple example
(18 answers)
Closed 7 years ago.
I am trying to run a toast app. where one button shows a normal toast message and the second button shows a custom toast with an image (ic_launcher) and some text.
The normal toast works fine without the second button code inserted. For the custom toast.
1.custom_layout and 2. custom_toast_layout_id are not recognized and the application shows an error.
Here is the xml layout:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/ivImage"
android:layout_marginRight="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"
android:id="#+id/button"
android:layout_below="#+id/tv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Again"
android:id="#+id/button2"
android:layout_marginTop="56dp"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/tvToast"
android:textColor="#000"/>
</RelativeLayout>
This is my activity:
package com.example.rahulshukla.myapplication;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends Activity {
TextView helloText;
Button press, press2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
press.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"This is a Toast", Toast.LENGTH_LONG).show();
}
});
press2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout_id));
ImageView image = (ImageView)findViewById(R.id.ivImage);
image.setImageResource(R.drawable.ic_launcher);
TextView toastText = (TextView)findViewById(R.id.tvToast);
toastText.setText("Button is clicked");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
private void initialize() {
helloText = (TextView)findViewById(R.id.tv1);
press = (Button)findViewById(R.id.button);
press2 = (Button)findViewById(R.id.button2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
Add id here . set id android:id="#+id/custom_toast_layout_id" & set this layout name custom_toast.xml.Then clean -Rebuild-Restart
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/custom_toast_layout_id">
Related
I have a popup menu inside every item on a listview. When you click the imageview to the left (settings img with 3 dots) a popup menu should showup. however, I'm getting error
Could not find a method showPopupMenu(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageView with id 'settings_img'
does anyone know what this error is. it is obvious that can't find the method, but is it because it can't find the class/view? if so how can I fix it? thanks
main_activity
package com.example.george.hostscanmenus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List hostList;
ArrayAdapter<String> hostAdapter;
PopupMenu popup;
String subnet = "192.168.10.";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hostList = new ArrayList<String>();
//populating host addresses
for(int i = 0; i < 255; i++) {
hostList.add(subnet+i +"-aa:bb:00:cc:33:ee");
}
//inflating adapter
listView = (ListView) findViewById(R.id.scan_list);
hostAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.host_item, R.id.ip_address, hostList);
listView.setAdapter(hostAdapter);
}
/* code for popup menu in listview */
public void showPopupMenu(View v) {
popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.change_icon:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
case R.id.notifications:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onContextItemSelected(menuItem);
}
}
});
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.host_menu, popup.getMenu());
popup.show();
}
}
array adapter
package com.example.george.hostscanmenus;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by george on 8/11/17.
*/
public class HostAdapter extends ArrayAdapter<String> {
public HostAdapter(Context context, int num, ArrayList<String> allHost) {
super(context, 0, allHost);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
ImageView nodeHostImgView = (ImageView) convertView.findViewById(R.id.host_icon);
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.ip_address);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.mac_address);
ImageView nodeArrowImgView = (ImageView) convertView.findViewById(R.id.port_scan_arrow);
// Populate the data into the template view using the data object
nodeHostImgView.setImageResource(R.drawable.ic_computer_white_24dp);
nodeIpTxtView.setText(host.substring(0,host.indexOf("-")));
nodeMacTxtView.setText(host.substring(host.indexOf("-")));
nodeArrowImgView.setImageResource(R.drawable.ic_keyboard_arrow_right_white_24dp);
// Return the completed view to render on screen
return convertView;
}
}
main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scan_list_linear"
tools:context=".MainActivity">
<!-- labels for ip / mac addres list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_label">
<TextView
android:id="#+id/host_port_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Host"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/ip_open_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="20dp"
tools:text="IP / MAC"
android:paddingLeft="10dp"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/scan_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Scan"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
</LinearLayout>
<!--Listview to display scan output-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scan_list"/>
</LinearLayout>
list item 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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/settings_img"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:padding="3dp"
android:src="#drawable/ic_action_more_vert"
android:onClick="showPopupMenu"/>
<ImageView
android:id="#+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_computer_white_24dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:orientation="vertical"
android:paddingLeft="0dp"
>
<TextView
android:id="#+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
<TextView
android:id="#+id/mac_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
tools:text="aa:bb:cc:00:11:22"
/>
</LinearLayout>
<ImageView
android:id="#+id/port_scan_arrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_keyboard_arrow_right_white_24dp"/>
</LinearLayout>
</RelativeLayout>
menu xml
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/change_icon"
android:title="change icon"
/>
<item android:id="#+id/notifications"
android:title="set notification"
/>
</menu>
You should define your settings_img on your adapter getView and add a click listener for each element.
example:
ImageView settingsImgView = (ImageView) convertView.findViewById(R.id.settings_img);
settingsImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.showPopupMenu() //or popupmenu logic in here if you want
}
});
Usually, when it comes to the adapter, I pass the activity itself instead of the context. Then I pass the activity's context to super() and keep the activity in the field variable. That way I'm able to use all public methods of the parent activity.
I wanted to do some thing like this as shown in image below,Is there any widget to do this or we need to add a view & make it transparent.
Thanks .
you must be create a custom view for it and make it transparent..
Main Activity:
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopup();
}
#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;
}
public void initPopup() {
final Dialog view = new Dialog(this);
view.requestWindowFeature(Window.FEATURE_NO_TITLE);
view.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
view.setContentView(R.layout.transparent_layout);
view.setCanceledOnTouchOutside(true);
view.show();
RelativeLayout rl_chooseone_menu_main = (RelativeLayout) view
.findViewById(R.id.rl_chooseone_menu_main);
rl_chooseone_menu_main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view.dismiss();
}
});
/*Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
view.cancel();
view.dismiss();
}
}, 3000);*/
}
}
activity_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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
This is the Transparent View name is transparent_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/rl_chooseone_menu_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginRight="30dp"
android:text="Tap to view all" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:text="Tap to place your order" />
</RelativeLayout>
</RelativeLayout>
Enjoy Brother,i have used dimmy images,replace it with your own images..
You could make a tutorial Activity with a semi-transparent background, containing ImageViews and/or TextViews as shown in your example.
Tutorial activity's layout
<LinearLayout ...
android:background="#color/semitransparent"
... >
...
colors.xml
...
<color name="semitransparent">#80000000</color>
...
Then just start it on top of your usual Activity if you notice this is the first launch of your app by the user.
I have a button at the end of this app that takes the user to their default browser and to a predetermined link. Unfortunately, that button is broken. I am unsure of whether this is a problem with Java or with XML, but the code for both is attached.
package com.example.ldsm3;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity
import android.content.DialogInterface;
import android.content.Intent;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class Finished extends Activity {
public void onClick(Button button1)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL HERE"));
startActivity(browserIntent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finished);
// tv is the ID for the TextView in the XML file
TextView tv = (TextView) findViewById(R.id.textView2);
// set the TextView to show the score
tv.setText(Misc.correct + "/" + Misc.total);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.finished, menu);
return true;
}
}
And the XML code...
<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=".Finished" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp"
android:text="#string/Finished" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="19dp"
android:text="Go to the game!" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="93dp"
android:text="0/0" />
</RelativeLayout>
How do I fix this? Is it because of XML or Java? Did I not properly define the intent?
You need to add this on your onCreate() method:
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do your stuff here
}
});
I am practising android development, and I'm trying to follow guides on how to make a Twitter client app. I'm implementing my compose activity, but when I run the app the cursor is shown far down from the first line. I've followed many tips on stackoverflow to fix it but couldn't
here's a screenshot: http://t.co/qNA0gssOUG
ComposeActivity.java
package codepath.exercises.tweets;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
public class ComposeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compose);
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Button tweetButton = (Button) findViewById(R.id.tweet_button);
Button cancelButton = (Button) findViewById(R.id.cancel_button);
final EditText tweetEditText = (EditText) findViewById(R.id.edittext_tweet);
tweetEditText.requestFocus();
tweetEditText.setSelection(0);
tweetButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
imm.hideSoftInputFromWindow(tweetEditText.getWindowToken(), 0);
}
});
cancelButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
imm.hideSoftInputFromWindow(tweetEditText.getWindowToken(), 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.compose, menu);
return true;
}
}
activity_compose.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:background="#efefef"
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=".ComposeActivity" >
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/edittext_tweet"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:text="#string/cancel_button" />
<EditText
android:id="#id/edittext_tweet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/cancel_button"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:inputType="textMultiLine" />
<Button
android:id="#+id/tweet_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cancel_button"
android:layout_alignBottom="#+id/cancel_button"
android:layout_alignRight="#id/edittext_tweet"
android:text="#string/tweet_button" />
</RelativeLayout>
If I understand correctly, what you are seeing is the normal behavior of the EditText. The cursor appears at the center line.
set gravity top for edittext
<EditText
android:id="#id/edittext_tweet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/cancel_button"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:gravity="top"
android:inputType="textMultiLine" />
You can add
android:gravity="top"
to EditText in Xml
my first Activity Xm
<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:background="#a5c63b"
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=".MainActivity" >
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#434d23"
android:text="Ok To Continue"
android:onClick="frontt"
android:textColor="#a5c63b" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/question" />
<TextView
android:id="#+id/txtview11"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Loading..."
android:textSize="12dp" />
</RelativeLayout>
This is my Second Activity 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:background="#a5c63b"
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=".MainActivity" >
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="70dp"
android:onClick="onClick"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#434d23"
android:text="OK"
android:textColor="#a5c63b" />
<EditText
android:id="#+id/txtedit"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:background="#edf2db"
android:ems="10"
android:inputType="numberDecimal" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/question" />
<TextView
android:id="#+id/txtview"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignBottom="#+id/txtedit"
android:layout_alignLeft="#+id/txtedit"
android:layout_marginBottom="21dp"
android:text="Guess Single Digit Number"
android:textSize="12dp" />
<TextView
android:id="#+id/txtview2"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignLeft="#+id/btn"
android:layout_below="#+id/btn"
android:text="TextView" />
</RelativeLayout>
this is my first java file code
package com.example.game;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer btnsound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front);
Button next = (Button) findViewById(R.id.btn1);
btnsound = MediaPlayer.create(MainActivity.this,R.raw.game);
btnsound.start();
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent nextScreen = new Intent(getApplicationContext(), CopyOfMainActivity.class);
startActivity(nextScreen);
//finish();
}
} );
}
#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;
}
}
this is my Second java file
package com.example.game;
import java.util.Random;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;
public class CopyOfMainActivity extends Activity {
MediaPlayer btnsound;
Random random = new Random();
int randnumber = random.nextInt(10);
Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsound = MediaPlayer.create(CopyOfMainActivity.this,R.raw.game);
btnsound.start();
b2= (Button) findViewById(R.id.btn);
b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
EditText input = (EditText)findViewById(R.id.txtedit);
TextView resultText = (TextView) findViewById(R.id.txtview2);
String inputstring = input.getText().toString();
int number = Integer.parseInt(inputstring);
if(randnumber==number)
{
resultText.setText("you win" );
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.win);
}
else if (number>randnumber)
{
resultText.setText("you guess high number" );
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.tryagain);
}
else if (number<randnumber)
{
resultText.setText("you guess low number" );
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.lose);
}
}
} );
}
#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;
}
}
I have one button on First Activity xml layout .The first Activity Layout display after run application but whenever i click on button on first xml first want to go on next laytout after clicking then this error show in LogCat tab and application stop working.
AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
what i should do?
The right way to do this given below:
next.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent nextScreen = new Intent(MainActivity.this, CopyOfMainActivity.class);
startActivity(nextScreen);
//finish();
}
} );
Hope this will work fine.
Please add your both activities to Manifest file if you missed out.
<application>
<activity android:name="Activity1">
</activity>
<activity android:name="Activity2">
</activity>
public void butt591(View v) {
v.startAnimation(fivenineclick);
Button but591 = (Button) findViewById(R.id.button591);
but591.setBackgroundColor(Color.parseColor("#ee4035"));
Intent bu591 = new Intent();
bu591.setClass(this, level5q10.class);
startActivity(bu591);
finish();
}
hey, i copy pasted a part of code in my app here 591 denotes level 5 ninth question and on selecting first option
that is wrong answer so on clicking the option it will set background to #ee4035 which is red color and when the user presses this option it will switch to level 5q10
I hope you understood from this code