How to do something like "Touch anywhere to continue"? - android

How would you do something like "Touch Anywhere to Continue."

Set an onclick listener for the layout of the entire screen

It's as simple as this.
Give an id to "main" layout
set the listener to the main layout
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/mainLayout"> //<--- Provide ID
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
In this case, the main element is RelativeLayout. So add the listener to RelativeLayout to implement Touch anywhere to continue.
public class MainActivity extends AppCompatActivity {
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout)findViewById(R.id.mainLayout);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}

Related

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

android. run two events with an action

I have a RelativeLayout in FragmentActivity that looks like this:
<?xml version="1.0" encoding="utf-8"?>
...
<com.my.android.Myview
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</com.my.android.Myview>
I have a OnTouchListener in com.my.android.Myview.
And OnClickListener for RelativeLayout in FragmentActivity .
The problem is that I can not call the two events.
I managed to call one if not the other. But not the two events
Im not sure if what you are trying is a good idea.
Anyways, you can try something like this:
OnClickListener parent= new OnClickListener() {
public void onClick(View v) {
//whatever
}
}
OnClickListener child= new OnClickListener() {
public void onClick(View v) {
parent.onClick(null);
//whatever else
}
}
parentView.setOnClickListener(parent);
childView.setOnClickListener(child);

How to create a buttonlistener for an inflated button

Am quite new to Android programming and hope someone can help me out in this:
1. I am coding from one of the demos here:
http://developer.android.com/training/animation/screen-slide.html
I have a main activity_screen_slide.xml file:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
In another xml file, main.xml, I have a few buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonStart"
android:text="test"/>
</LinearLayout>
Within the main java activity file, the context is set as following:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
When I tried to set a listener for testButton, am unable to do so. This means that when the button is generated subsequently (through an inflator in another java file), there is no response when I click the button.
The java file to generate the inflator is simply:
ViewGroup rootView;
rootView = (ViewGroup) inflater.inflate(R.layout.main, container, false);
Thank you and appreciate your help
Cheers
D
I assume your button is defined in your xml layout activity_screen_slide.xml similar to this:
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
In your Activity you can add a ClickListener like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("", "I've been clicked");
}
});
}
Try this:
public class Settings extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
//...ALL THE OTHER CODE..\\
Button button = (Button) findViewById(R.id.YOUR_BUTTON_ID);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.YOUR_BUTTON_ID: {
ALPHA.YourFunction(parameters);
}
}
}
}

How to show different layouts in one activity?

I have a activity in that I need to change the layout.
In the first layout I have four buttons to display and in the second I need a GridView to display images.
I need to show the second layout in an AsyncTask onPostExecute method.
For now, I'm trying to set two setContentViews, but I get the following exception: ClassCastException
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focusarea);
videoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
animateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
pdfBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadPDFFiles().execute();
}
});
}
And in my postExecute i try like this
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
setContentView(R.layout.gallery);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}
});
}
});
Instead of using two layouts use a single layout as bellow
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/MyLayoutOne"
android:layout_width="fill_parent"
android:visibility="gone"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi This is my first layout" />
<!-- Your first layout contents add here-->
</LinearLayout>
<LinearLayout
android:id="#+id/MyLayoutTwo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi This is my Second layout" />
<!-- Your second layout contents add here -->
</LinearLayout>
</LinearLayout>
Add your first layout contents inside MyLayoutOne and second layout contents inside MyLayoutTwo
And use following codes inside your activity,
public class MainActivity extends Activity {
LinearLayout MyLayoutOne;
LinearLayout MyLayoutTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyLayoutOne=(LinearLayout)findViewById(R.id.MyLayoutOne);
MyLayoutTwo=(LinearLayout)findViewById(R.id.MyLayoutTwo);
// this will make first layout visible
MyLayoutOne.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
MyLayoutTwo.setVisibility(View.GONE);
//=========================================
//in your post create add this codes
//=========================================
// this will make first layout hidden
MyLayoutOne.setVisibility(View.GONE);
// this will make second layout visible in your layout
MyLayoutTwo.setVisibility(View.VISIBLE);
//=========================================
}
}
This is a simplest method you must study fragments for better UI management. You can use viewflipper also.
So Study Fragments and Viewflipper..
Instead of that you can have a layout that contains both a wrapper for your four buttons and other for the GridView while this last one is with visibility set to 'gone'.
When the AsycTask finished, you hide the buttons layout and show the GridView layout.
Why don't you set one content view with two layouts or two fragments? A layout can be like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text1">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text2">
</Button>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text3">
</Button>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text4">
</Button>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</GridView>
</LinearLayout>
Use one setContentView() and define separates Linear/Relative layout one for buttons and second for gridView.And hide/show the Views according to your need.
onPostExecute of AsyncTask runs on UI thread so you need not specify runonUiThread explicitly.Instead of using 2 setcontent View it is better to have 2 views in your layout file and make it visible invisible as required.
Friend change the id of wrapper1 to child as below,
<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"
tools:context=".Focusarea" >
<LinearLayout
android:id="#+id/wrapper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<GridView
android:id="#+id/gridView1_bir"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
</GridView>
</LinearLayout>
<RelativeLayout
android:id="#+id/wrapper2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/vid_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
and initialize your linear layouts outside oncreate as below,
LinearLayout wrapper1;
RelativeLayout wrapper2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wrapper1 = (LinearLayout)findViewById(R.id.wrapper1);
wrapper2=(RelativeLayout)findViewById(R.id.wrapper2);
// this will make first layout visible
wrapper2.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
wrapper1.setVisibility(View.GONE);
ImageView videoBtn = (ImageView) findViewById(R.id.vid_btn);
ImageView animateBtn = (ImageView) findViewById(R.id.anit_btn);
ImageView pdfBtn = (ImageView) findViewById(R.id.pdf_btn);
videoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
animateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
pdfBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadPDFFiles().execute();
}
});
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting VIDEOS
pDialog.dismiss();
vid=new ArrayList<String>(new ArrayList<String>(vid));
videoUrl=parsing.parse(videoUrl);
System.out.println("VIDEO URL" +videoUrl);
runOnUiThread(new Runnable() {
public void run() {
//--here you wont need to initialize again--
// this will make first layout visible
wrapper1.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
wrapper2.setVisibility(View.GONE);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
//ListView gibi buna da adapter set ediliyor.
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}
});
}
});

Set button clickable property to false

I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?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="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});

Categories

Resources