I am trying to write a code to hide certain ui elements while Android Keyboard is shown , i tried to add "adjustResize" in the manifest but it re-sizing the elements automatically but not hiding some of the views in the layout.
MyCode:
package com.example.code;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No above", Toast.LENGTH_SHORT).show();
}
} }
XML :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity">
<ScrollView
android:id="#+id/scroll"
android:layout_width="wrap_content"
android:layout_height="300dp">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/image_back"/>
</ScrollView>
<TextView
android:id="#+id/xyz"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:background="#android:color/holo_orange_dark"
android:gravity="center_horizontal"
android:text="Test Layout"
android:textSize="40sp" />
<EditText
android:id="#+id/abc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/xyz"
android:layout_alignParentLeft="true"
android:background="#android:color/holo_blue_bright"
android:ems="10"
android:gravity="center_horizontal"
android:text="Please enter text"
android:textSize="40sp" >
<requestFocus />
</EditText>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.code"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.code.MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="#string/app_name"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Screenshots:
So , these are above codes and screenshot of my app , so i am want hide the "Test Layout" textview while keyboard is open. So , please suggest me some technique and solution for my issue.
You can try something like this and update the visibility of your layout according to the visibility of the keyboard.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
/*Hide button when keyboard is open*/
view.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
view.getWindowVisibleDisplayFrame(r);
int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 244) { // if more than 100 pixels, its probably a keyboard...
//ok now we know the keyboard is up...
buttonLayout.setVisibility(View.GONE);
} else {
//ok now we know the keyboard is down...
buttonLayout.setVisibility(View.VISIBLE);
}
}
});
Related
Activity supports Landscape mode
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="landscape"
android:label="#string/app_name" >
where I have allocated 50% space to Video Player (using FrameLayout) and rest 50% to ListView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/video_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ListView
android:id="#+id/video_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Now, I have started playing video > moved to full screen mode > pressed back to exit full screen mode > getting Activity in Portrait mode (whereas I was expecting to get it in Landscape mode)
boolean isFullScreen = false;
#Override
public void onGoToFullscreen() {
isFullScreen = true;
videoListView.setVisibility(View.INVISIBLE);
}
#Override
public void onReturnFromFullscreen() {
videoListView.setVisibility(View.VISIBLE);
}
#Override
public void onBackPressed() {
Log.d("boolean:-", Boolean.toString(isFullScreen));
if(isFullScreen) {
imaPlayer.getContentPlayer().setFullscreen(false);
}
else {
super.onBackPressed();
}
}
/* I am not sure,can you put this line of code in on back pressed method ? */
Log.d("boolean:-", Boolean.toString(isFullScreen));
if(isFullScreen) {
imaPlayer.getContentPlayer().setFullscreen(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
super.onBackPressed();
}
Hello I am new in android developping, I made a small test but it's giving me that error :
'Unfortunately android test has stopped'
I sought some similar topics here, but no one resolved my problem, here is my code :
main activity :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(checkLogin(username.getText().toString(),password.getText().toString())){
Intent i = new Intent(MainActivity.this, HelloActivity.class);
startActivity(i);
}
else{
username.setText("");
password.setText("");
}
}
});
}
private boolean checkLogin(String u,String p){
if (u == "hello" && p == "world")
return true;
else return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
the main layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.androidtest.MainActivity$PlaceholderFragment" >
<EditText
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:ems="10"
android:hint="username" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/username"
android:layout_below="#+id/username"
android:ems="10"
android:hint="password"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/username"
android:layout_below="#+id/password"
android:text="Button" />
</RelativeLayout>
the second acivity
package com.androidtest;
import android.app.Activity;
import android.os.Bundle;
public class HelloActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.hello_layout);
}
}
its layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="193dp"
android:text="Afin akhay lpchiiwr"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
and finally the Manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.androidtest.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.androidtest.HelloActivity"></activity>
</application>
</manifest>
Thanx
The first xml block you've posted looks like it's from the fragment_main.xml file. If so, then your problem is that findViewById() is looking for Views in the Activity that have been inflated into the Fragment. If you don't need the Fragment, then move that xml to activity_main.xml and remove the code for the Fragment. Otherwise, move the View initializations and methods to your Fragment.
And, as Kedarnath points out, you should use the String.equals() method to compare Strings.
First of all let me tell you that you are comparing String in wrong way,
private boolean checkLogin(String u,String p)
{
if (u == "hello" && p == "world") // Wrong way
return true;
else return false;
}
You need to compare string in following way,
private boolean checkLogin(String u,String p)
{
if (u.equals("hello") && p.equals("world") ) // Right way
return true;
else return false;
}
I have a question about xml parser in android. I am creating button information in the xml. I am getting its height,width,color and so on. I am creating all buttons in a function. Now i want to do the following. When i click the buttons, new android page opens. As far as i know i will do it by creating new activities.
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
I am writing this on my function which i create all buttons.when i do this it does not work.can somebody help me?
In your Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
View btnTapOnMe = findViewById(R.id.btnTapOnMe);
btnTapOnMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
}
view_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnTapOnMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap on Me" />
</RelativeLayout>
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourpackage"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.yourpackage.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.yourpackage.MainActivity2"/>
</application>
</manifest>
Should work like a charm
UPDATE:
Don't ever use AbsoluteLayout it is deprecated
This class was deprecated in API level 3.
Use FrameLayout, RelativeLayout or a custom layout instead.
First of all change it to Frame or Relative layout.
Here what i get in my example below :
public class MainActivity extends FragmentActivity {
public void AddAllButtons() {
AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
Button btn = new Button(this);
btn.setText("Test case");
Layout.addView(btn);
AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn.getLayoutParams();
absParams.x = 100;
absParams.y = 100;
absParams.width = 150;
btn.setLayoutParams(absParams);
btn.setBackgroundColor(Color.GREEN);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SearchActivity.class);
startActivity(i);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
AddAllButtons();
}
}
and here is xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/btnTapOnMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap on Me"/>
<AbsoluteLayout
android:id="#+id/layout1"
android:layout_below="#+id/btnTapOnMe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</AbsoluteLayout>
</RelativeLayout>
I'd like to pop up a layer in fullscreen mode (overlays the whole desktop) and hide on click.
I'm new in Android developing and looking for something like this (pseudocode):
$('#layer').on('click', function() {
this.hide();
});
<div id="layer" class="fullscreen">I'm a annoying layer, click me!</div>
Does anyone have a solution, code snippet, tutorial or some keywords to google for? I follow some basic tutorials right now and would like to know in which direction I've to focus.
Thanks in advance!
This is a simple activity that will close itself when the screen is touched.
activity_popup.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="Hello World!" />
</RelativeLayout>
PopupActivity.java
public class PopupActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup);
}
#Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
finish();
return true;
}
return false;
}
}
androidmanifest.xml
<activity
android:name="com.your.package.name.PopupActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
I have the following the layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:orientation="horizontal"
android:id="#+id/main">
<com.company.DrawView
android:id="#+id/draw_view"
android:layout_width="900dp"
android:layout_height="500dp"
android:layout_alignParentLeft="true"
android:background="#CCCC00" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/draw_view"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="10dp"
android:clickable="true"
android:onClick="buttonClicked"
android:text="button" />
<EditText
android:id="#+id/text1"
android:textSize="5dp"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:maxWidth="5dp"
android:visibility="invisible"
android:gravity="bottom"
/>
</LinearLayout>
</RelativeLayout>
Whenever I set the focus on the EditText, it is expanding and filling up the top half of the screen and the soft keyboard is filling up the bottom half of the screen. What should I do retain the size of the EditText and show the soft keyboard?
The intent is to capture the key strokes (on the soft keyboard) of the user and use it on another part of the application.
Edit 1:
I'm using sdk-version 15 and I am on macbook if that helps. Adding the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".ChalkrText"
android:label="#string/app_name"
android:windowSoftInputMode="adjustNothing" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
//in your manifiest.xml write the below code in your activity
android:windowSoftInputMode="adjustNothing"
This is standart behavior for EditText in landscape orientation of device. You can't change it.
I found a work around by showing the soft keyboard on clicking a button and overriding the dispatchKeyEvent in the Activity to capture the key strokes. I didn't need the EditText and it wasn't required to show the soft keyboard.
This is to show the soft keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
This is the dispatchKeyEvent:
public boolean dispatchKeyEvent(KeyEvent event) {
/**
* ACTION_MULTIPLE is for keys with special characters like ©
* British Pound sign etc. These keys do not generate the ACTION_DOWN
* or ACTION_UP events.
*
* Only the ACTION_DOWN is handled. ACTION_UP is ignored to avoid
* duplication of the letters that the user wants to type.
*/
if (event.getAction() == KeyEvent.ACTION_DOWN ||
event.getAction() == KeyEvent.ACTION_MULTIPLE) {
DrawView dview = (DrawView) findViewById(R.id.draw_view);
Log.v(LOG_TAG, "c = " + event.getKeyCode() +
", l = " + event.getDisplayLabel() +
", u = " + event.getUnicodeChar() +
", uc = " + (char)event.getUnicodeChar() +
", chars = " + event.getCharacters()
);
dview.addText(event);
}
return true;
}
This is the addText method where the concerned event is handled:
public void addText(KeyEvent event) {
int unicode = event.getUnicodeChar();
int keycode = event.getKeyCode();
String characters = event.getCharacters();
if (unicode == 0) {
switch (keycode) {
case 0:
if (characters != null) {
text += characters;
}
break;
case KeyEvent.KEYCODE_DEL:
text = text.substring(0, text.length()-1);
break;
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
// ignore, nothing to do
break;
}
}
else {
text = text + (char)unicode;
}
invalidate();
}
These are the posts that set me in the right direction:
How to Capture soft keyboard input in a View?
Can I use the soft keyboard without an EditText?