skip to Main Content

I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.

But when I text, Its don’t work.

Here is my code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);

        Button bubble = (Button) findViewById(R.id.start_bubble);
        bubble.setOnClickListener(this);// calling onClick() method

        Button predict = (Button) bubbleView.findViewById(R.id.predict);
        predict.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_bubble:
                startService(new Intent(getApplicationContext(), SimpleService.class));
            case R.id.predict:
                View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
                TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
                predict_text.setText("Hi"); // <--- It don't work :(
            default:
                break;
        }
    }
}
[EDIT] [] Add some .XML file
Here is my 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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.siddharthks.sampleapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Project KHKT"
        android:textColor="#AA000000"
        android:textSize="21sp" />

    <Button
        android:id="@+id/start_bubble"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Paste and Predict"
        android:textSize="18sp" />
</LinearLayout>

and here is my bubble_view.xml, its just for a

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="16dp"
            android:paddingTop="16dp"
            android:paddingEnd="16dp"
            android:text="Project KHKT"
            android:textColor="#AA000000"
            android:textSize="21sp" />

        <Button
            android:id="@+id/predict"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Paste and Predict"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/predict_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="16dp"
            android:paddingTop="16dp"
            android:paddingEnd="16dp"
            android:textColor="#AA000000"
            android:textSize="21sp" />


    </LinearLayout>
</ScrollView>

Do you have any suggested for me ?

4

Answers


  1. Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.

    public class MainActivity extends AppCompatActivity  {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    LinearLayout parent = findViewById(R.id.activity_main);        //parent layout.
            View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
                
            parent.addView(childView);
           
    
    
            Button predict = (Button) childView.findViewById(R.id.predict);
    
    
            TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
    
            
    
            predict.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    predict_text.setText("Hi"); // <--- It don't work :(
                }
            });
    }
    }
    
    Login or Signup to reply.
  2. Add break to the each case statement in the switch.

    Login or Signup to reply.
  3. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        TextView predict_text;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button start = (Button) findViewById(R.id.start_bubble);
    
            LinearLayout parent = findViewById(R.id.activity_main);  
            View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
            parent.addView(childView);
            Button predict = (Button) childView.findViewById(R.id.predict);
            predict_text = (TextView) childView.findViewById(R.id.predict_text);
    
            predict.setOnClickListener(this);
            start.setOnClickListener(this);
    
    
    
        }
    
        @Override
        public void onClick(View view) {
    
            switch (view.getId()){
                case R.id.predict:
                    predict_text.setText("Hi"); 
                    break;
            }
        }
    }
    
    Login or Signup to reply.
  4. I’m not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.

    Fist answer, Assuming you want everything inside the activity.

        public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button bubble;
        private Button predict;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initUIViews() // Initialze UI Views
            initUIActions() // Initialize Ui Actions 
        }
    
        private void initUiViews() {    
            Button bubble = (Button) findViewById(R.id.start_bubble);
            Button predict = (Button) bubbleView.findViewById(R.id.predict);
        }
    
         private void initUIActions() {
            bubble.setOnClickListener(this);// calling onClick() method 
            predict.setOnClickListener(this);
         }    
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.start_bubble:
                    startService(new Intent(getApplicationContext(), SimpleService.class)); 
                    break;
                case R.id.predict:
                    predict_text.setText("Hi");  
                    break;
                default:
                    break;
            }
        }
    }
    

    and restructure your XML layout as follow. There are few ways to restructure these layouts, I’ll write the easiest way, but note that this is not the optimal way.

    <?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/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        tools:context="com.siddharthks.sampleapp.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Project KHKT"
            android:textColor="#AA000000"
            android:textSize="21sp" />
    
        <Button
            android:id="@+id/start_bubble"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Paste and Predict"
            android:textSize="18sp" />
    
        <!-- include bubble layout file -->
        <include layout="@layout/bubble_view.xml" />
    
    </LinearLayout>
    

    Other than the include tag you can add the whole code inside to the Activity layout.

    The second answer, Assuming you want activity and Service with a bubble view.

    If you are looking for a bubble view, You have to create a Bubble service.

    Check this answer: Bubble Example
    Official Doc: Android Bubble

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search