Android How do display array contents to textview? - android

How do I loop around contents of an array and display line by line on a text view
java code
package com.stuff;
import android.app.Activity;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ScrumActivity extends Activity
{
TextView textout;
EditText inputBox;
Button okButton;
ProgressBar progressBar;
Scrumer scrumbler = new Scrumer();
StringBuilder sb = new StringBuilder();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get elements from XML to use in application
textout = (TextView) findViewById(R.id.textView1);
inputBox = (EditText) findViewById(R.id.inputBox);
okButton = (Button) findViewById(R.id.button1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//progressBar.setVisibility(View.VISIBLE);
SpannableStringBuilder sb = new SpannableStringBuilder();
String regularText = "This text is ";
String clickableText = "clickable";
sb.append(regularText);
sb.append(clickableText);
sb.setSpan(new ClickableSpan(), sb.length()-clickableText.length(), sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//tv.setText(sb);
//setting a listener for the button to be clicked
okButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//textout.setText(inputBox.getText().toString());
scrumer.jumble(inputBox.getText().toString());
for(String s : scrumbler.getMatchArray())
sb.append(s+"\n");
textout.setText("ok");
System.out.print(sb.toString());
}
});
}
}
Heres my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<EditText
android:id="#+id/inputBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search" />
<TextView
android:id="#+id/textView1"
android:layout_width="184dp"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

StringBuilder builder = new StringBuilder();
String[] arr = {"These","are","some","words"};
for (String s : arr){
builder.append(s).append(" ");
textview.setText(builder.toString());
Will put These are some words into your TextView. If you want a line-break after every single string, use builder.append(s).append("\n");

Related

How to set a line break after a set of EditText which have been dynamically created?

I am trying to show few EditText field just like n*n Matrix. I am creating these EditText field dynamically.
Problem is all my EditText field prints in the same line horizontally.
I wanted to know is there anything like "System.out.println();" in Android Studio!
Note: I want this line break in between Views, not in between Text, so \n doesn't work here!
This is my activity_main.xml file,
<?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/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity">
<EditText
android:id="#+id/a"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:gravity="center"
android:inputType="number" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/holo_purple"
android:text="Click"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/child_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<!--EditText Fields going to be here!-->
</LinearLayout>
</LinearLayout>
And Here's my MainActivity.java file
package com.ridi.magicfield;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends AppCompatActivity {
private static int a;
private static EditText magicField, ainput;
private static Button btn;
private static LinearLayout child_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
child_layout = (LinearLayout) findViewById(R.id.child_layout);
ainput = (EditText) findViewById(R.id.a);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = Integer.parseInt(ainput.getText().toString());
child_layout.removeAllViews();
createMatrixInputField();
}
});
}
private void createMatrixInputField() {
int p = 1;
for (int t = 1; t <= a; t++) {
for (int i = 1; i <= a; i++) {
magicField = new EditText(MainActivity.this);
magicField.setId((p));
magicField.setHint("et-" + (Integer.toString(p)));
magicField.setGravity(Gravity.CENTER);
child_layout.addView(magicField);
p++;
}
}
}
}
Here's my output screen. I want et-3 and et-4 to go to next line.
Is there anyone who can help please...
Output Screen Image
Use TableLayout instead of LinearLayout for n*n matrix pattern
for (int i = 0; i < noOfRows; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT
,TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
for(int j = 0; j < noOfColumns; j++) {
EditText et = new EditText(this);
//Extra properties goes here...
et.setText("Your text");
row.addView(et,j);
}
tableLayout.addView(row,i);
}

How can store Data text view data?

In this file i have two Edit text and one textview.I want to sum two number.then result are show in text view.Then i want to store Textview data. please help.i am trying but i am fail.
Activity_main.xml//
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
android:onClick="add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
android:onClick="sub"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingBottom="30dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pr"
android:text="Previoues Data"
/>
</LinearLayout>
</LinearLayout>
the java file is
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText Edit,Edit1;
Button add, sub, pr;
TextView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit = (EditText) findViewById(R.id.Edit);
Edit1 = (EditText) findViewById(R.id.Edit1);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
pr = (Button) findViewById(R.id.pr);
view = (TextView) findViewById(R.id.view);
Edit.setText("0");
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 + value2;
view.setText(Integer.toString(result));
// Edit.setText(Integer.toString(result));
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("value",result);
editor.commit();
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 - value2;
view.setText(Integer.toString(result));
Edit.setText(Integer.toString(result));
}
});
pr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
// int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
String v=sharedPreferences.getInt("value","");
view.setText(v);
}
});
}
}
update your xml file remove onClick from your button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
/>
Problem is with your getting value from sharedPreference
String v=sharedPreferences.getInt("value","");
why are you passing string as default value and your
try this String
v = Integer.toString(sharedPreferences.getInt("value",0));
in the layout you´ve got android:onClick="add" there should be a public void add(View v) to handle the event. Do the sum inside that handler.
Please, follow the documentation
http://developer.android.com/reference/android/widget/Button.html and see the :onCLick handling.

show EditText on condition

Right now I have an idea for a project and I would like to know if anyone can help me on the same logic.
As such I need to create or generate a number of EditText according to amount you enter, ie, to select or enter a number such as 5, show me 5 EditText layout for type 5 values​​. They know that the form could accomplish this? Any ideas please?
I guess it must be a way to do it with a loop, but not like carrying this calculation Java to XML. Thank you.
Here is an example of generating EditText items based on the number you enter. You can ignore the scrollview in the layout file I just put it in case someone added a lot of EditText items that would go off the screen.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText count = (EditText) findViewById(R.id.count);
final TextView output = (TextView) findViewById(R.id.output);
final Button generate = (Button) findViewById(R.id.generate);
final Button values = (Button) findViewById(R.id.values);
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int numberOfControlsToGenerate = 0;
try {
numberOfControlsToGenerate = Integer.parseInt(count.getText().toString().trim());
} catch (NumberFormatException e) {
Log.e(TAG, e.getMessage(), e);
}
if (numberOfControlsToGenerate > 0) {
if (container.getChildCount() > 0) {
container.removeAllViews();
}
for (int counter = 0; counter < numberOfControlsToGenerate; counter++) {
addEditText(container);
}
}
}
});
values.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] editTextValues = new String[container.getChildCount()];
StringBuilder editTextValuesBuilder = new StringBuilder();
for (int counter = 0; counter < container.getChildCount(); counter++) {
EditText child = (EditText) container.getChildAt(counter);
editTextValues[counter] = child.getText().toString().trim();
editTextValuesBuilder.append(editTextValues[counter]).append("\n");
}
output.setText(editTextValuesBuilder.toString());
}
});
}
private void addEditText(LinearLayout container) {
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
EditText editTextToAdd = new EditText(this);
editTextToAdd.setLayoutParams(params);
container.addView(editTextToAdd);
}
}
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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp"
tools:context="${packageName}.${activityClass}" >
<EditText
android:id="#+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:textSize="20sp" />
<Button
android:id="#+id/generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
<Button
android:id="#+id/values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Values" />
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>

trouble opening xml page

I created two java class and two xml pages . I am trying to when onclick button of one class,open another class. afer opening , memo.xml page showing blank.
see following code.
please help me
My login.java page:
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity{
SQLiteDatabase db;
int z,i=0;
String[] arr = new String[3];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = openOrCreateDatabase("mydatabase.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Login");
d1.setContentView(R.layout.login);
d1.show();
Button registerques1 = (Button) d1.findViewById(R.id.registerques1);
EditText ques = (EditText) d1.findViewById(R.id.question);
Cursor cur= db.rawQuery("select * from questions1",null);
cur.moveToNext();
System.out.println(cur.getString(0));
System.out.println(cur.getString(1));
ques.append(cur.getString(0));
Cursor c = db.rawQuery("SELECT * from questions1", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("question"));
String Name = c.getString(c.getColumnIndex("answer"));
arr[i]=firstName;
i++;
}while (c.moveToNext());
}
}
for(z=0;z<2;z++)
{
registerques1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if(z == 2)
{
Cursor cur= db.rawQuery("select * from questions1",null);
cur.moveToNext();
EditText ques = (EditText) d1.findViewById(R.id.question);
EditText ans = (EditText) d1.findViewById(R.id.answer);
Editable newTxt=(Editable)ans.getText();
String str = newTxt.toString();
int l=str.length();
int l1=cur.getString(1).length();
int l2=l-l1;
ques.setText(arr[0]);
ans.setText("");
if(l2 == 0)
{
z=5;
}
}
if(z == 5)
{
EditText ques = (EditText) d1.findViewById(R.id.question);
ques.setText(arr[1]);
Cursor cur= db.rawQuery("select * from questions1 where question='"+arr[1]+"' ",null);
cur.moveToNext();
//ques.setText();
EditText ans = (EditText) d1.findViewById(R.id.answer);
System.out.println("------"+ans.getText());
Editable newTxt=(Editable)ans.getText();
String str = newTxt.toString();
int l=str.length();
int l1=cur.getString(1).length();
int l2=l-l1;
ques.setText(arr[1]);
ans.setText("");
/*ques.setText(arr[2]);
ans.setText("");*/
if(l2 == 0)
{
/*Intent intent = new Intent(getBaseContext(), Memo.class);
startActivity(intent);*/
z=6;
}
}
if(z == 6)
{
EditText ques = (EditText) d1.findViewById(R.id.question);
ques.setText(arr[2]);
Cursor cur= db.rawQuery("select * from questions1 where question='"+arr[2]+"' ",null);
cur.moveToNext();
EditText ans = (EditText) d1.findViewById(R.id.answer);
System.out.println("------"+ans.getText());
Editable newTxt=(Editable)ans.getText();
String str = newTxt.toString();
int l=str.length();
int l1=cur.getString(1).length();
//System.out.println(l+l1);
int l2=l-l1;
ques.setText(arr[2]);
ans.setText("");
if(l2 == 0)
{
System.out.println("memo-----");
Intent intent = new Intent(getBaseContext(), Memo.class);
startActivity(intent);
}
}
}
});
}
}
}
login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<TextView android:text="Question1" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="#+id/question" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
<EditText android:id="#+id/answer" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
<Button android:id="#+id/registerques1" android:layout_width="wrap_content" android:text="registerques" android:layout_height="wrap_content"></Button>
</LinearLayout>
Memo.java
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class Memo extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Register Questions");
d1.setContentView(R.layout.memo);
Button view = (Button) d1.findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
/*Intent intent = new Intent(getBaseContext(), View.class);
startActivity(intent);*/
}
});
}
}
memo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<TextView android:text="Titile" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="#+id/question" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
<TextView android:text="Text" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:minLines="6" android:maxLines="10" android:id="#+id/answer" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
<TableLayout android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button android:id="#+id/add" android:layout_width="wrap_content" android:text="ADD" android:layout_height="wrap_content"></Button>
<Button android:id="#+id/view" android:layout_width="wrap_content" android:text="VIEW" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
You haven't called the show() method for your dialog.
Add d1.show() at the end of your onCreate() method

Want to add item in a list in android at runtime

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/speed"
android:inputType="number"></EditText>"
<TextView
android:id="#+id/TextViewSpeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="Speed"
android:layout_below="#+id/speed"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="AddValue"
android:id="#+id/AddValue"
>
</Button>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ListView"
android:layout_below="#+id/AddValue"
>
</ListView>
</RelativeLayout>
This is my layout code. I wanted to add a text data from EditText to the ListView which is on same page. How to write a code to add text to the list when I click on the AddValue Button.
Thanks in advance.
use this code
addbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String val = edittext.getText().toString();
list.add(val);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
}
});
Below snippet wil help you.
package org.sample;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class SampleActivity extends Activity
{
private Button add;
private EditText speedText;
private ArrayAdapter<String> adapter;
private ListView AddValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
AddValue=(ListView)findViewById(R.id.AddValue);
AddValue.setAdapter(adapter);
add = (Button) findViewById(R.id.add);
speedText = (EditText) findViewById(R.id.speed);
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if (speedText.getText().toString().length() != 0)
{
adapter.add(speedText.getText().toString());
adapter.notifyDataSetChanged();
}
}
});
}
}

Categories

Resources