I'm kind of new when it comes to Android Application development and I'm developing an app at the moment. I'm trying to get my TextView change every time the user clicks the Button(NEXT) and when another Button (PREVIOUS) gets clicked on I want it to change back to the original TextView. So basically I'd like to set up a certain amount of TextViews and be able to browse through them with the two Buttons I mentioned.
So far I only know how to make the the TextView change one time on a Button(NEXT) click. I'm using this piece of code for that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Next = (ImageButton) findViewById(R.id.Next);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text1= (TextView) findViewById(R.id.Text1);
Text1.setText("New Text");
}
});
NOTE: The Button "PREVIOUS" isn't included yet because I didn't know what to do with it yet.
I'm getting the feeling this code is only used when you want the TextView to change one time and you need a whole different method to make it change multiple times.
I hope I provided you with enough information and you are willing to help me out here.
Thanks in advance!
public class MyActivity extends Activity implements View.OnClickListener {
int stringIdList[] = {R.string.text1, R.string.text2, R.string.text3, R.string.text4}
int stringListCounter = 0;
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton next = (ImageButton) findViewById(R.id.Next);
ImageButton previous = (ImageButton) findViewById(R.id.Previous);
text1 = (TextView) findViewById(R.id.Text1);
Next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.Next && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
} else if (id == R.id.Previous && counter > 0) {
stringListCounter--;
}
Text1.setText(stringIdList[stringListCounter]);
}
What this does is assigns your Activity to an OnClickListener to handle the click events. If Next was pressed and the counter is within the range of the array list, it will increase the counter. The same for previous. At the end of the click, it will set the text to whatever the ID is. This assumes your strings are in a strings.xml file which is recommended in the Android spec and is static.
I think you can store history as List and have all states of textview in each moment.
Only thing that you have to do is to take previous value from this history stack after pressing previous button.
Related
I'm new at androidstudio and want to compare a imageView by the following:
I have 2 imageView, both are using a drawable i named "blank" at the start of the app, using if/else I want to chance those images to another drawable i have, i tried the following:
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
if (equipament1.equals(R.drawable.blank)){
equipament1.setImageResource(R.drawable.reactor);
}
else if (equipament2.equals(R.drawable.blank)){
equipament2.setImageResource(R.drawable.reactor);
} else {finish();}
but it doesn't work, the app just replaces the first image and if i click on the button again, nothing happens (this if/else is inside a button).
I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces.
I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app.
The problem is that the second time you have already changed the value of the comparator.
If the objective is just to change the images you don't need the if/else.
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
When the user clicks your button, you want to do 2 things. You want to show some images, or you want to call finish().
I would suggest using a boolean as a flag the the state and compare that instead of comparing the ImageView itself. This'll be easier, and make your code easier to read.
I created a flag called firstClick that is set to true by default. When the user clicks your button (button1 in this example), we check against that and show the images. Then we set it to false,
so the next click will call finish().
private ImageView equipament1;
private ImageView equipament2;
// The current state of the Activity
private boolean firstClick = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
// Setting your OnClickListener
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( firstClick ) {
firstClick = false;
sentImg();
} else {
finish();
}
}
});
}
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});
From this code it creates dynamic buttons accodring to a given value from another layout. I need to get the id of that and add another button (if dynamic button clicks then I need to add another button dynamically).
for (int i = 0; i < value1; i++) {
LinearLayout.LayoutParams paramsIButton = new LinearLayout.LayoutParams
((int) ViewGroup.LayoutParams.WRAP_CONTENT, (int) ViewGroup.LayoutParams.WRAP_CONTENT);
ibutton = new ImageButton(HomePage.this);
ibutton.setImageResource(R.drawable.add);
ibutton.setLayoutParams(paramsIButton);
paramsIButton.topMargin = -70;
paramsIButton.leftMargin = 370;
paramsIButton.bottomMargin = 30;
ibutton.setId(i);
ibutton.getPaddingBottom();
ibutton.setBackgroundColor(Color.WHITE);
ibutton.setAdjustViewBounds(true);
rR.addView(ibutton);
}
If I understood correctly from the additional information you provided on your comment, you need to know when a user clicked on a button. You could set an OnClickListener to your button.
// Somewhere in your activity . . .
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// The button is clicked! Do whatever you want.
}
});
}
// ...
// Rest of the code
// ...
Of course, you should replace R.id.button1 with your button's id.
Seems to me like you need to add an onClickListener for the dynamically added button.
Make your class implement OnClickListener and then add a listener for the dynamic button:
ibutton.setOnClickListener(this);
and add an onClick Listener within your class:
#Override
public void onClick(View v)
{
// do something with this ID
v.getId()
}
I don't know how you keep track of the bulbs and fans, I'd hope you don't do it via the UI elements alone. I'd probably do it a bit differently, creating a data structure to track the bulbs and fans and attach the specific bulb or fan object to the UI element as a tag.
I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}
I have this problem with my ToggleButton.
I want it to create/delete a button upon being toggled, and at the same time add content/functions to the button, like drawable and such.
This is the current code:
public class BillardScoreboardActivity extends Activity {
/** Called when the activity is first created. */
Button minuskegle, minuskugle, pluskugle, pluskegle, plusmidkegle, minusmidkegle, miss;
ToggleButton toggle;
LinearLayout bottomlayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggle = (ToggleButton) findViewById(R.id.bRedGreen);
toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
pluskugle = (Button) findViewById(R.id.bBallhole);
minuskugle = (Button) findViewById(R.id.bBallhole);
pluskegle = (Button) findViewById(R.id.bKegle);
minuskegle = (Button) findViewById(R.id.bKegle);
plusmidkegle = (Button) findViewById(R.id.bKeglemid);
minusmidkegle = (Button) findViewById(R.id.bKeglemid);
bottomlayout = (LinearLayout) findViewById(R.id.bottomlayout);
miss = (Button) findViewById(R.id.bMiss);
if(toggle.isChecked())
{
minuskugle.setBackgroundResource(R.drawable.redballinhole);
minuskegle.setBackgroundResource(R.drawable.redkegle);
minusmidkegle.setBackgroundResource(R.drawable.midkegleminus);
miss.setBackgroundResource(R.drawable.missbutton);
miss.setVisibility(View.VISIBLE);
}
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
miss.setVisibility(View.GONE);
}
}
});
}
The current problem is that it can't find the (buttontest) in this part of the code:
else
{
pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
pluskegle.setBackgroundResource(R.drawable.kegleb);
plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
bottomlayout.removeView(buttontest);
}
And as mentioned earlier, the second problem is to make the button inherit some functions/content.
for bigger version: http://i.imgur.com/KxKvh.png
Btw... Everytime i start up the application, it gives me 2 apps to choose between, whereof only the bottom one works:
I guess the problem is that the togglebutton's initial state is 'checked'. That means when you click it the first time, isChecked() will return false and the else-part of your code will be executed. But at that point, buttontest hasn't been added to bottomlayout yet.
I recommend you to have the button inside the layout by default and call buttontest.setVisibility(View.GONE) when you would like to hide it and buttontest.setVisibility(View.VISIBLE) when it needs to be shown.
As for your second question, just call setBackgroundResource/Drawable to add content (like you're already doing it with the other buttons). If you say you want to add functionality, I assume you intend to do something when the button is clicked? If yes, add a View.OnClickListener.
Hope I could help you.