So, Im trying to show how many characters has in the string that I'm receving for one entrance.
This is string is going to message. But the problem is, if I put this line
setContentView(R.layout.activity_display_message);
, It doesnt show the string and it shows the already set string. If I remove this line, the code works but only shows one textView, the second doesn't work.
Here is my code:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
setContentView(R.layout.activity_display_message);
//until here everything is working
TextView myTextView = (TextView) findViewById(R.id.mytextview);
myTextView.setText("My double value is ");
I have the ID mytextview on the xml file.
You only need
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
You can use textView.append(value); textView.append("\n") instead of inflating a layout
OR You only need
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView myTextView = (TextView) findViewById(R.id.mytextview);
myTextView2.append("My double value is ");
myTextView2.append("\n"); // new line
myTextView.append(message);
Assuming activity_display_message.xml has a textview with id mytextView
If you need another textview
TextView myTextView2 = (TextView) findViewById(R.id.mytextview2);
// need to have another textview with id mytextview2 in activity_display_message.xml
myTextView2.setText("My double value is ");
But instead you can use append with a single textview
You should paste both of your text views into layout xml:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_display_message);
//until here everything is working
TextView myTextView1 = (TextView) findViewById(R.id.mytextview1);
myTextView1.setText(message);
TextView myTextView2 = (TextView) findViewById(R.id.mytextview2);
myTextView2.setText("My double value is ");
Related
I have 4 TextView: " what is your name?", "in some circumstances" ,you might in some circumstances,you might", "________" and " to help user understand why your app needs a permission.For example " and a Edittext.
Now I want create a layout and when I user layout add view in my class ( not file.xml ) it can show same :
I used custom layout same here : Custom RowLayout and add view in code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
int permissionCheck =
rowLayout = (RowLayout) findViewById(R.id.row);
TextView tv1 = new TextView(this);
tv1.setText("what is your name?");
rowLayout.addView(tv1);
EditText ed1 = new EditText(this);
ed1.setWidth(50);
rowLayout.addView(ed1);
TextView tv2 = new TextView(this);
tv2.setText(" in some circumstances ,you might in some circumstances,you might");
rowLayout.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText("_____");
rowLayout.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText("to help user understand why your app needs a permission.For example");
rowLayout.addView(tv4);
<convert.htv.ex.convertdata.RowLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"></convert.htv.ex.convertdata.RowLayout>
and result
You can see two problems in here:
1. Edittext not in line with text view.
2. when string text length it create a new line. I want a part off textview will show in old line and when end of the line it will show the rest of textview in newline ( sample picture 1).
How I can do it? please help me.
1)Answer for first problem,
add this line of code
tv1.setGravity(Gravity.CENTER)
2) Change this
ed1.setWidth(50);
to
ed1.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
I'm new to android. just playing with the basic things. here's my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String status = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(status);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
</LinearLayout>
when i run this on my phone it says, "unfortunately, the app has stopped"
Change the order like
setContentView(R.layout.activity_display_message);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
You must setContentView(...) first and then initialize your Views.
You can add text view dynamically in other way also :
Create method inside your activity :
private TextView getCustomTextView(Context context, String tvValue, String tvHint) {
TextView textView = new TextView(context);
textView.setText("" + tvValue);
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setTextSize(20);
// to set font family
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
textView.setTypeface(face);
textView.setHint(tvHint+""); // static
textView.setHint(context.getString(R.string.text_hint)); // from string file
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return textView;
}
Add text view in linearlayout.
linearLayout.addView(getCustomTextView(this, "text value in string", "hint value in string"));
You can more then one text view using same code.
In my MainActivity I have the following
Intent myintent=new Intent(MainActivity.this, DisplayInformation.class);
myintent.putExtra("displayName",userName.getText().toString());
myintent.putExtra("displayContact",userContact.getText().toString());
myintent.putExtra("displayAddress",userAddress.getText().toString());
myintent.putExtra("displayStore",userStore.getSelectedItem().toString());
myintent.putExtra("displayRequest",userRequest.getText().toString());
startActivity(myintent);
I can display it correctly in my SecondActivity and here's the code
TextView textView1 = (TextView)findViewById(R.id.textView1);
TextView textView2 = (TextView)findViewById(R.id.textView2);
TextView textView3 = (TextView)findViewById(R.id.textView3);
TextView textView4 = (TextView)findViewById(R.id.textView4);
TextView textView5 = (TextView)findViewById(R.id.textView5);
textView1.setText(getIntent().getStringExtra("displayName"));
textView2.setText(getIntent().getStringExtra("displayContact"));
textView3.setText(getIntent().getStringExtra("displayAddress"));
textView4.setText(getIntent().getStringExtra("displayStore"));
textView5.setText(getIntent().getStringExtra("displayRequest"));
Intent myintent=new Intent(DisplayInformation.this, Confirmation.class);
myintent.putExtra("confirmID",smsCode.getText().toString());
startActivity(myintent);
Problem is when I use it in my ThirdActivity, it doesn't work.
TextView textView2 = (TextView)findViewById(R.id.confirmName);
TextView textView3 = (TextView)findViewById(R.id.confirmContact);
TextView textView4 = (TextView)findViewById(R.id.confirmAddress);
TextView textView5 = (TextView)findViewById(R.id.confirmStore);
TextView textView6 = (TextView)findViewById(R.id.confirmRequest);
textView2.setText(getIntent().getStringExtra("displayName"));
textView3.setText(getIntent().getStringExtra("displayContact"));
textView4.setText(getIntent().getStringExtra("displayAddress"));
textView5.setText(getIntent().getStringExtra("displayStore"));
textView6.setText(getIntent().getStringExtra("displayRequest"));
There's no error or anything. Just that it doesn't show the value in ThirdActivity. Any idea how to go about ? I want to display the same info in ThirdActivity.
You have to put all the data again in the intent before sending it to Third activity.
Intent myintent = new Intent(DisplayInformation.this, Confirmation.class);
myintent.putExtra("confirmID", smsCode.getText().toString());
myintent.putExtra("displayName", textView1.getText().toString());
myintent.putExtra("displayContact", textView2.getText().toString());
myintent.putExtra("displayAddress", textView3.getText().toString());
myintent.putExtra("displayStore", textView4.getSelectedItem().toString());
myintent.putExtra("displayRequest", textView5.getText().toString());
startActivity(myintent);
It doesn't show anything because the second Intent doesn't contain the values your are trying to get. Actually, you are putting only the "confirmID" value in your second Intent. If you want also the other values you have to set them even in the second Intent, in the same way you have done with the first one.
I been following it and i am stuck on 3 textview errors
tutorial:http://developer.android.com/training/basics/firstapp/starting-activity.html
I get 2 error saying textView cannot be resolved and one saying textView cannot be resolved as a variable. help!
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textview = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
`
You've declared it as textview (lowercase "v") but are referencing it as textView (uppercase "v"). Pick one!
TextView textView = new TextView(this); // Change the "v" to uppercase
textView.setTextSize(40);
textView.setText(message);
Variables in Java are case sensitive, so textView and textview are different. Change your code to this:
TextView textview = new TextView(this);
textview.setTextSize(40);
textview.setText(message);
// Set the text view as the activity layout
setContentView(textview);
I've created a new .xml file in my layout folder called log.xml. It only contains one TextView.
Is it possible to set the text on the textview located in the log.xml from my main activity? Or can it only be set when in an activity which uses the log.xml as view? Hope you get what i mean here, otherwise ill elaborate.
Thanks
If you don't set the xml you are talking about on "setContentView()" you can always get it with layout inflater. You'll have to add the tv to the current layout using addView() though.
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.log, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.tv); //get a reference to the textview on the log.xml file.
Unless log.xml is included in the current, visible layout findViewById() will return null.
Since you want to set the TextView's text when you load it in a new activity, you can pass the new String in the Intent used to start your Activity.
In the appropriate onClick() from your First Activity:
Intent intent = new Intent(this, Second.class);
intent.putExtra("myTextViewString", textString);
startActivity(intent);
In your Second Activity's onCreate():
setContentView(R.layout.log);
TextView textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if(extras != null) {
String newText = extras.getString("myTextViewString");
if(newText != null) {
textView.setText(newText);
}
}
Below solution worked for me -
Get View object of layout XML file (ex. toast_loading_data) -
View layout = inflater.inflate(R.layout.toast_loading_data,
(ViewGroup) findViewById(R.id.toast_layout_root));
Get the TextView element from this View (ex. TextView id - toast_text)-
TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
Set text of the TextView -
tvToast.setText("Loading data for " + strDate + " ...");
Below is snippet for customized Toast message from Main Activity -
View layout = inflater.inflate(R.layout.toast_loading_data,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
tvToast.setText("Loading data for " + strDate + " ...");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0); //Set toast gravity to bottom
toast.setDuration(Toast.LENGTH_LONG); //Set toast duration
toast.setView(layout); //Set the custom layout to Toast
Hope this helps
I think I understand what you are trying to say. If you do this:
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(output);
where textView2 is the id of the text view that you want to set the text of, you can set it to any string value using the setText() function. Hope this helps!