Toast not showing properly in my activity - android

i can't figure why my toast dosen't show up anyone can help ... i started today to learn app development but i don't understand what is the problem...
My app works without any error , this is a work exercise from my programming book
package com.example.geoquiz;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class QuizActivity extends Activity {
private Button but1;
private Button but2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
but1 = (Button) findViewById(R.id.true_button);
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
but2 = (Button) findViewById(R.id.false_button);
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorect_toast, Toast.LENGTH_SHORT).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.quiz, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"
android:id ="#+id/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"
android:id ="#+id/false_button" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GeoQuiz</string>
<string name="hello_world">Hello world!</string>
<string name ="question_text">Gabriel este un baiat destept ?</string>
<string name ="true_button">TRUE</string>
<string name ="false_button">FALSE</string>
<string name ="action_settings">Settings</string>
<string name ="correct_toast">RaspunsCorect</string>
<string name ="incorect_toast">RaspunsIncorect</string>
</resources>

Try this. Go into your xml and on the button you want to make the toast text add
android:onClick="ToastButtonClicked" />
^ into it.
Then use the below code for making the toast text.
}
public void ToastButtonClicked(View view)
{
Toast.makeText(this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
}

Try replacing
Toast.makeText(QuizActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
with
Toast.makeText(getApplicationContext(),R.string.correct_toast, Toast.LENGTH_SHORT).show();

replace this
Toast.makeText(QuizActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
with
Toast.makeText(QuizActivity.this,"Correct Toast", Toast.LENGTH_SHORT).show();
may be it will help you

Related

Access string_array from activity and send selected item to another activity in android

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array

edit_message can not be resolved or is not a field

I am getting this error. It says "edit_message can not be ressolved or is not a field" Can somebody tell me why I am getting this error is how can I fix it.
LAYOUT OF MAIN ACTIVITY: -
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
CODE OF MAIN ACTIVITY
package com.practice.firstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE= "com.practice.fristapp1.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message); ///********ERRORR***********
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
LAYOUT OF SECOND ACTIVITY: -
<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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.firstapp1;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
STRING RESOURCES:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
It looks like your code is right. Check the following things:
1) Save the layouts in xml, one reason could be its not saved.
2) Check there is no error in your xmls and project is build.
3) If you are using eclipse, go to Projects->Clean All Projects and then projects->Build Automatically.
4) Open your activity and press "cmd + shift + o" or ctrl+ shift+o (in windows). It shall import the R.java, select the com..R in the options.
I hope it shall solve the error.
You're missing import com.practice.firstapp1.R in MainActivity.java.
See the docs on Accessing Resources for more information.

How to change textView to a #string and raising

so what i am tring to do is, i am having a book like-app, and want to setup a button when click it change the textview to a string in the #string rec. but in a different way
i will put the code just a moment, lets say i have a string in #string that is NH1,NH2,NH3
i can do for button View.setText(R.string.NH1); that will work but i want to create a counter and make it so every time clicked counter++ and want it to be like this View.setText(R.string.NH+counter); if you know what i mean so each time it clicked to goes to next #string without changing layout every time and without an array because the paragraph are very long
also if i have NH1-10 and the counter did not stop and still goes on what will i do.
if you are sill here thank you for reading.
XML(sheetpaper)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="496dp"
android:background="#drawable/bg1"
android:gravity="top"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:id="#+id/bPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="51dp"
android:background="#android:color/transparent"
android:text=" " />
<TextView
android:id="#+id/tvHadith"
android:layout_width="310dp"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:text="#string/NH1"
android:textColor="#android:color/white" />
<Button
android:id="#+id/bNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bPrev"
android:layout_alignBottom="#+id/bPrev"
android:layout_alignRight="#+id/tvHadith"
android:background="#android:color/transparent"
android:gravity="center"
android:text=" " />
"
</RelativeLayout>
</LinearLayout>
Java (Nawawi)
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Nawawi extends Activity {
private static final String NH = null;
Button Next;
Button Prev;
TextView View;
int counter = 0;
String H=NH;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sheetpaper);
Next = (Button) findViewById(R.id.bNext);
Prev = (Button) findViewById(R.id.bPrev);
View = (TextView) findViewById(R.id.tvHadith);
Next.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
View.setText(R.string.H);
}
}); Prev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
View.setText(R.string.);
}
});
}
}
You could do this by creating an XML string array, so your strings will still be referenced in resources, but you'll be able to loop through them.
Something like this, in values/strings.xml:
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
And then in Activity:
Resources res = getResources();
final String[] planets = res.getStringArray(R.array.planets_array);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter < planets.length) {
textView.setText(planets[counter]);
counter++;
}
}
});
Be sure to check the counter against arrays length, otherwise you will get an IndexOutOfBoundsException.

Button OnClickListener not working

I am new to Android. I am trying to get input from the EditText and displaying it in TextView with the help of button. When I click the button nothing happens. Please help, here is my MainActivity.java code -
package com.example.addname;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView v1;
EditText t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
t1 = (EditText) findViewById(R.id.editText1);
v1 = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and here is my 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="48dp"
android:text="Button" />
</RelativeLayout>
Instead of
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
You can use
if(t1.getText().isEmpty()) // isEmpty() is only available from API 9 and Above
{
v1.setText(t1.getText().toString());
}
or this
if(t1.getText().trim().length == 0)
{
v1.setText(t1.getText().toString());
}
Don't compare strings using ==. You're actually comparing reference of an object. To compare strings, use equals() method.
I'd recommend comparing strings with .equals() since Objects compared with == usually fails (== compares references, not Object content).
if(t1.getText().toString().equals("")))
in your case though,since you're comparing an empty String, you can also use
if (t1.getText().toString().length == 0)
Then note that once you set the text, you're setting v1's text to an empty string since your logic indicates that only upon an empty string you set v1's text. So I'd recommend getting rid of the if altogether:
#Override
public void onClick(View arg0) {
v1.setText(t1.getText().toString());
}

Compare and see if the user click the right radio button for android quiz answer

Hello guys I need to make a simple android quiz. Well what I've done so for is these with the help of other examples online.
MyOSIActivity.java
package com.mns.mp;
import tp.mns.mp.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyOSIActivity extends Activity {
private RadioGroup radioAnswerGroup;
private RadioButton radioAnswerButton;
private Button btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioAnswerGroup = (RadioGroup) findViewById(R.id.radioAnswer);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioAnswerGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioAnswerButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyOSIActivity.this,
radioAnswerButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
main.xml
<?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" >
<TextView
android:id="#+id/Qn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Qn_1"
/>
<RadioGroup
android:id="#+id/radioAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_1"
android:checked="true" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_2" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_3" />
</RadioGroup>
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_submit" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyAndroidAppActivity!</string>
<string name="app_name">MyAndroidApp</string>
<string name="Qn_1"> Question 1: Acknowledgement, Sequencing, and Flow control are characteristics of which OSI layer?</string>
<string name="radio_1">Layer 2</string>
<string name="radio_2"> Layer 3</string>
<string name="radio_3"> Layer 4</string>
<string name="btn_submit">Submit</string>
</resources>
Ok now I'm trying to make the system compare the user answer with the correct answer after the user press submit button which in this case its Layer 4. Then if the user answer is wrong, the toast will reply to the user it is incorrect and show the correct answer. Where as if the user is correct then it will reply correct to the user. So I need to use if else but the thing is I am still not familiar with it. Ill appreciate if you guys give me guidances because I really need it. Thank you soo much :D
you can do this in so many ways.... if you know the id of the correct answer, you can do:
if (selectedId==correctId){
//show toast saying it is correct
}
else{
//toast incorrect
}
In kotlin You may use..
radioAnswerGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
//radioAnswerButton is your correct radio button id
val isChecked = radioAnswerButton.isChecked
if (isChecked) {
Log.e("is check","right answer--")
}else{
Log.e("is check","wrong answer")
}
})

Categories

Resources