Android- Eclipse can't find ImageButton class - java

I am fairly experienced with Java/Eclipse, but I'm entirely new to Android development and it's proven to be quite an odd beast so far. I'm currently trying to create an application with two image buttons. My XML is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/now_playing" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/StartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/start_button"
android:contentDescription="#string/start_button" />
<ImageButton
android:id="#+id/StopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#drawable/stop_button"
android:contentDescription="#string/stop_button" />
</LinearLayout>
</LinearLayout>
This appears to be correct, but the ImageButtons do not render properly in the Graphical Layout tab and there is a message that says, "The following classes could not be found:
- ImageButton (Change to android.widget.ImageButton, Fix Build Path, Edit XML)".
After looking around online a bit, I found that this question has popped up a few times on StackOverflow, but I was unable to find any satisfactory answers. The most common answer was to clean the project, but this has not done anything for me. Any suggestions?

Sometimes the Eclipse Graphical layout tab is unable to draw stuffs whereas the code is fine. Try to run a simple Activity with setContentView(R.layout.yourxml); and see what happen :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
}
}
Post your LogCat if you still have errors.
EDIT : Oh and I didn't see :
On your second ImageButton, replace
android:text="#drawable/stop_button"
by
android:src="#drawable/stop_button"

Try to import the ImageButton class on java.
import android.widget.ImageButton;

Related

Android Watch fullscreen FAB like the DismissOverlay

There is the DismissOverlayView, which you can implement to give the user an alternative way to exit the application on an android watch. This View implements something, that looks like a fullscreen FAB. Now I would like to know, how I could implement the same View with another button icon/color/behaviour. Since you can't change the DismissOverlayView which code looks btw. like this :
<android.support.wearable.view.DismissOverlayView
android:id="#+id/dismiss_overlay"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
I guess you have to implement some custom FAB, but I can't use the FAB in my watch XML either because there is an dependency missing or because it's simply not supported by the watch os. I tried following code for testing :
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"/>
Edit: For better understanding, I want the button look like this :
Edit: I just found this solution, I was not aware of that one :
https://developer.android.com/training/wearables/ui/confirm.html
But I would still be curious, if you could implement a more customisable version of this buttons.
Okay I found a solution for that problem, actually you can simply create a new layout.xml and overwrite the default code, which is the following one :
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/dismiss_overlay_explain"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="32dp"
android:layout_width="wrap_content"
android:padding="12dp"
style="#style/DismissOverlayText"
/>
<android.support.wearable.view.ActionPage
android:id="#+id/dismiss_overlay_button"
android:layout_height="match_parent"
android:background="#color/dismiss_overlay_bg"
android:layout_width="match_parent"
android:src="#drawable/ic_full_cancel"
android:color="#color/dismiss_close"
app:buttonRippleColor="#color/dismiss_close_pressed"
android:text="#string/dismiss_overlay_button_label"
/>
</merge>
Now you can use your newly created xml customise it and use this one instead of the original dimissOverlayView. But you should be careful with doing so, because this is not really intended from the design guidelines to do.

Running TextView failure/Bug (Singleline marquee)

Im trying to let my textview run from left to right in an infinite way.But somehow it looks really weird I even cant explain it in words so I decided to record and upload it.
Here is the video : https://www.youtube.com/watch?v=gj3FF7fEutk&feature=youtu.be
So thats how I wrote my Textview :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:layout_marginTop="20dp"
android:textColor="#color/white"
android:id="#+id/textView_title_full"
android:layout_centerHorizontal="true" />
And this TextView is in a RelativeLayout declared like this :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="170dp"
android:id="#+id/BELOW_FULL"
android:layout_gravity="bottom"
android:background="#drawable/color_below_player_full"
android:layout_below="#+id/viewpager_albumart_full"
android:layout_alignParentBottom="true">
where the RelativeLayout is also in a Relativelayout like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#0E0E0E">
I of course used the txt_song_title.setSelected(true); method.
How it looks like what I want to achieve :https://www.youtube.com/watch?v=o4uAA4pik68
Thank you all very much !
EDIT:
All parts where I do sth with my TextView :
public void init_textviews()
{
...
txt_song_title = (TextView)findViewById(R.id.textView_title_full);
txt_song_title.setSelected(true);
}
//init_textviews() is called in my onCreate method
txt_song_title.setText("Some Long text"); // called in onCreate after init_textviews()
And lastly:
public static void update_GUI_full (Context context) //Called when it is a button is clicked
{
...
txt_song_title.setText("longTexthere");
...
}
Working side by side with #Ahmet Kazaman we've found that there seemed to be a problem when combining a marquee TextView with another TextView periodically updated using a Handler.
After some tests we've found that this strange behaviour only happens when the TextViews are iside a RelativeLayout and everything woeks as expected when the enclosing layout is a LinearLayout. Thus, the solution (more a workaround) is to change the enclosing layout to be a Linearlayout.

How to properly add view programatically?

I know that this topic is nothing new, but I really got stuck with this and tried a lot of answers and still I can't make it clear what and where should I write and use.
I have this layout file settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
...
<TextView
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/pass_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="7dp"
android:text="Удалить пароль"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
</LinearLayout>
I have TextView passdel that I want to add programmatically. Its onCreate description is passdel=(TextView) findViewById(R.id.pass_del);
And I have these methods
public void onPasSet() {
pass.setText("Сменить пароль");
((LinearLayout)passdel.getParent()).addView(passdel);
}
public void onPasDel() {
pass.setText("Установить пароль");
((LinearLayout)passdel.getParent()).removeView(passdel);
}
onPasDel is working well.
I guess Java takes current layout. So when I remove View, it's on this layout. And when I try to add this View, Java tries to find this view on current layout, but it's removed, so ..nullpointerexception. How should I write all addView stuff properly? How to point on the needed layout?
Why do you need to remove/add this View? If I understood the goal of this component correctly, the best way will be just to hide/show it:
passdel.setVisibility(View.GONE);
passdel.setVisibility(View.VISIBLE);
Of course, after you've removed passdel from ViewGroup, it has no parents anymore, so you'll get NPE while trying to call onPasSet() after onPasDel()
As you suggested, getParent() won't work intil TextView is not instantiated.
You should get Layout in different way.
You can do it by findViewById(R.id.layout.id).
Example:
In XML:
<LinearLayout
...
android:id="#+id/my_layout">
...
In Activity:
LinearLayout ll = (LinearLayout) findViewById(R.id.my_layout);
ll.addView(myView);

Attempting to Implement a Thread for a Game Loop

I am a novice learning Android development and the next step I must take is to be capable to understanding threads to be able to implement a game loop.
I have taken knowledge from google and StackOverflow and snippets from examples to come up with a lump of code that seems to not be working:
MainActivity.java
GameSetup.java
GameView.java
activity_main.xml
game_setup.xml
MainActivity.java is a simply title screen with a play button. This seems to work fine and will link to the next activity. However, I am getting a crash whenever I attempt to load up the next activity. My guestimations based on the error lead me to believe that threads have issues with RelativeActivity, but I'm not so sure on what the issue may be exactly, I am rather confused.
Here is the error I am getting thrown, I am having issues trying to decipher the issue with it.
Thanks.
R.id.layout is a RelativeLayout but you are trying to cast it to GameView, that's causing your crash.
Edit:
To add your GameView to the RelativeLayout you can do
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.your.package.GameView
android:id="#+id/my_game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
</RelativeLayout>
and retrive it with
GameView myGameView = (GameView) findViewById(R.id.my_game_view);
Hmm, from the code you have written so far, I can explain the reason for the ClassCastException.
You are declaring a RelativeLayout in both XML files.
Both RelativeLayouts use the same android:id "#+id/layout"
In GameView.java you try to find a view by specifying the id
m_game = (GameView) findViewById(R.id.layout);
IMO you are retrieving one of the RelativeLayouts and then try to cast it to a GameView. As this is not possible, Java throws the exception.

Android - Programmatically Hide/Show Soft Keyboard [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you close/hide the Android soft keyboard programmatically?
First thing first I already saw this thread. I tried the accepted methods given there, but nothing worked for me.
I have two screens in my app.
First one has 2 EditText - One for username and one for password
Second one have one ListView, and an EditText - to filter the
listView
In my first screen, I want username EditText to have focus on startup and the Keyboard should be visible. This is my implementation (simplified by removing unnecessary/unrelated code).
#app_login.xml
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip">
<EditText android:id="#+id/username"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="Username"
android:imeOptions="actionDone" android:inputType="text"
android:maxLines="1"/>
<EditText android:id="#+id/password"
android:password="true"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Password" />
</LinearLayout>
#AppLogin.java
class AppLogin extends Activity{
private EditText mUserNameEdit = null;
private EditText mPasswordEdit = null;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.app_login);
mUserNameEdit = (EditText) findViewById(R.id.username);
mPasswordEdit = (EditText) findViewById(R.id.password);
/* code to show keyboard on startup.this code is not working.*/
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);
}//End of onCreate()
}
Well, the keyboard is not showing at startup. And my design badly requires a keyboard there.
Now on to second page. As I already mentioned, I have a listView and EditText there. I want my keyboard to be hidden on startup only to appear when the user touches the editText. Can you believe it? whatever I tried soft Keyboard is showing when I load the activity. I am not able to hide it.
#app_list_view.xml
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="#+id/filter_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search" android:inputType="text"
android:maxLines="1"/>
<ListView android:id="#id/android:list"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
#AppList.java
public class MyListActivity extends ListActivity{
private EditText mfilterEditText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_list_view);
mFilterEditText = (EditText) findViewById(R.id.filter_edittext);
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mFilterEditText.getWindowToken(), 0);
}
}
To simplify
On Login Page (first Page) I want my keyboard to be visible on startup.
On SecondPage I want the keyboard to be hidden first, only to appear
when the user touches editText
And my problem is I am getting the exact opposite on both occasions. Hope someone faced this issue before. BTW I am testing on the simulator and HTC Desire phone.
#FINAL OUTCOME
Well, I got it working, with the help of all my friends here.
1. To Show keyboard on startup
Two answers worked for me. One provided by #CapDroid, which is to use a handler and post it delayed..
mUserNameEdit.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(mUserNameEdit, 0);
}
},50);
The second answer is provided by #Dyarish, In fact, he linked to another SOF thread, which I haven't seen before. But the funny thing is that this solution is given in the thread which I referenced at the start. And I haven't tried
it out because it had zero votes in a thread where all other posts have plenty of votes. Height of foolishness.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
For me, the second solution looked neat, so I decided to stick with it..But the first one certainly works.
Also, #Dyarish's answer contains a clever hack of using a ScrollView below EditText to give EditText the focus. But I haven't tried it, but it should work. Not neat though.
2. To hide keyboard at activity start
Only one answer worked for me, which is provided by #Dyarish. And the solution is to use
focusableInTouchMode settings in XML for the layout containing the EditTexts. This did the trick
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<EditText android:id="#+id/filter_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search" android:inputType="text"
android:maxLines="1"/>
<ListView android:id="#id/android:list"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
Anyway, I end up using Dyarish's answer in both cases. So I am awarding the bounty to him. Thanks to all my other friends
who tried to help me.
UPDATE 2
#Override
protected void onResume() {
super.onResume();
mUserNameEdit.requestFocus();
mUserNameEdit.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(mUserNameEdit, 0);
}
},200); //use 300 to make it run when coming back from lock screen
}
I tried very hard and found out a solution ... whenever a new activity starts then keyboard cant open but we can use Runnable in onResume and it is working fine so please try this code and check...
UPDATE 1
add this line in your AppLogin.java
mUserNameEdit.requestFocus();
and this line in your AppList.java
listview.requestFocus()'
after this check your application if it is not working then add this line in your AndroidManifest.xml file
<activity android:name=".AppLogin" android:configChanges="keyboardHidden|orientation"></activity>
<activity android:name=".AppList" android:configChanges="keyboard|orientation"></activity>
ORIGINAL ANSWER
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
for hide keyboard
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
for show keyboard
imm.showSoftInput(ed, 0);
for focus on EditText
ed.requestFocus();
where ed is EditText
Adding this to your code android:focusableInTouchMode="true" will make sure that your keypad doesn't appear on startup for your edittext box. You want to add this line to your linear layout that contains the EditTextBox. You should be able to play with this to solve both your problems. I have tested this. Simple solution.
ie: In your app_list_view.xml file
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusableInTouchMode="true">
<EditText
android:id="#+id/filter_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:inputType="text"
android:maxLines="1"/>
<ListView
android:id="#id/android:list"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:layout_width="fill_parent"
android:focusable="true"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
------------------ EDIT: To Make keyboard appear on startup -----------------------
This is to make they Keyboard appear on the username edittextbox on startup. All I've done is added an empty Scrollview to the bottom of the .xml file, this puts the first edittext into focus and pops up the keyboard. I admit this is a hack, but I am assuming you just want this to work. I've tested it, and it works fine.
<?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="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip">
<EditText
android:id="#+id/userName"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="1"
/>
<EditText
android:id="#+id/password"
android:password="true"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Password" />
<ScrollView
android:id="#+id/ScrollView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ScrollView>
</LinearLayout>
If you are looking for a more eloquent solution, I've found this question which might help you out, it is not as simple as the solution above but probably a better solution. I haven't tested it but it apparently works. I think it is similar to the solution you've tried which didn't work for you though.
Hope this is what you are looking for.
Cheers!
Try this code.
For showing Softkeyboard:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
For Hiding SoftKeyboard -
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
Did you try InputMethodManager.SHOW_IMPLICIT in first window.
and for hiding in second window use InputMethodManager.HIDE_IMPLICIT_ONLY
EDIT :
If its still not working then probably you are putting it at the wrong place. Override onFinishInflate() and show/hide there.
#override
public void onFinishInflate() {
/* code to show keyboard on startup */
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);
}

Categories