String array to show text at textview on refresh - android

Instead of Text, it's necessary to read the R.id.text text from TextView = My Text, I tried a lot but failed, how, please help.
This is the line:
public class MyWidgetProvider extends AppWidgetProvider {
// String array to show text at textview on refresh
private static final String[] refreshStrings = {"Text"};
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="My Text"
android:textColor="#android:color/white"
android:textSize="18sp" />
I want it to be like this:
private static final String[] refreshStrings = R.id.text;

I found this How to find view id in appwidgetprovider?, so it is not possible to reference or modify in Appwidget directly. One solution could be to use AppCompatActivity. Here you can see how to update and modify with AppCompatActivity http://android4beginners.com/2013/06/lesson-1-3-how-to-modify-textview-in-java-code-findviewbyid-settext-and-gettext-methods/.
This is my code with AppCompatActivity how to get the text from the TextView by the R.id.<idname>
MainActivity:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.text);
}
}
activity_main.xml (Don't forget to change the standard Layout, I chose LinearLayout):
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="My Text"
android:textColor="#android:color/white"
android:textSize="18sp" />

Related

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

button onclick from a layout file

so I was working on some basics of android when I stumbled upon this. I am trying to implement a listview(having its layout stored in file eachrow.XML)and a button on my app such that when the user clicks one button "schoollife", the listview automatically imports layout stored from other file "eachrow2.XML".
the layout of eachrow.xml consists of buttons only, while the layout of eachrow2.xml consists of buttons.
Everything seems to work fine, the user clicking the button imports the other layout , but the problem starts when user tries to click buttons from the second layout. in the error logs, it is stating that the button 'subject' causing a null pointer exception
eachrow.xml:
eachrow2.xml:
===========codes========
eachrow2.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/subjectbutton"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="1dp"
android:layout_margin="1dp"
android:textAllCaps="false"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/marksbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="click the button to show marks of each subject"
android:textAlignment="center"
/>
</LinearLayout>
mainactivity.java:
package com.example.ansh.reportcard;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView l;
private String[] d_myself;
private ArrayAdapter adp;
private HashMap<String,String> d_school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.a_list);
d_myself= new String[]{"something"
};
d_school=new HashMap<>();
d_school.put("A","B");
Button B_myself= (Button)findViewById(R.id.myself);
B_myself.setOnClickListener(this);
Button B_school=(Button)findViewById(R.id.school);
B_school.setOnClickListener(this);
Button subject=(Button)findViewById(R.id.subjectbutton);
subject.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.myself){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow,R.id.textView,d_myself);
l.setAdapter(adp);
}
if (view.getId()==R.id.school){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow2,R.id.subjectbutton,d_school.keySet().toArray());
l.setAdapter(adp);
}
if (view.getId()==R.id.subjectbutton){
Button tmp=(Button)view;
CharSequence x=tmp.getText();
TextView t= (TextView) findViewById(R.id.marksbox);
t.setText(d_school.get(x.toString()));
}
}
}
According to my understanding to your question, You declaring the Button from the xml file activity_main.xml, Instead you should find it from the eachrow2.xml file.
The subjectbutton is in the eachrow2.xml so you need to find the view from this file only instead activity_main.xml.

New to coding, app keeps crashing on start up

I'm trying to have a text field and a text view, and set the text view text to whatever is on the the field.
This is my code:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView helloMessage = (TextView) findViewById(R.id.hello); //define textView to show the message
EditText nameField = (EditText) findViewById(R.id.Name); // Text field to get the name from
String getName = nameField.getText().toString(); // set a variable with the text field's value
helloMessage.setText(getName); // set text on the hello textview to be the name.
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hello"/>
</LinearLayout>
Thanks in advance for any sort of help!
Brother you need to setContentView(R.layout.activity_main); before finding any other view
You forgot to add setContentView(R.layout.activity_main) to onCreate()

Show String from EditText

I want to show TextView editable like the app "Google Keep" but
EditText text = (EditText)findViewById(R.id.edittext);
String value = text.getText().toString();
didn't work
Hello I've made a example that I think you can use:
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#FF0000"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/getInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET INFO"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
MainActivity
package com.example.testedittext;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView info;
private EditText input;
private Button getInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (TextView) findViewById(R.id.textView);
input = (EditText)findViewById(R.id.editText);
getInfo = (Button)findViewById(R.id.getInfoButton);
getInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String inputText = input.getText().toString();
info.setText(inputText);
}
});
}
}
Here is the output:
Hope this helps,
Cheers
That is the correct syntax for getting the text in string format of an edittext.
The value of String "value" is actually "" right now because you called
text.getText().toString();
IMMEDIATELY after EditText text was instantiated. As you can imagine, the moment it was created, there was no text inside it, so that's why "value" has an empty string.
If you want to retrieve the value of the edittext at a specific call, I'd recommend adding a button in your xml layout, and in your code, add this:
Button button = (Button) findViewById(R.id.somebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.getText().toString();
}
});
This will get the current String value of the edittext when you click on the button.
You have to use the value String somewhere else it will tell you that its not used.
Then you have to use that piece of code at a point of time where you have had a change to set the value of that ediettext.

Setting table data in an activity - will this approach work

I have a simple table layout that I want to populate just by initialising a specific type
of object in my onCreate method. First off this is the layout I have
jobs_table.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/Jobs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="#+id/JobRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="#+id/JobID" android:layout_column="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#color/black"></TextView>
<TextView android:id="#+id/JobStatus" android:layout_column="2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#color/black"></TextView>
<TextView android:id="#+id/Customer" android:layout_column="3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#color/black"></TextView>
<TextView android:id="#+id/Department" android:layout_column="4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#color/black"></TextView>
<TextView android:id="#+id/DocType" android:layout_column="5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#color/black"></TextView>
</TableRow>
</TableLayout>
And the activity
package com.producermobile;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import com.producermobile.jobs.ProducerJobRows;
public class JobRowsActivity extends Activity {
private BatchingJobRows jobRows;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jobs_table);
jobRows = new BatchingJobRows(this);
}
}
Now the idea will be when I create my jobRows the data for the table will be loaded by the BatchingJobRows object, which should hopefully then be available to my activity. I don't have a way of checking this at the mo though so I'm not sure if the concept is right (my main worry is the while loop which is adding rows to the table).
Anyway here's a simplified version of what's happening in the BatchingJobRows.
package com.producermobile.jobs;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.producermobile.R;
public class BatchingJobRows {
private TableLayout table;
private View tableView;
private Context context;
private HashMap<String,ArrayList<String>> jobs;
private ArrayList<String> jobsVals;
public BatchingJobRows(Context context) {
this.context = context;
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tableView = inflater.inflate(R.layout.jobs_table, null);
table = (TableLayout) tableView.findViewById(R.id.Jobs);
jobVals = new ArrayList<String>();
jobVals.add("HELD");
jobVals.add("DELL");
jobVals.add("PRINT");
jobVals.add("STATEMENT");
jobs = new HashMap<String,ArrayList<String>>();
jobs.put("012345",jobVals);
jobs.put("123456",jobVals);
this.setRows();
}
public void setRows() {
Iterator<String> keys = jobs.keySet().iterator();
while(keys.hasNext()) {
//Init Objects
TableRow row = (TableRow) TableView.findViewById(R.id.JobRow);
TextView jobid = (TextView) tableView.findViewById(R.id.JobID);
TextView jobstatus = (TextView) tableView.findViewById(R.id.JobStatus);
TextView customer = (TextView) tableView.findViewById(R.id.Customer);
TextView department = (TextView) tableView.findViewById(R.id.Department);
TextView doctype = (TextView) tableView.findViewById(R.id.DocType);
String key = keys.next();
jobid.setText(key);
jobstatus.setText(jobs.get(key).get(0));
customer.setText(jobs.get(key).get(1));
department.setText(jobs.get(key).get(2));
doctype.setText(jobs.get(key).get(3));
row.addView(jobid);
row.addView(jobstatus);
row.addView(customer);
row.addView(department);
row.addView(doctype);
table.addView(row);
}
}
}
Well this is not a direct answer to your question but what I think you should do is create a list activity, and move this functionality down into a ListAdapter of some type. We can help you with question on how to set that up. Its probably easier than the route you are going, and more familiar to people on this board. So i would recommend taking the conventional approach on this. Use ListActivity, and ListAdapter Base or Cursor depending on your needs.

Categories

Resources