android studio casting warning - android

I mistakenly disable this feature but I don't know how to enable again. The feature is that warns me when I cast an object to another class. For example:
ImageView scanner;
TextView errorMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
errorMessage = (TextView) findViewById(R.id.errorMessage);
scanner = (TextView) findViewById(R.id.scanner); // studio warns me when I do this cast, but I mistakenly close this feature
}
Thanks
EDIT
this is the exact solution:

You decalred scanner as imageview on top
ImageView scanner;
and you are tying to cast it to textview in oncreate, that is not possible.
Change
scanner = (TextView) findViewById(R.id.scanner);
to
scanner = (ImageView) findViewById(R.id.scanner);

You can try the following trick:
Step 1:
Find the inspector icon (a human with a cap) at the bottom right corner of your Android Studio.
Step 2:
Click on that and you can find a seek bar. It will help you to enable/disable or even you can configure it according to your need.

Related

Grabbing a pointer to a TextView of a tab on the action bar?

Some other users and I are developing an Android application for the Stack Exchange chat network. We're adding a tutorial for each activity to explain the UI to the user. However, we've run into a bit of a road block.
The tutorial library wants a pointer to a view (be it a TextView, ImageView, whatever) in order to get the coordinates of the view in the display so it knows where to draw the drop shadows and stuff.
We have one activity which uses the standard "Tabbed Activity" from Android Studio, so we aren't using any custom toolbars.
The action bar looks like this:
And we want to grab a pointer to the TextView on each tab that holds the title of the tab.
So for example, we want to be able to access this Textview:
We haven't been real successful in finding anything on the internet about how to do this. It appears to be relatively easy if you're using a custom toolbar, but we aren't.
Digging in the AOSP source code, we found a potential way to do it, but the fields that we needed access to were either private or otherwise unaccessible from the main activity code.
So the question is, how can we grab a pointer to that TextView? Is it even possible?
Well, it isn't pretty but we found a way to do it. Using the layout inspector in Android Device Monitor to look at the view hierarchy, we were able to grab a pointer to it in the following way.
Keep in mind:
You may need to adjust for your activity's layout
If you're using a custom toolbar there's an easier way to do this
That being said, here's what worked for this specific use case:
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
LinearLayout testb = (LinearLayout) viewGroup.getChildAt(0);
FrameLayout testc = (FrameLayout) testb.getChildAt(1);
ActionBarOverlayLayout testd = (ActionBarOverlayLayout) testc.getChildAt(0);
ActionBarContainer teste = (ActionBarContainer) testd.getChildAt(1);
LinearLayoutCompat testg;
if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT)
{
ScrollingTabContainerView testf = (ScrollingTabContainerView) teste.getChildAt(2);
testg = (LinearLayoutCompat) testf.getChildAt(0);
}
else //Landscape
{
Toolbar teste2 = (Toolbar) teste.getChildAt(0);
ScrollingTabContainerView testf = (ScrollingTabContainerView) teste2.getChildAt(0);
testg = (LinearLayoutCompat) testf.getChildAt(0);
}
testg.setId(android.R.id.tabcontent);
//String IdAsString = testg.getResources().getResourceName(testg.getId());
//Log.e("TestG", IdAsString);
TutorialStuff.chatsExplorationTutorial(this, testg);
And here's the end result:

Android Studio cannot resolve symbol textView

I'm completely new to Android Studio.
I have just installed it on Windows 10 and since I wanted to learn how to use it, I have started to follow the online guide (https://developer.android.com/training/basics/firstapp/starting-activity.html#Up).
I am literally following it step by step, but when it comes to this part of the code:
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}
the "textView" part after "findViewById" appears red and I get this error message: "cannot resolve symbol 'textView'".
This happens even if I have imported android.widget.TextView.
Maybe this is stupid question, but I am completely new to Android Studio.
Thanks for the answers :)
In your file R.layout.activity_display_message, the <TextView> tag must have android:id="#+id/textView"
Can you include your XML File?
As mentioned by buutqn
<TextView> tag must have android:id="#+id/textView"
This is in the file: app.res.layout.activity_display_message.xml
under the Text tab(as opposed to Design tab).
This issue can be duplicated by deleting the TextView box from the design tab, and inserting a new (second) text view.
In my case, the problem was for other reason although the error was the same. I copied the following code out of the OnCreate method.
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
The text (TextView) before findViewById was not neccesary.
The TextView object of the Activity_display_message is called TextView2, instead of Textview. So I tried and changed:
TextView textView = (TextView) findViewById(R.id.textView);
for
TextView textView = (TextView) findViewById(R.id.textView2);
And it worked!
Erase the statement, then retype it. There is going to be a small pop up indicating you to press a command. It is basically an import library issue when you press the command (for macOS it is ⌥⏎ ) the respective library will be added. If this does not work, close Android Studio and reopen it . I am not an expert on this matter, but this worked for me hence I am sharing.
After tiresome searches and updating settings,
I Just clicked Invalidate Catches -> Invalidate And Restart
And Done! TextView Error Disappearedenter image description here

Click on linkified TextView scrolls ScrollView

I have a ScrollView containing a TextView. I linkify parts of that TextView with
Pattern pattern = Pattern.compile("MyLink");
Linkify.addLinks(textView1, pattern, "mylink://");
I habe an Intent filter in my Manifest for mylink:// so a new Activity is opened when MyLink is clicked (as described in this question).
This works most of the time, sometimes though a click on the MyLink portion of the TextView doesn't open the Activity but only scrolls the ScrollView in which my TextView resides to the top.
Where does this behaviour come from and how can I fix it?
If you are attempting to open a link that leads to your current activity, it might be recreating the same activity and gives the sensation that it's scrolling up. Probably you want to modify your manifest and set the activity's launchMode attribute to singleTask
Why dont you use TextViewWithLinks?
With that you can have two types of click on the textview,
1. On TextView
2. On Link TextView
String text = "bananas on www.bananas.ba and Google";
TextViewWithLinks textview = new TextViewWithLinks(this);
textview.setText(Html.fromHtml(text));
textview.linkify(new TextViewWithLinks.OnClickLinksListener() {
#Override
public void onTextViewClick() {
// Do whatever you want
}
#Override
public void onLinkClick(String url) {
// Do whatever you want
}
});
//SET Colors
textview.setLinkColors(Color.RED, Color.BLACK);
setContentView(textview);
Let me know if this is not resolving your issue.
Enjoy Coding... :)

"...cannot be resolved to type" error message; no method suggestions in Eclipse

Ok guys I've got a question that for the life of me I cannot get figured out. I'm using Eclipse, and I've got the Android ADT pluging all installed, the program configured with the Android SDK path, and all the extra SDK parts downloaded (Google API's, SDK Platforms, System Images). Here is my problem: whenever I go to access a method from a certain object, Eclipse gives no method "suggestions" like it usually does. I'm probably not explaining this the best, so I've attached a screenshot that kind of illustrates the issue.
public class StartingPoint extends Activity {
int counter;
Button add;
Button sub;
TextView display;
//putting add.whatever here doesn't work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter = 0;
add = (Button) findViewById(R.id.buttonAdd);
sub = (Button) findViewById(R.id.buttonSub);
display = (TextView) findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
//putting add.whatever here doesn't show suggestions either.
I've tried everything I can think of to get it working, deleting & reinstalling eclipse, deleting everything Android from with Eclipse and going through all the SDK setup again, changing my workspace, creating an entirely new project, deleting the Eclipse preference file, endless Googling, resetting & altering settings, and I can't for the life of me figure out what's causing this. Any ideas? Thanks!

Expand a list view when clicked on an element of it

I am beginner in the android development, can any one tell me how to expand a list when I click on an element of it say an image icon.
This does it for me, due to privacy I can't write the whole code here
TextView desc = (TextView) v.findViewById(R.id.editText1);
ImageView icon = (ImageView) v.findViewById(R.id.listIcon);
if (desc.getVisibility() == View.VISIBLE) {
icon.getLayoutParams().height = heightIcon;
desc.setVisibility(View.GONE);
} else {
icon.getLayoutParams().height = LayoutParams.FILL_PARENT;
desc.setVisibility(View.VISIBLE);
}
Now when I click the icon, the text view placed below to it, becomes visible and thus has solved the question.
Take a look at ExpandableListView and ExpandableListAdapter
Here is the example code snippet to do it : ExpandableList1.java
You can even find it in your Android SDK folder
Drive:***\***\Android-sdk\samples\android-<apilevel>\ApiDemos\src\com\example\android\apis\view\ExpandableList1.java

Categories

Resources