1

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 ?

Bao Dinh
  • 64
  • 6
  • Layouts are not `View`s. They are blueprints for `View`s, and every time you `inflate()` one, it is a new, completely separate instance that has no relation to any other instance that you might have used elsewhere. Neither of the instances of `R.layout.bubble_view` you inflate in `MainActivity` is added to an on-screen hierarchy anywhere, so whichever instance you are seeing and interacting with (presumably one created in `SimpleService`) is not going to be affected by the code in `MainActivity`. – Mike M. Dec 22 '21 at 03:36
  • if you can share xml also then it will be quite easy to find out the actual issue. – bhaskarkh Dec 22 '21 at 03:43
  • Thanks for your explanation, Mike. btw do you have any suggestions to fix it? – Bao Dinh Dec 22 '21 at 03:45
  • case R.id.predict: predict_text.setText("Hi"); // <--- It don't work :( try this – bhaskarkh Dec 22 '21 at 03:46
  • Ok i will edit and add some xml file in the post ,bhaskarkh – Bao Dinh Dec 22 '21 at 03:47
  • Cannot resolve symbol 'predict_text'. I already tried,and I got this error @bhaskarkh – Bao Dinh Dec 22 '21 at 03:48
  • I edited the post. Can u see my XML code ? – Bao Dinh Dec 22 '21 at 03:55
  • Well, I'm not really sure what your overall design is. I'm assuming that you have a `View bubbleView` added to `WindowManager` in `SimpleService`. If so, that's the one that you need to use with `bubbleView.findViewById()`. The simple fix is to basically just move all of that code to your `SimpleService` class. If you need to somehow pass or relay data from `MainActivity` to that `SimpleService`, that would probably be the next thing you want to investigate for your design. As it is now,`bubble_view` really has no need to be in `MainActivity` anywhere. – Mike M. Dec 22 '21 at 04:00
  • I'm designing a bubble floating like messenger. And do u know how to get id from a different layout? – Bao Dinh Dec 22 '21 at 04:05
  • It's not clear exactly what that means. If you're trying to using `findViewById()` from `MainActivity` to get at a `bubble_view` created in `SimpleService`, it's not going to work like that. As mentioned, that's something you'll need to investigate and research yourself, because there are multiple different ways to do it, so I can't blindly recommend one way over another. If that is what you're trying to do, are you sure that `MainActivity` even needs to know about `bubbleView`? Can't you just pass the needed data around? Why does `MainActivity` have to know about a completely separate `View`? – Mike M. Dec 22 '21 at 04:11

4 Answers4

1

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

KZoNE
  • 1,249
  • 1
  • 16
  • 27
0

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 :(
            }
        });
}
}
bhaskarkh
  • 179
  • 2
  • 11
0

Add break to the each case statement in the switch.

Dada
  • 6,313
  • 7
  • 24
  • 43
0
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;
        }
    }
}