newbie77
newbie77

Reputation: 35

Access string_array from activity and send selected item to another activity in android

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);

    }
}

Upvotes: 1

Views: 111

Answers (2)

Karan Mehta
Karan Mehta

Reputation: 1541

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);

Upvotes: 2

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

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

Upvotes: 0

Related Questions