1

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .

// Button activity page
android:layout_height="match_parent">

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

    <Button
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="50dp"
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:text="@string/OKILA"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:layout_marginRight="50dp"
        android:text="1"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>
//strings.xml
    <string-array name="chapters">
        <item>this is tes1</item>
        <item>this is test2</item>
    </string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class Lipok extends AppCompatActivity implements View.OnClickListener {
    Toolbar mActionBarToolbar;
    Button btnOne;
    Button btnTwo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipok);
        mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setTitle("CHAPTERS");
    String[] chapters=getResources().getStringArray(R.array.chapters);

        btnOne = findViewById(R.id.btn1);
        btnTwo = findViewById(R.id.button1);        

        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String text="";
        switch(v.getId()){
            case R.id.btn1 : {
                text = getResources().getStringArray(R.array.chapters)[0];
                break;
            }
            case R.id.button1 : {
                text = getResources().getStringArray(R.array.chapters)[1];
                break;
            }
        }


        Intent intent = new Intent(Lipok.this,lipokchapters.class);
        intent.putExtra("DATA", text);
        startActivity(intent);
    }
}

//textview activity page
package com.Aolai.temeshilai;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class lipokchapters extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipokchapters);
        textView=findViewById(R.id.textv);
        String text= getIntent().getStringExtra("Data");
        textView.setText(text);

    }
}
13
  • So the textview that you want to display in is in different activity? Commented Feb 19, 2020 at 5:28
  • yes diff activity.I tried your code below but there is a problem here"Text.setText(text);" setText cannot resolve method. Commented Feb 19, 2020 at 6:43
  • What about the other activity? Commented Feb 19, 2020 at 10:53
  • i have updated please check. Commented Feb 19, 2020 at 11:19
  • And what is the error? Commented Feb 19, 2020 at 11:20

2 Answers 2

2

Try this :

xml file

android:layout_height="match_parent">


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

    <Button
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="50dp"
        android:id="@+id/OKILA"
        android:layout_width="wrap_content"
        android:text="@string/OKILA"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:layout_marginRight="50dp"
        android:text="1"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

strings.xml

    <string-array name="chapters">
        <item>this is tes1</item>
        <item>this is test2</item>
    </string-array>

Main Class

public class Lipok extends AppCompatActivity implements View.OnClickListener {
    Toolbar mActionBarToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipok);
        mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setTitle("CHAPTERS");

    }

    @Override
    public void onClick(View v) {
         String text="";
         switch(v.getId()){
              case R.id.OKILA : {
                   text = getResources().getStringArray(R.array.testArray)[0];
                   break;
              }
              case R.id.button1 : {
                   text = getResources().getStringArray(R.array.testArray)[1];
                   break;
              }
         }
         Toast.makeText(this, text, Toast.LENGTH_LONG).show();

             Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
             intent.putExtra("DATA", text);
             startActivity(intent);

         // Or else you can do whatever you want to do here with that text
    }
}

Then in your other activity

String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
Sign up to request clarification or add additional context in comments.

15 Comments

my textview is on another activity so I need to link to that activity too
You can't link it like that, you will have to send data to that screen with intent and then setText there @newbie77
sorry to bother you but how to i send data to that screen with intent.Cause your above code isnt working.I think i have to write somecode on my other textview activity as well
"your_textview_name.setText(text)" here when i replace it with the name of my textview it doesnt accept.Maybe since its on another activity.I havent wrote any code on my textview activity.
For sending data to other screen, follow this link : stackoverflow.com/questions/2091465/…
|
0

This is basics of Android so before answering I suggest you to go through basic tutorials before posting on Stackoberflow.

Now,

Here is how you can access the items from your string_array.

String[] chapters= getResources().getStringArray(R.array.chapters);

Now,here is pseudo code that might help you.

btn1.setOnClickListener(

your_text_view.setText(chapters[0])

)
//Here 0 means your first item, likewise you can access items by their index in 
 array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.