thank you.
I wanted to add a simple TextSwitcher to a fragment, it's all good (I think) till this line:
TextView t = new TextView (TextSwitcherActivity.this);
I am missing out why it cannot resolve the symbol: TextSwitcherActivity.
Please, if you have any suggestions I would truly appreciate.
Here is the whole Java code, I believe is by the book, nothing new.
public class viewOne extends AppCompatActivity {
TextSwitcher switching;
Button pre_button, next_button;
String strTextSwitcher[] = {"Text 1", "Text 2", "Text 3, Text 4", "Text demo 5"};
int currentIndex = -1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_view_one);
next_button=(Button)findViewById(R.id.next_button);
pre_button=(Button)findViewById(R.id.pre_button);
switching=(TextSwitcher)findViewById(R.id.switching);
switching.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
TextView t = new TextView (TextSwitcherActivity.this);
t.setGravity(Gravity.TOP|Gravity.CENTER);
t.setTextSize(36);
return t;
}
});
switching.setCurrentText("click on the next button to switch text");
pre_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if (currentIndex>0)
switching.setText(strTextSwitcher[currentIndex]);
}
});
next_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (currentIndex<strTextSwitcher.length-1)
currentIndex = currentIndex+1;
switching.setText(strTextSwitcher[currentIndex]);
}
});
}
}
Try
TextView t = new TextView (this);
//or
TextView t = new TextView (viewOne.this);
Related
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I am working with this bingo callboard project in Android Studio. The principle is when I click a number button, it will show on the first textview. When I click the second number button, the first textview will show the current number pressed and the previous string will be passed to the second textview. How would I do this? Screenshot of app included. A sample of the MainActivity.java code is here:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
Screenshot
Hey so from what I understand you need to update the lst textview with the data of txt textView. Well you can write a common method which can be called from any button click with the new data. something like this:
public class MainActivity extends AppCompatActivity {
private Activity mainActivity;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = MainActivity.this;
final Button button1 = findViewById(R.id.b1);
final Button button2 = findViewById(R.id.b2);
final Button button3 = findViewById(R.id.b3);
final Button button4 = findViewById(R.id.b4);
final Button button5 = findViewById(R.id.b5);
final TextView txt = (TextView) findViewById(R.id.presentcall);
final TextView lst = (TextView) findViewById(R.id.lastcall);
button1.setOnClickListener(new View.OnClickListener() {
Drawable background = button1.getBackground();
#Override
public void onClick(View v) {
txt.setText("B1");
updateTextView("B1");
if (button1.getText().equals("1"))
{
button1.setText(" 1 ");
button1.setBackgroundResource(R.drawable.onclick_border);
}
else if (button1.getText().equals(" 1 "))
{
button1.setText("1");
button1.setBackground(background);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
Drawable background = button2.getBackground();
#Override
public void onClick(View v) {
txt.setText("B2");
updateTextView("B2");
if (button2.getText().equals("2"))
{
button2.setText(" 2 ");
button2.setBackgroundResource(R.drawable.onclick_border);
}
else if (button2.getText().equals(" 2 "))
{
button2.setText("2");
button2.setBackground(background);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
Drawable background = button3.getBackground();
#Override
public void onClick(View v) {
txt.setText("B3");
updateTextView("B3");
if (button3.getText().equals("3"))
{
button3.setText(" 3 ");
button3.setBackgroundResource(R.drawable.onclick_border);
}
else if (button3.getText().equals(" 3 "))
{
button3.setText("3");
button3.setBackground(background);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
Drawable background = button4.getBackground();
#Override
public void onClick(View v) {
txt.setText("B4");
updateTextView("B4");
if (button4.getText().equals("4"))
{
button4.setText(" 4 ");
button4.setBackgroundResource(R.drawable.onclick_border);
}
else if (button4.getText().equals(" 4 "))
{
button4.setText("4");
button4.setBackground(background);
}
}
});
button5.setOnClickListener(new View.OnClickListener() {
Drawable background = button5.getBackground();
#Override
public void onClick(View v) {
txt.setText("B5");
updateTextView("B5");
if (button5.getText().equals("5"))
{
button5.setText(" 5 ");
button5.setBackgroundResource(R.drawable.onclick_border);
}
else if (button5.getText().equals(" 5 "))
{
button5.setText("5");
button5.setBackground(background);
}
}
});
}
private void updateTextView(String newData){
String oldData = txt.getText().toString();
lst.setText(oldData);
txt.setText(newData);
}
}
This will update your last textview with your txt textview data. YOu can call this method from your button clicks with the new data as a parameter . Hope this helps you
There is nothing wrong with p.matthew13's code. The problem is with the code sequence. I may also have to delete a line on my code because it's already redundant.
The redundant code is txt.setText(""); which contradicts txt.setText(newData);
I have to delete my setText code and replace it with the updateTextView("");
Also, I did place p.matthew13's code before my public void.
And that fixes my problem, after trying to shuffle up the code execution sequence.
Thanks very much!
I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.
I have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});
I have a simple app with 50 articles, my previous and next button work good in all choices BUT when the user goes to the end (at 50th article) the app can't load again from the beginning the 1st article.
Thanks for any help, here is my code:
I don't know if needs more cede for to be clear, cause I am a newbie.
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}