I've just wrote my first android application, but I got a so sneaky problem :D.
The application is so simple, it has just one text box, one button and one web view.
The web view is invisible, when the button is clicked it will be visible and go to the url written in the text box
the application is full screen.
The problem is that when I click the button it does nothing at first, the second time it works.
So as I know there is a focus problem, I think, i just themed it with
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
so there is no title bar at top. I remember that when I had title bar these acts took place
1 - I click on button
2 - the title bar appears
3 - I click on button
4 - it took affect
I can say that when I make title bar invisible, steps are the same as top.
So i need help to solve this problem, any ideas?
activity_fullscreen.xml
<FrameLayout 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:background="#0099cc"
tools:context=".FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#ff3191ff">
<LinearLayout android:id="#+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
</LinearLayout>
</FrameLayout>
<AutoCompleteTextView android:id="#+id/iptxt"
android:layout_width="272dp" android:layout_height="59dp"
android:nextFocusUp="#id/iptxt" android:nextFocusLeft="#id/iptxt"
android:layout_gravity="center"
android:textColor="#ff26b9ff"
android:textSize="10pt"
android:text="192.168.1.104"
android:textAlignment="center"
android:textStyle="bold"
android:singleLine="true"
android:typeface="normal"
android:inputType="text" />
<Button
android:layout_width="fill_parent"
android:layout_height="59dp"
android:id="#+id/ConnectBTN"
android:layout_gravity="bottom"
android:background="#ff3191ff"
android:text="Connect"
android:textColor="#ffffffff"
android:textSize="12pt"
android:textStyle="bold" />
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Mainwbv"
android:visibility="invisible"></WebView>
</FrameLayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.proapp.share" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
fullscreenactivity.java
package ir.proapp.share;
import ir.proapp.share.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import org.apache.http.protocol.RequestUserAgent;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class FullscreenActivity extends Activity {
/**
* Whether or not the system UI should be auto-hidden after
* {#link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
*/
private static final boolean AUTO_HIDE = true;
/**
* If {#link #AUTO_HIDE} is set, the number of milliseconds to wait after
* user interaction before hiding the system UI.
*/
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
/**
* If set, will toggle the system UI visibility upon interaction. Otherwise,
* will show the system UI visibility upon interaction.
*/
private static final boolean TOGGLE_ON_CLICK = true;
/**
* The flags to pass to {#link SystemUiHider#getInstance}.
*/
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
/**
* The instance of the {#link SystemUiHider} for this activity.
*/
private SystemUiHider mSystemUiHider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content_controls);
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
Button connectbtn=(Button)findViewById(R.id.ConnectBTN);
connectbtn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
EditText iptetx=(EditText)findViewById(R.id.iptxt);
WebView maniWV=(WebView)findViewById(R.id.Mainwbv);
WebSettings mainSettings=maniWV.getSettings();
mainSettings.setJavaScriptEnabled(true);
mainSettings.setUserAgentString("Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31
(KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");
maniWV.setVisibility(View.VISIBLE);
maniWV.setWebViewClient(new Ac1());
maniWV.loadUrl("http://"+iptetx.getText()+":9999");
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
/* findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener); */
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
Related
I have a search bar, a search button (as i just converted to using onKeyPress and haven't removed it yet), and a TextView.
It works well, except that each key press issues a new call to Search(), and the old call doesn't stop running. If i type too quickly or for too long, the app crashes. How do I better manage my threads or quit prior Search() executions when onKeyPress() fires?
Thanks!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="corp.dtc.tel" >
<application
android:allowBackup="true"
android:icon="#mipmap/tel_ico"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".TEL_Main_Activity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<EditText
android:id="#+id/search_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Enter Here"
android:inputType="text"
android:textSize="12sp"
android:layout_marginLeft="5dp"
android:layout_weight="1" />
<Button
android:id="#+id/search_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Search..."
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:onClick="Search"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="List of Numbers will be here"
android:id="#+id/list_view"
android:maxLines="50"
android:scrollbars="vertical"
android:freezesText="true" />
</LinearLayout>
MainActivity.java
package corp.dtc.tel;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
public class TEL_Main_Activity extends ActionBarActivity {
TextView textView;
EditText editText;
Button button;
Employee[] list;
Employee[] employees;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.mipmap.tel_ico);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
LoadArray la = new LoadArray();
try {
employees = la.LoadArray(this, R.raw.droid);
} catch (IOException e) {}
setContentView(R.layout.activity_tel_main);
textView = (TextView) findViewById(R.id.list_view);
textView.setHorizontallyScrolling(true);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setTypeface(Typeface.MONOSPACE);
button = (Button) findViewById(R.id.search_button);
editText = (EditText) findViewById(R.id.search_box);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Search(findViewById(R.id.layout));
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onResume() {
super.onResume();
try {
//check if any view exists on current view
Button style = ((Button) findViewById(R.id.search_button));
} catch (Exception e) {
Intent i = new Intent();
i.setClass(getApplicationContext(), TEL_Main_Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tel_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void UpdateTextView () {
textView.setText(String.format("%-20s %10s %10s", list[0].name, list[0].number, list[0].support));
for (int x = 1 ; x < list.length & list[x] != null ; x++) {
textView.append("\n" +
(String.format("%-20s %12s %10s", list[x].name, list[x].number, list[x].support)));
}
}
public void Search(View view) {
textView.setText(null);
EditText line = (EditText) findViewById(R.id.search_box);
String[] tokens = line.getText().toString().split(" ");
if (tokens.length == 0)
{
//There was nothing in the search box.
}
else {
list = new Employee[50];
int listCtr = 0; //Keeps ctr for list[]
for (int dbCtr = 0 ; dbCtr < 5000 ; dbCtr++) {
System.out.println(dbCtr);
if (listCtr == 50)
break;
if (employees[dbCtr] == null)
{
//Should have less than 50 listed items and finished searching.
//Now it is okay to update the list view.
UpdateTextView();
break;
}
if (employees[dbCtr].contains(tokens))
{
list[listCtr] = employees[dbCtr];
listCtr++;
}
}
}
}
}
The App purpose: search through company employee listing, display results.(Must have less than 51 results to display)
I don't think that "the other search doesn't stop running" is really a good description of what's going wrong here. I don't see any "threads." I think that the problem is that you're attempting to do the search "on keyPress," therefore "with every keyPress."
A much better way to think of what you're trying to do here would be: "I want to automatically 'push the button' as soon as the user stops typing."
So, basically, when the user starts pressing keys, you'll start a timer (if such a timer isn't already running) set to go off in, say, 1/2-second. Then, in any case, you'll set a flag to true which indicates that "the user has recently pressed a key."
When the timer goes off, it checks to see if this flag is true. If so, it sets the flag to false and reschedules the timer to go-off again in another 1/2-second. Otherwise, it does what "pressing the button" used to do, then it will reset the (separate) flag that indicates that the timer is running.
As long as the user is pressing at least one key every 1/2 second, the timer will continue to reschedule itself (and, no one else will attempt to start the timer since they can see that the timer's running). Eventually, though, the timer will see that the key-has-been-pressed flag has remained false for half-a-second. That's when it "presses the button," causing the search to take place.
For a thorough solution, the timer-routine, after performing the search, would check the "key-has-been-pressed" flag once again. If it's still false, then the user really has stopped typing and it's time to present those search-results. Otherwise, the timer-routine should start rescheduling itself again.
Eventually, the user will stop typing. The timer will run the search, and, having done so, will see that the user still hasn't typed anything more. Results will then be displayed (for the first time).
I am relatively new to Android programming and really need assistance with this problem I am trying to solve. I have an application that revolves around three base activities. In the one activity - the largest by far - I have ten buttons in the UI that implement 16 different fragments (each with their own UIs) in a logical (i.e. step-by-step) fashion depending on what the user wants to accomplish.
So, for each of these 16 different fragments I need to activate and deactivate (enable and disable) various buttons in the UI depending on what the user is allowed to do when a specific fragment is active (i.e. at the front of the view stack or in view or visible to the user). In actual fact, I need to change (i.e. set) the states (i.e. enabled state) of all 10 buttons everytime a new fragment is loaded into the fragment placeholder/container to give the user a clear idea of where they are in this logical process of steps.
Please note: all 10 buttons are visible (i.e. should be visible) at all times (always) and must only be enabled or disabled depending on the fragment that is currently loaded or currently in view (i.e. displayed/visible to the user). OK, so let's look at some code...
Here is the Activity "DmpAct.java" (complete code to date) that I was referring to above...
package com.carzy.carzyapp;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DmpAct extends Activity implements OnClickListener {
Fragment fragment;
Fragment newFragment;
FragmentManager fragMan;
Button birtListBtn, evenListBtn, appoListBtn, todoListBtn, specListBtn, dmpExitBtn;
#SuppressLint({ "NewApi", "CommitTransaction" })
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the Title Bar of the Application --> Must come before setting the Layout...
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Hide the Status Bar of Android OS --> Can also be done later...
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Now you can draw the second Layout --> HomeScreen of the Application...
setContentView(R.layout.dmp_act);
// Instantiate the FragmentManager and FragmentTranstion...
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
// Now you need to define or set the initial/start fragment to be loaded when the view is laid out...
DMPWelcFrag startFragment = new DMPWelcFrag();
fragTrans.add(R.id.dmpFragContainer, startFragment);
fragTrans.commit();
// Instantiate (or get references to) all buttons laid out in this Activity
Button birtListBtn = (Button) findViewById(R.id.dmp_bir_btn);
birtListBtn.setOnClickListener(this);
Button evenListBtn = (Button) findViewById(R.id.dmp_eve_btn);
evenListBtn.setOnClickListener(this);
Button appoListBtn = (Button) findViewById(R.id.dmp_app_btn);
appoListBtn.setOnClickListener(this);
Button todoListBtn = (Button) findViewById(R.id.dmp_tod_btn);
todoListBtn.setOnClickListener(this);
Button specListBtn = (Button) findViewById(R.id.dmp_spe_btn);
specListBtn.setOnClickListener(this);
Button dmpExitBtn = (Button) findViewById(R.id.dmp_exi_btn);
dmpExitBtn.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dmp, menu);
return true;
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// Fragment newFragment;
// Set the DMP Fragment state here and pass it on to the FragmentTransaction module that follows..
if (v.getId() == R.id.dmp_bir_btn) {
newFragment = new BirtListFrag();
}
else if (v.getId() == R.id.dmp_eve_btn) {
newFragment = new EvenListFrag();
}
else if (v.getId() == R.id.dmp_app_btn) {
newFragment = new AppoListFrag();
}
else if (v.getId() == R.id.dmp_tod_btn) {
newFragment = new TodoListFrag();
}
else if (v.getId() == R.id.dmp_spe_btn) {
newFragment = new SpecListFrag();
}
else {
newFragment = new DMPWelcFrag();
}
if (v.getId() == R.id.dmp_exi_btn) {
Intent go2Main = new Intent(DmpAct.this, MainAct.class);
startActivity(go2Main);
}
else;
FragmentTransaction transact = getFragmentManager().beginTransaction();
transact.replace(R.id.dmpFragContainer, newFragment, "activeFrag");
transact.addToBackStack("activeFrag");
primeRelativeBtns();
transact.commit();
};
#SuppressLint("NewApi")
public void primeRelativeBtns() {
if (newFragment.equals("dmpBirtListFragTag")) {
birtListBtn.setEnabled(false);
evenListBtn.setEnabled(true);
appoListBtn.setEnabled(true);
todoListBtn.setEnabled(true);
specListBtn.setEnabled(true);
}
else if (newFragment.getTag() == "dmpEvenListFragTag") {
birtListBtn.setEnabled(true);
evenListBtn.setEnabled(false);
appoListBtn.setEnabled(true);
todoListBtn.setEnabled(true);
specListBtn.setEnabled(true);
} else;
}
}
Please note...at the time of submitting this question I had only completed 6 of the 10 buttons used by this activity (as is obvious in the code above)...
Anyway...on we go...
Here is the UI that is implemented for this activity by the "dmp_act.xml" file...
<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="horizontal"
android:baselineAligned="false"
tools:context=".DmpAct">
<LinearLayout
android:id="#+id/leftButtonColumn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2705"
android:gravity="center"
android:paddingTop="5pt"
android:paddingBottom="10pt"
android:paddingLeft="8pt"
android:paddingRight="8pt"
android:orientation="vertical">
<ImageView
android:id="#+id/dmp_cat_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0180"
android:contentDescription="#string/dmp_cat_sign"/>
<Button
android:id="#+id/dmp_bir_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_eve_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_app_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_tod_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"
android:layout_marginBottom="13pt"/>
<Button
android:id="#+id/dmp_spe_btn"
android:layout_width="wrap_content"
android:layout_height="23pt"
android:layout_weight="0.0164"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dmpFragContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5350"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="8pt"
android:paddingBottom="8pt">
<!-- <ImageView
android:id="#+id/dmp_wel_sta_wal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="1"
android:contentDescription="#string/dmp_welc_wall"/> -->
</LinearLayout>
<LinearLayout
android:id="#+id/rightButtonColumn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1795"
android:gravity="center"
android:paddingTop="20pt"
android:paddingBottom="20pt"
android:paddingLeft="8pt"
android:paddingRight="8pt"
android:orientation="vertical">
<Button
android:id="#+id/dmp_edi_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_sav_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_add_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_del_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"
android:layout_marginBottom="15pt"/>
<Button
android:id="#+id/dmp_exi_btn"
android:layout_width="wrap_content"
android:layout_height="0pt"
android:layout_weight="1"/>
</LinearLayout>
Before posting here I tried all the available solution already discussed here in SO...but to no avail. So to summarize the problem...I basically need a really good way (best-practise) of setting or changing all the buttons' enabled states everytime a new fragment is loaded (and displayed) to the user in the "dmpFragContainer".
I want to apologize beforehand if it seems like I am talking down to anyone, but I want to make sure everyone that reads this post will clearly understand the problem at hand. Please feel free to shred my code if you think I can implement better "best-practise" code structure - as I said before...I am new to Android coding and need all the help I can get.
Appreciate the help...
Cheers,
SilSur.
Thweeet! I solved the problem. Hello all... I managed to solve this problem I had with enabling or disabling all ten buttons just the way I want them to be disabled everytime a new fragment is loaded and visible to the user. The solution is that you have to implement this "enable/disable" code in the class files for each of the fragments. Pretty straightforward actually... So without further ado here is the solution in a more understandable fashion [code]...
This is the code from only one "BirtListFrag.java" of the 16 fragments I have running through my application...
package com.carzy.carzyapp;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
#SuppressLint("NewApi")
public class BirtListFrag extends Fragment {
public static Button BirCatBtn;
public static Button EveCatBtn;
public static Button AppCatBtn;
public static Button TodCatBtn;
public static Button SpeCatBtn;
public static Button EdiFunBtn;
public static Button SavFunBtn;
public static Button AddFunBtn;
public static Button DelFunBtn;
public static Button ExiFunBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dmp_birt_list_frag, container, false);
}
#Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
/*** Get references to all the buttons you want to manipulate everytime this Fragment is loaded & visible in the viewport ***/
BirCatBtn = (Button) getActivity().findViewById(R.id.dmp_bir_btn);
EveCatBtn = (Button) getActivity().findViewById(R.id.dmp_eve_btn);
AppCatBtn = (Button) getActivity().findViewById(R.id.dmp_app_btn);
TodCatBtn = (Button) getActivity().findViewById(R.id.dmp_tod_btn);
SpeCatBtn = (Button) getActivity().findViewById(R.id.dmp_spe_btn);
EdiFunBtn = (Button) getActivity().findViewById(R.id.dmp_edi_btn);
SavFunBtn = (Button) getActivity().findViewById(R.id.dmp_sav_btn);
AddFunBtn = (Button) getActivity().findViewById(R.id.dmp_add_btn);
DelFunBtn = (Button) getActivity().findViewById(R.id.dmp_del_btn);
ExiFunBtn = (Button) getActivity().findViewById(R.id.dmp_exi_btn);
/*** Now you can manipulate whatever attributes (***See Below***) of the buttons u created references to ABOVE ***/
BirCatBtn.setEnabled(false);
EveCatBtn.setEnabled(true);
AppCatBtn.setEnabled(true);
TodCatBtn.setEnabled(true);
SpeCatBtn.setEnabled(true);
EdiFunBtn.setEnabled(false);
SavFunBtn.setEnabled(false);
AddFunBtn.setEnabled(true);
DelFunBtn.setEnabled(false);
ExiFunBtn.setEnabled(true);
}
}
As you can see...it is pretty logical. Anyway. I owe this breakthrough to "Greensy" who answered a question posted by "ColorFrog" on a similar problem that I had ==> here is the jump if you want to check out what I am talking about..."Disabling buttons in a Fragment".
Anywho...at the time of posting this reply I had been a member of S.O. for only 5 days and thus I couldn't "upvote" Greensy's answer as I didn't have that privilege at the time. So, since his answer really helped me solve my problem, I decided the least I could do was post this reply and thank him/her upfront. So thanks mate...I definitely owe u something (hmm...a beer maybe?!). Who knows. Anyway, I hope a will get to return the favor one day.
Cheers,
Shore-T.
I am new to the Android development world and I've built a simple "Hello World" app. First, activity requests a text. When the "Go" button is clicked, the app launches the second activity displaying the input text.
If I click the HOME button and then click the application icon, the app launches the first activity again but if I press-hold the home button and click the icon from the "Recent apps" bar, it resumes the app where I left.
How do I avoid this?
I need my app to resume even if the launcher icon is clicked.
MainActivity.java,
package com.example.myfirstandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view){
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txtName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayActivity.java,
package com.example.myfirstandroidapp;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#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);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_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"
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" >
<EditText
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignParentRight="true"
android:onClick="sendMessage"
android:text="Go!" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtName"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Please input your name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
activity_display_message.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"
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=".DisplayMessageActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstandroidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstandroidapp.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.example.myfirstandroidapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstandroidapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstandroidapp.MainActivity" />
</activity>
</application>
</manifest>
Problem:
I'm not qualified to say this a bug, but there is a behaviour with release builds when starting the application from the launcher. It seems that instead of resuming the previous Activity, it adds a new Activity on top. There is a related bug report on this topic here.
Solution:
I'm working around this this by closing the Launcher Activity if it's not the root of the task, as a result the previous Activity in that task will be resumed.
if (!isTaskRoot()) {
finish();
return;
}
Issue for me was whenever app in installed by an apk with the click on 'Open' option it used to relaunch every time when we minimize it.
resolved it by
SplashActivity.java:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!isTaskRoot
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& intent.action != null
&& intent.action.equals(Intent.ACTION_MAIN)) {
finish()
return
}
}
I simplified my problem to the smallest example when it can be reproduced.
So:
1 activity with VideoView and ImageView.
After clicking on ImageView AlertDialog is showed.
AlertDialog have 1 EditText field.
I touch this EditText and standard Android keyboard is showed.
Close keyboard.
Close dialog.
Problem: VideoView's borders (black rectangle) were extended and thus ImageView is not showed anymore.
Any help is appreciated! Thanks.
Code:
MainActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.VideoView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity act = this;
final VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath("/mnt/sdcard/s1ep01.mp4");
videoView.requestFocus();
findViewById(R.id.imageView1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(act);
View view = LayoutInflater.from(act).inflate(R.layout.dialog, null);
builder.setView(view);
builder.setPositiveButton("Save translation", null);
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
activity_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"
tools:context=".MainActivity" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/videoView1"
android:src="#android:drawable/btn_dialog" />
</RelativeLayout>
dialog.xml
<?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="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
as a Temporary solution, i created a Runnable that makes the VideoView Invisible, then makes it Visible after 200 milli seconds :
// Hide the VideoView
videoLayout.setVisibility(View.INVISIBLE);
// create a handler to handle the delayed runnable request
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// After the 200 millis (which are passes as a second parameter to this postDelayed() method in the last line of the code, this handler will invoke the following method :
// run on UI so you can set the Visibitity of the UI elements
runOnUiThread(new Runnable() {
#Override
public void run() {videoLayout.setVisibility(View.VISIBLE);} // make it visible again
});
}
}, 200); // this is the second parameter which decides when this handler will run it's run() method
hope this temporary solution helps for now
call this methods when your keyboard is hiding, alike on ENTER key, or when you touch a certain view outside the keyboard to hide it ... but dont put it in the onTouchEvent() of the activity or in the onUserInteraction so not to keep flashing
Since im quite new to Java and Android apps, i had to ask this question because im working for 10 hours and couldnt achive anything. I made a full screen web app in eclipse but i cant see videos in there. Here's my codes:
Tarim.java
package com.tarim.tarimvideo;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import com.tarim.tarimvideo.util.SystemUiHider;
#SuppressLint("SetJavaScriptEnabled")
public class Tarim extends Activity {
private static final boolean AUTO_HIDE = true;
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
private static final boolean TOGGLE_ON_CLICK = true;
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
private SystemUiHider mSystemUiHider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tarim);
final View controlsView = findViewById(R.id.fullscreen_content_controls);
final View contentView = findViewById(R.id.fullscreen_content);
WebView webView = (WebView)findViewById(R.id.tarayici);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.youtube.com/watch?v=W8SCqLkHDqw");
// Set up an instance of SystemUiHider to control the system UI for
// this activity.
mSystemUiHider = SystemUiHider.getInstance(this, contentView,
HIDER_FLAGS);
mSystemUiHider.setup();
mSystemUiHider
.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
// Cached values.
int mControlsHeight;
int mShortAnimTime;
#Override
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public void onVisibilityChange(boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
// If the ViewPropertyAnimator API is available
// (Honeycomb MR2 and later), use it to animate the
// in-layout UI controls at the bottom of the
// screen.
if (mControlsHeight == 0) {
mControlsHeight = controlsView.getHeight();
}
if (mShortAnimTime == 0) {
mShortAnimTime = getResources().getInteger(
android.R.integer.config_shortAnimTime);
}
controlsView
.animate()
.translationY(visible ? 0 : mControlsHeight)
.setDuration(mShortAnimTime);
} else {
// If the ViewPropertyAnimator APIs aren't
// available, simply show or hide the in-layout UI
// controls.
controlsView.setVisibility(visible ? View.VISIBLE
: View.GONE);
}
if (visible && AUTO_HIDE) {
// Schedule a hide().
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
}
});
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
mSystemUiHider.hide();
}
};
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tarim.tarimvideo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.tarim.tarimvideo.Tarim"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity.tarim.xml
<FrameLayout 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:background="#0099cc"
tools:context=".Tarim" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<TextView
android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="#string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="#color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<Button
android:id="#+id/dummy_button"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/dummy_button" />
</LinearLayout>
<WebView
android:id="#+id/tarayici"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</FrameLayout>
Please note that im in this Java thing just for 10 hours, so if you can explain me as much as simple, it would be so great.
It does not look like it is possible in a WebView without the assistance of flash. Please look at this: Youtube in a webview (used for reference). And look at this for a flash demo: Flash demo. Lastly this might also be helpful, it is Android's YouTube API.
Just put a .html file in your SDCARD which contain your video link.(mention video link as content:\\).and load that .html file to the webview.