In my app which has tabbed activity, I am sending a simple message to textView and logcat (message indicating which tab I am selecting).
However, textView shows correct string but logcat shows different one.
Steps to reproduce this problem are very simple:
Start a new Android Studio project (My Android Studio version: 1.5.1)
Give any application name (e.g. MyApplication)
Keep only "Phone and Tablet" selected
Select "Tabbed Activity"
Choose Navigation Style "Action Bar Tabs (with View Pager)"
Click finish
This will generate and build project. If we run this application it will show string "Hello World from section: 1" on first tab, "Hello World from section: 2" on second and "Hello World from section: 3" on third tab.
Now, go in MainActivity.java and in method onCreateView (of class PlaceholderFragment) just below this line (where it send string to textView),
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
add this line
Log.i("onCreateView", "From tab number: " + getArguments().getInt(ARG_SECTION_NUMBER));
Expected behaviour:
textView and logcat both should display same message as we tap on tabs.
Actual behaviour:
textView shows correct strings. However in logcat, tab numbers shown are incorrect and hence not in consistent with textView output.
This is indeed weird and height of all is, instead of this when I tried forming string on my own and passing it to textView and LogCat
String strMessage = "Msg from tab number: " + getArguments().getInt(ARG_SECTION_NUMBER);
textView.setText (strMessage);
Log.i("onCreateView", strMessage);
It still has SAME behaviour i.e. shows correct strings in textView for relevant tabs but Logcat shows completely different tab numbers. Same string I am sending but only number changes ???
I found root cause of this on my own. Looks like textView.setText called in onCreateView sets the text value and it never changes afterwards. I wanted to find out which tab currently is active or visible so that I could update its contents. And I was under wrong impression that onCreateView gets called when tab is visible. Finally I found this answer which solved my problem.
Related
I am a newbie of Android Studio.
I have a problem with display the logs in my app.
For example:
String timeStamp = new
SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cycle);
TextView textView = (TextView) findViewById(R.id.cykleoncreate);
Log.d("[ " + timeStamp + "]", "[onCreate]");
I only want to display this log in my app. How can I do it?
you can display the logs by Toast or set in textView
Toast.makeText(getApplicationContext(),timeStamp,Toast.LENGTH_SHORT).show();
or
textView.setText(timeStamp);
What you have done is write but you can get confused as in where's the log, Log has tag and message, Tags are usually Activity or fragment name so that its easier for one to locate from where did a particular error or message came from, and you can include anything in the message part of it.
we have different types of logs this can be found here
You will find a logcat button on bottom side left side of android studio from there you can select depending on which type you want to see? in your case it's debug
Ok, it works well
textView.setText(timeStamp)
But I don't know why it doesn't work, when I want to build my string:
textView.setText(timeStamp + "my string")
Actually Log is not displaying in app , it will display in android studio . For displaying in app , we need to use Toast . your code is working fine you can check it in your android studio , as its shown in image We can display like this Log.d("onCreate", timeStamp); and search using TAG as onCreate.
I am building a text area in my APP using a TextInputLayout and its multiline property to let the users write more than 1 line, I am building it programatically (no xml)
TextInputLayout textInputLayout = new TextInputLayout(context);
textInputLayout.setLayoutParams(new TextInputLayout.LayoutParams(TextInputLayout.LayoutParams.MATCH_PARENT, TextInputLayout.LayoutParams.WRAP_CONTENT));
TextInputEditText field = new TextInputEditText(context);
field.setText(text);
field.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
field.setLayoutParams(new TextInputLayout.LayoutParams(TextInputLayout.LayoutParams.MATCH_PARENT,(int) (100 * dp)));
textInputLayout.addView(field);
myLinearLayout.addView(textInputLayout); // add text to layout
The code above makes my textarea, but is not showing the line break button, it shows a submit button instead.
This is the current result:
This is the expected result (from another app):
Which property I have to add to show the line break button like the second image?
How to get the keyboard to show a return key?
Try setting IMEOptions as None.
field.setImeOptions(EditorInfo.IME_ACTION_NONE);
Also check if the EditText has become SingleLine that also causes the done button to appear.
The PWA Web-push notifications for Android aren't appearing without a title set (body only).
I have a bug that an Android push notification is not multiline.
All the text is set in the notification's title - no surprises.
Splitting the text is kinda difficult as there are really a lot of options with their own variables and localization.
I've tried to set the text into a body instead of a title as it's done for iOS (working fine), and the notification just won't come!
webPushBuilder = new WebPush.Build()
.addBody(localizedText)
.addData(customDictionary);
The pushes arrive with title only and with title+body, but body-only ones don't come.
Is there a way to send them without the title?
From what I found, a title is required to receive a Push notification on Android.
Setting title to "\n", " ", "" won't work either.
I'm new to robotium and i'm trying to write a quick and dirty script to run through all screens in an app.
The problem i have mainly with the 'home button' in the app. I've tried lots of options but i cant seem to get it to click there except with index, which is not what i want.
When i check out the button with the hierarchyviewer it looks like this:
Link
However when i try for example:
assertTrue(
"Wait for text (id: myapp.R.id.home) failed.",
solo.waitForImageById("myapp.R.id.home", 20000));
solo.clickOnImage((ImageView) solo.findViewById("myapp.R.id.home"));
solo.waitForActivity("MenuActivity");
It fails at the waitForImageByID line. Ive tried multiple options like waitForImageButton etc, but i just cant seem to get it clicked. What am i missing here?
junit.framework.AssertionFailedError: View with id: '0' is not found!
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1990)
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1970)
at com.bitbar.recorder.extensions.OtherUtils.a(OtherUtils.java:246)
at com.bitbar.recorder.extensions.OtherUtils.b(OtherUtils.java:241)
at com.bitbar.recorder.extensions.v.a(Waiter.java:71)
at com.bitbar.recorder.extensions.ExtSolo.waitForImageButtonById(ExtSolo.java:4176)
at com.example.android.apis.test.Test.testRecorded(Test.java:137)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1740)
Use the following line to press the home button in the action bar
solo.clickOnActionBarHomeButton();
The issue is that the id that it is referring is not in your application, it is in androids default R file, try android.R.id.home and it should work fine. It is worth noting though that if your application uses action bar sherlock to support the action bar pre 4.0 that this will have a different ID there and you will have to handle this in your test.
You can see this for yourself looking at: http://developer.android.com/reference/android/R.id.html
When you are using ActionBarSherlock there are two different Ids you have to check, android.R.id.home for API-Level>11 and abs__home for lower levels (provided by ActionBarSherlock):
View homeButton = activity.findViewById(android.R.id.home);
if (homeButton == null) {
homeButton = activity.findViewById(R.id.abs__home);
}
What about this code:
ArrayList<LinearLayout> ll = solo.getCurrentViews(LinearLayout.class);
//You can change 1 with the ordinal number of LinearLayout you want to click.
solo.clickOnView(ll.get(1));
or also
ArrayList<ImageView> iv = solo.getCurrentViews(ImageView.class);
//You can change 0 with the ordinal number of Image you want to click.
solo.clickOnView(iv.get(0));
I think if you identify the correct id for view or linear layout or image view it should work.
Dave C's answer was working only partially for me. The button was clicked but before the preceding screen was loaded assertions had started and thus were always false. The solution is to run "home click" on the main thread (Robotium 5.2.1):
getInstrumentation().runOnMainSync(new Runnable() {
#Override
public void run() {
solo.clickOnActionBarHomeButton();
}
});
From your question I can see that it is an image view. You can click on any view using the following piece of code.
View view = solo.getView("View_name_from_hierachy_viewer");
solo.clickOnView(view);
View_name_from_hierachy_viewer in your case will be "home".
Let me know if this does not work.
Situation:
I have a TextView that have the property
android:textAlignment="center"
I am generating another TextView dinamically, based on my TextView from XML Layout, using a clone, cloning all the basic properties to work the way above.
Problem:
To do this i need to use this method:
this.myTextView.setMyTextViewProperty(MyTextView.getMyTextViewProperty());
for example:
this.MyTextView.setText(MyTextView.getText());
Note that this.MyTextViewis a local variable and MyTextView is a private var declared on the top of the file, under class name.
I do this on all the properties of the TextView but when i hit the following line of code from the TextAlignment property...:
this.myTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
I tried to set it to a Custom Aligment instead of getting from my XML TextView It gives me an error:
11-20 15:27:04.460: E/AndroidRuntime(9185): FATAL EXCEPTION: main
11-20 15:27:04.460: E/AndroidRuntime(9185): java.lang.NoSuchMethodError: android.widget.TextView.setTextAlignment
But i see on Ctrl + Space that the method exists, so i cant understand what is happening.
A second try was, to set my TextView property to the property that comes from my TextView:
this.myTextView.setTextAlignment(MyTextView.getTextAlignment());
With no success, too.
Obs: i do not want a Android XML Layout solution, i want a solution on code, because i generate the TextView dinamically on the Activity
I'm using API Level 15
Any help?
The setTextAlignment method was added in API level 17. Maybe your compiler in the IDE is above 17 and the device/emulator which you are testing is less than that. Here the link to setTextAlignment.
Added from the comments:
For API level 15 and below, you want setGravity, per this question.