Probably a really basic answer to this but google is throwing up nonsense.
Ok, so i have a bunch of nested linear layouts, each one contains a textview and an imageview. What i want is my textview to be linked so that when a user clicks on the text, it will take the user to a new page that is in the same project. Not a website or anything.
Appreciate any help!!
You can add an onTouchListner to you TextViews and when user clicks on it you simply launch a new Activity. Google for adding touch listeners and then for launching activities and you will find the components you need.
Activities are like pages in Android apps.
Yeah, Juhani's right. You can use an onTouchListener. It's actually really simple. Just create a new .java file that loads the Layout you want in the onCreate. In the code you just use this line for the onClickListener:
startActivity(new Intent(this, newjavafile.class));
and add the new Activity to your manifest. I had something close to this exact problem. The nice thing about doing it this way is the back button on the phone/device works to get you back to the main screen.
First write attribute in TextView Like,
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Click Here" />
And in Java File
TextView text= (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,SecondActivity.class));
}
});
Related
I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.
I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now
I'm making a button in xml, like this:
<Button
android:id="#+id/buttondp"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thisisstringtext" />
and I want it to direct it to another page coded in xml. Can anyone help me out?
Make another activity and use
setContentView(R.layout.your_other_layout);
inside of it.
Then in the onClickListener for your button put this:
Intent i = new Intent(YourActivity.this, YourOtherActivity.class);
startActivity(i);
You can add the onclick listener to your button, so: android:onclick="method_in_your_activity".
In your activity added the method (method_in_your_activity) and add startActivity(NewActivity).
If you dynamically want to change the Content of your activity you can always call setContentView(my_layout) and change the content. However; its best practice to use another Activity.
You can use different activities for different layout. But if you want to use same activity for different layout then you should go for ViewFlipper . You can also get some animation when switching from one view to another. Tutorial regarding the same can be found here.
using this code in java file you will click button to redirect next page
public void onClick(View v)
{
Intent ia=new Intent(getApplicationContext(),second.class);
startActivity(ia);
}
In your button XML, add :
android:onCLick="myRedirectFunction"
In your MyMainActivity.java, add function named myRedirectFunction and inside that function :
Intent homepage = new Intent(MyMainActivity.this, MySubActivity.class);
startActivity(homepage);
I'm with some really strange trouble with my android click listener button! I've already done that several times, I'm getting crazy not founding an solution (neither an logical explanation) for this error.
error
The event handler for 2 buttons on my activity are not being executed. There is no error, it just not performance the handler action at runtime. This is the code for one of the buttons:
btnNext = (Button) findViewById(R.listclient.btnnext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MyActivityClassName.this, "Flag 01", 1).show();
btnNext.setText("CLICKED!");
}
});
And that's the button on xml layout:
<Button android:id="#+listclient/btnnext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
style="#style/Widget.TextViewInfo"
android:text="Next"
/>
info
There is also an ImageButton inside this activity, this imageButton works perfectly with an inner OnClickListener class (just like this one).
I have already tried to make my activity class inherit OnClickListener and set it as the click listener for the button with no success.
I have also created an class inside my Activity class, and set it as the button click listener, no success too.
I'm compiling for Android 2.1 + Google API (SDK 7)
------------EDITED-----------------
If I put in my code:
btnNext.performClick();
It's executed! I'm getting even crazy right now!
And the button is in fact clicked when I touch it, I can see the button "animation", and the click is logged in LogCat.
You can't use listclient when specifying or using an id. The first part is the type of the resource, which has to be id in your case.
Change android:id="#+listclient/btnnext" to android:id="#+id/btnnext". Also adjust your code:
btnNext = (Button) findViewById(R.id.btnnext);
At a quick glance over you code, I noticed when you don't call findViewById correctly. Change the id of your Button to "test" then try: findViewById(R.id.test). Make sense?
How do I create on-screen buttons for android apps. I am using the SDK's Lunar Lander game as a base, and I want to make it so that you don't need a keyboard.
To create a button you add something like this in your layout XML file:
<Button
android:id="#+id/button_1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:text="Button 1"/>
To get a hook to the button in your code where you can add some action when the button is pressed add an OnClickListener:
findViewById(R.id.button_1).setOnClickListener(new MyButtonListener());
Declare the OnClickListener as a private class (or inline)
private class MyButtonListener implements OnClickListener {
#Override
public void onClick(View v) {
// Your code doing something cool goes here...
System.out.println("Click!");
}
}
Please look in the documentation that Mr Dittmar posted links to for more details, but this should hopefully get you started. :)
Take a look at the documentation on how to create a layout. After that, the documentation on how to create and handle buttons might come handy.
I made a textview clickable then it triggers an intent, it works but only once. After clicking the textview the first time it's no longer clickable and I have no idea why. Your help will be appreciated.
<TextView android:text="Click Me" android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="#+id/textView1"
android:textSize="50dp" android:focusable="false" android:longClickable="true"></TextView>
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Example.this, Alert.class));
return false;
}
});
What does the alert class do? If it is an activity it could be that it is being laid over the top of your current activity so although you can see your activity, it's not at the top of the stack/in the foreground so you're not actually pressing the TextView, you're pressing a transparent activity that is over the top of it.
The easiest way to check that is to press the TextView, then press your device's back key and see if the TextView responds to the click.
Or are you sure you're not setting the same layout in Alert.class? That would make it look like it's the same activity but if the Alert class doesn't set the click listener, nothing is going to happen.
The fact that you're starting an activity with an intent and that's making an instance of another class (which I assume is also an activity) stops the click working to me is seriously suggesting that Alert is getting the click somehow instead of Example. When you say things work fine if you remove the intent backs that up as well. Maybe you could post the full source of both classes?
Do you have an onClickListener that disables the textview?
By returning false from onLongClick Android would also invoke the onClick listener if you have one.
Also you could try to remove android:focusable="false"