Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Programmatically setting button text

Tags:

android

button

Does anybody know how to programmatically set the text of a button?

thing is i'm not calling this from the main layout(setContentView) i'm calling it in a view thats inflated after an asynctask heres what i have tried but this is giving a null pointer exception on the 2nd line

Button mButton=(Button)findViewById(R.id.contact);
        mButton.setText("number");

heres my layout where i am calling the button

   <Button
       android:id="@+id/contact"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/address"
       android:layout_toLeftOf="@+id/badge"
       android:background="@drawable/ic_btn_call"
       android:textSize="10dp"
       android:textStyle="bold"
       android:textColor="@color/white"/>

here my view i'm inflating

ClubInfo = LayoutInflater.from(getBaseContext()).inflate(R.layout.clubinfocell,
                null);

        TextView TeamNameText = (TextView) ClubInfo.findViewById(R.id.TeamName);
        TeamNameText.setText(teamName);

        TextView AddressText = (TextView) ClubInfo.findViewById(R.id.address);
        AddressText.setText(address1);



        Button mButton=(Button)ClubInfo.findViewById(R.id.contact);
        mButton.setText(telephone);
like image 866
Luke Batley Avatar asked Jun 22 '12 10:06

Luke Batley


People also ask

How to declare a Button in Android studio?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How to change the background of a Button in Android studio?

Go to the layout folder and in the activity_main. xml file change the ConstraintLayout to LinearLayout and give its orientation vertical. Add the Button and Switch to the layout.


3 Answers

Then use your view's object to initialize it:

Button mButton = (Button)your_view_object.findViewById(R.id.contact);
mButton.setText("number");

When you try to identify a view other than your Activity's layout, you have to pass the reference of that view like this. If not Android will keep looking for this element from the layout which you provided in the setContentView().

For example, consider you have inflated a view like this:

View View = mInflater.inflate(R.layout.gridelement, null);

Then use this View's object for the Button present in that inflated layout:

  Button mButton = (Button)View.findViewById(R.id.contact);
like image 79
Andro Selva Avatar answered Oct 19 '22 16:10

Andro Selva


change your code as:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//set layout here in which u have add contact in xml
        Button mButton=(Button)findViewById(R.id.contact);
        mButton.setText("number");

EDIT: Your \res\layout\main.xml look like as:

<?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" >
<Button
       android:id="@+id/contact"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/address"
       android:layout_toLeftOf="@+id/badge"
       android:background="@drawable/ic_btn_call"
       android:textSize="10dp"
       android:textStyle="bold"
       android:textColor="@color/white"/>
</LinearLayout>
like image 37
ρяσѕρєя K Avatar answered Oct 19 '22 17:10

ρяσѕρєя K


your mButton is null.so NPE.are you refrenced xml resources after setContentView

onCreate(){
...
setContentView(R.layout.yourlayout);

Button mButton=(Button)findViewById(R.id.contact);
mButton.setText("number");

}
like image 1
Samir Mangroliya Avatar answered Oct 19 '22 17:10

Samir Mangroliya