Search code examples
androidandroid-widget

how to change the widget button's text in android


i am very new to android and tying to make a simple to-do list widget

The text on the widget button doesnt change. The activity launchs, but once i type inside the textfield the text from the text field doesnt get stored in the button

this is my current code which is inside an onCreate fun of an activity that gets launched when clicked on said button

inside mainactivity.kt

val remoteViews: RemoteViews = RemoteViews("com.example.noteswidget", R.layout.widget)
remoteViews.setTextViewText(R.id.appwidget_Button,it.text)
inside widget.xml
<Button
        android:id="@+id/appwidget_Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top"
        android:text="@string/appwidget_text" /> 

i am very new to android, i may have made some noob mistakes


Solution

  • Based on your code i don't know why your logic like that. I'll give you sample code. activity_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"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/text_print"
            android:layout_width="match_parent"
            android:layout_marginHorizontal="16dp"
            android:layout_marginTop="16dp"
            android:textSize="16sp"
            android:layout_height="wrap_content"
            tools:text="Sample"/>
    
        <Button
            android:id="@+id/btn_click"
            android:layout_width="match_parent"
            android:layout_marginTop="16dp"
            android:layout_height="wrap_content"
            android:text="Click it"/>
    
    </LinearLayout>
    

    Then in MainActivity.kt

    class MainActivity : AppCompatActivity() {
    
        //first init
        private lateinit var btnClick: Button
        private lateinit var textPrint: TextView
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            enableEdgeToEdge()
            setContentView(R.layout.activity_main)
            ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            }
    
            //assign variable with value
            btnClick = findViewById<Button>(R.id.btn_click)
            textPrint = findViewById<TextView>(R.id.text_print)
    
            //do actions
            btnClick.setOnClickListener {
                textPrint.text = btnClick.text
            }
    
        }
    }
    

    In the sample above, there are several types if you used findViewById

    1. Create name of the variable your component
    2. Binding the variable with the xml files using findViewById
    3. Do the actions

    In that sample when button is clicked, so the textview will show the text in the button.

    But, my recommendations you can learn about ViewBinding to better development. Because that is very helpful for you, and make your work more efficient. Happy to code :)