MyFirstApp android tutorial error on DisplayMessageActivity.java - android

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

Related

TextView auto create new line when end of the line and edittex not align with textview

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

android creating text view dynamically

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.

TextView does not show the message

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

Sending data to other activity and display some additional TextViews

I have an Android application in which i send data from one activity to another. I can do that part easily but the problem is i want to display some additional TextViews as well. So when i display the sent data using setContentView i can see only the sent data. I cannot see the additional TextViews. I want both of them to be displayed simultaneously.
I searched and tried hard to find a solution without any luck.I am attaching the code for reference.Thanks!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
TextView textViewci= new TextView(this);
textViewci.setText(message);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
When i use this code,i am unable to see anything and the activity remains blank.Actually i am using a TableLayout here so that i can display data in form of tables
You have to add text view on your layout.
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
You have to add the text view in the parentview. For example, if you have used the text view in the linear layout then add the text view in the linear layout and then use the addview(linearlayout).
Below is the example
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
I have just make a test program for you. first class code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondClass.class);
intent.putExtra("firstValue", "1");
intent.putExtra("secondValue", "2");
startActivity(intent);
}
});
Second class code :
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message1 = intent.getStringExtra("firstValue");
String message2 = intent.getStringExtra("secondValue");
RelativeLayout layout = new RelativeLayout(this);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
TextView textViewci= new TextView(this);
textViewci.setText(message1+""+message2);
Log.e("intent value is", ""+message1+""+message2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
layout.addView(textView1);
layout.addView(textView2);
addContentView(layout, lp);
Try and let me know. YOu have to make related layouts for both classes.
Hope this will help
are you getting any error messages? if yes then please post them here. and the others are right.you need to add yours views to the parent layout.
I have found out the answer by myself.It's fairly easy and there is no need here to use views in the parent layout.I modified the textViews in my xml file and got their values using findViewById.I am attaching the updated code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
textView=(TextView)findViewById(R.id.textViewci);
textView1=(TextView)findViewById(R.id.textViewmd);
textView2=(TextView)findViewById(R.id.textViewso);
textView.setText(message);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
textView.setTypeface(tf);
}

Updating a textview in different layout xml from the main activity

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!

Categories

Resources