Remove button over a button Kotlin - android

I am coding the functionality of a button that serves to remove other buttons. My buttons are dynamic. I have to program the functionality in my .kt. I am noob on android studio so am not sure what the best way to do it.
The function is like this:
I have a TableRow where are my buttons. All this buttons have their own listener.
val param: TableRow.LayoutParams =TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT)
var buttondynamic = Button(this)
buttondynamic = buildButton(item, i, mysize, mysize, true)
buttondynamic.layoutParams = param
tr.addView(buttondynamic)
Also I have a listener for the trash button but I don't know how to make the action works. How I should code this? Hove you some idea?
trashButton.setOnClickListener {
// This for goes over the table taking every single button
for(i in 1..12){
val btnFor: Button = findViewById(i) as Button
... -> here the codo shoul be (I think)
}
}
On the other hand, I have the code for the delete button listener. I only need the idia for the remove button over a button.
Thank you so much

If you want to delete it for just the current view , mean that you want it to appear the next time you open that activity , so just set its visibility like :
button.Visiblity = View.GONE
But if that buttons is connected to some stored data , like an array or a list or something like that , you can just delete that stored data for that button and reload your table again .

Related

How to refer to a button that is in the list Kotlin

I am trying to create an app in android studio. I've only recently started to get interested in this and ran into a problem. As planned, for each move in the game, the player presses several buttons, I would like to put the id of these buttons in a separate list, and when necessary, use this list to change the color of all those buttons that are in this list
What i did:
I have the list->
val move_list: MutableList<Any> = mutableListOf()
When the player presses the button, I add its id to move_list
fun for_btn_buba2(view: View){
move_list.add(buba2.id)
In activity_main.xml my button seems like:
<Button
android:id="#+id/buba2"
android:text="Buba 2"
...
android:onClick="for_btn_buba2"/>
And on click of another button i wanted to insert code like this:
move_list[0].setBackgroundColor(Color.parseColor(colorString:"#FFFC9D45"))
move_list[0] means id for button buba2
In python it can be, but it isnt python)
How can I change the color of the button through the list index with the buttons?
Firstly I suggest you indicate the type of what is in the list, which is Int so like
val move_list: MutableList<Int> = mutableListOf()
Then you can probably do this in the onClick of the other button
findViewById<View>(move_list[0]).setBackgroundColor(Color.parseColor("#FFFC9D45"))
or to do it to all
move_list.forEach {
findViewById<View>(it).setBackgroundColor(Color.parseColor("#FFFC9D45"))
}
I also notice you directly refer to buba2 in it the for_btn_buba2. I have the feeling that you are writing this function for every buba. This is unnecessary. You can get the id from the view parameter because that is in fact the same id. So do
fun for_btn_buba(view: View){
move_list.add(view.id)
}
then you can give each buba the same for_btn_buba as android:onClick
Alternatively you don't even work with ids at all and make it a
val move_list: MutableList<View> = mutableListOf()
and then do
fun for_btn_buba(view: View){
move_list.add(view)
}
and then you can actually change the background like you wrote it:
move_list[0].setBackgroundColor(Color.parseColor("#FFFC9D45"))
or
move_list.forEach {
it.setBackgroundColor(Color.parseColor("#FFFC9D45"))
}

How can I display a button in the onCreate method of an activity based on a given condition?

I want to display a button in an activity, but the text and background of the button are different if a specific condition is met by the user. How can I initialise the same button in the OnCreate method, having two different designs based on a condition?
Here Maybe this help
val btnGet = findViewById<Button>(R.id.btnGet)
if (id == 1){ //Your condition
btnGet.text = id.toString()
btnGet.visibility = View.VISIBLE //Make Sure in xml it is gone
btnGet.setTextColor(Color.parseColor("#bdbdbd"))
btnGet.setBackgroundColor(Color.parseColor("#ffffff"))
}else{
btnGet.text = id.toString()
btnGet.visibility = View.VISIBLE
btnGet.setTextColor(Color.parseColor("#FFBB86FC"))
btnGet.setBackgroundColor(Color.parseColor("#FF03DAC5"))
}
If you want the full code I will share it with you
Happy coding
1. You can change this properties in run time.
2. You can create two drawables files to set in the button background, but the text you still needing to change in runtime.
3. And you can use data binding in your xml file, using layout Data, like:
Google Link

Keep updating the same activity with different data on button click

I have an android application that asks users a few questions, kind of a survey. Each question has an YES and a NO Button. So when the user presses either YES or NO button, a new Button to go to the next question becomes visible over the YES and NO buttons. When the user clicks on the next question button it changes the textview for the question to the next question text as well as the imageView for that question changes.
I am making use of DataBinding along with LiveData.
I have created a list of images used and another list for the questions as follows in my ViewModel class.:
//list of images : Question 4 does not have an image
imgs= listOf(
R.drawable.imgQ1, // 1
R.drawable.imgQ2,// 2
R.drawable.imgQ3)//3
//list of questions
qs= listOf(
"What is .....?",//1
"Who is ....?",//2
"How to .....?",//3
"Why is .....?")//4
I created a ViewState to handle when to show which button and UI:
sealed class MyUIState{
data class UIState(
val policyText: Boolean, // determines if the policy textview should be visible or not
val nextQuestionButton: Boolean,// determines if the next question button should be visible or not
val nextQuestionNewImage: Int = R.drawable.imgQ1,//what image is used for the next question
val nextQuestion: String //what is the next question from the list
) : MyUIState()
}
I also have a BindingAdapter class which handles the visibility and text changes and image changes
(basically this - I have added these function to my layout file with data binding):
#BindingAdapter("btnNextQ")
fun Button.btnNextQ(myState: MyUIState?){
visibility = if (myState is MyUIState.UIState) {
if (state.nextQuestionButton)
View.VISIBLE
else
View.GONE
} else
visibility
}
#BindingAdapter("newImage")
fun ImageView.newImage(myState: MyUIState?){
if(myStateis MyUIState.UIState){
setImageResource(state.nextQuestionNewImage)
}
}
And then in my ViewModel class I have a function for the click event on either the YES or NO Button where I call the UIState like this:
//I have this declared at the top of my class (global)
private val myViewState = MutableLiveData<MyUIState>()
//this the button click for yes:
fun YesClick(){
myViewState.postValue(MyUIState.UIState(
policyText= true,
nextQuestionButton = true,
nextQuestion = qs[0])) //still keep the text for question1
}
Now, I want the image and question to change after the user clicked the next question button. So basically what happens is that after clicking next question, the next question buttons goes invisible as well as the other components keeping the YES and NO buttons and the updated question and image. This is my code for the click event on the next question button:
fun nextQuestion(){
myViewState .postValue(MyUIState.UIState(
nextQuestionNewImage = imgs[1],
nextQuestion = qs[1],
nextQuestionButton = false,
policyText= false))
}
So the state does move to the next question with the necessary items being visible and invisible, but I am struggling to keep it going to the next questions in the list. I can go from question1 to question2 but not sure how to keep doing that until the end of the list of questions.
So basically, I just want to keep updating the same activity with the new data.
Any help or advice will be highly appreciated.
Not sure if I understood your real problem. Maybe if you share more of your code we could help you better.
Anyway, I would recommend you to have two states for your view (activity in your case), one state for the question itself containing both yes and no buttons, question text and the right image. And another state for your "move to next" question which contains the question text, button and whatever you want.
Then your view needs to take care of each state. In the case, you need to deal with one state while dealing against the other state, for example showing one button and hiding the ones you don't need.
So whenever you click on the yes button, the VM will get the event, check everything and then emit an event back to the View with a new state, and so on.
It's quite the same thing you did, but using two states will help you to tackle each state and ease off your code

Android Programming, C#: Show a certain line of code, only when a certain button is pressed

here is my beginner problem.
Ive just been introduced to android programming, and now im just playing with a simple calculator program, only to see how i connect my xaml files with my source code. all the program does really is ask for two integers, then add them together, and, if the "equals" button is pressed, displays the result in an empty EditText.
1) is an EditText the right empty box to display a result?
2) how do i get the EditText to display my result when the equal button is pressed? here is the bit of code where im lacking the "if-declaration":
//output result
if //button result is clicked:
{
res.Text = res.ToString();
}
can you hint me towards the right if declaration?
thanks!
I would suggest using a Textbox
If you are using Xamarin for Android. First you have to locate the button
Get a reference to the controls that were created in the layout file via the Android Designer. Then add the following code inside the OnCreate method, after the call to SetContentView
Button buttonEqual = FindViewById<Button>(Resource.Id.CallButton);
Then add the following code to response to the button click
buttonEqual.Click += (object sender, EventArgs e) =>
{
// When the button is clicked,
// display the result in the textbox
textBox.Text = res.ToString();
}

android how to add items dynamically to a listview like in chat application

I am trying to do a chat application. In my activity I have a ListView and at bottom EditText with a Button "Send"
Like in chat after writing in EditText and press the Send Button the text will appear in ListView. Two users are there. If the last user is same then that text should add to the previous list.
How can i do that ?
here is my code below,
http://pastebin.com/hghj1fBJ
i don't want in this structure,
me: hi (after send button click)
me: h r u (after send button click)
i want in this structure,
its taking two list item but i want
me: hi (after send button click)
h r u (after send button click)
friend: fine
public void sendMessage(String message)
{
messageAdapter.add("me :"+message);
messageAdapter.notifyDataSetChanged();
}
}
Since you are trying to build chat kind of application, you should check transcript mode of listview too.
check here,
http://www.mubasheralam.com/tutorials/android/listview-transcript-mode
I don't see that you call notifyDataSetChanged() on your ArrayAdapter.
setAdpater().. method should be called after the new text is beeing added.. and the String[] array should be updated with new values..
or
refer this http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List12.html

Categories

Resources