I want to use clickable text (Android) - android

I want to use clickable text to move from main activity to another activity, can somebody give me a sample code to understand that how this works.

TextView textview = (TextView) findViewById(R.id.textView);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,AnotherActivity.class);
startActivity(i);
}
});

In the layout of your activity, define the onclick on the textview item.
<TextView ....
android:onClick="onClick"
android:clickable="true" ... </TextView>
OnClick Method
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);

Related

Image button not responding with new activity

i have some image buttons in my main layout, and some of them are made to start new activities but when i install the app in my phone the image buttons do not make nothing. please help.
heres my code in main activity
ImageButton botondiseno = (ImageButton) findViewById(R.id.Botondiseno);
botondiseno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
and my layout
<ImageButton
android:id="#+id/Botondiseno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="0dp"
android:layout_marginTop="224dp"
android:background="#null"
android:clickable="true"
android:contentDescription="#string/accept"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/botondisenointeriores" />
I believe you have to use AppCompatImageButton instead of just ImageButton
Like,
AppCompatImageButton botondiseno = (AppCompatImageButton) findViewById(R.id.Botondiseno);
botondiseno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
Did you see your button? Set the "src" attribute of your ImageButton to make it visible:
android:src="#drawable/xxxxxxxxxxxxxx"
I just ran ur code at new proj and its working fine, the ImageButton botondiseno is working fine and change the activity to Main2Activity. the only change i made to modify the code,i changed at the .xml the line:
app:srcCompat="#drawable/botondisenointeriores"
to:
app:srcCompat="#drawable/my_own"
and this because i dont have that drawable.
it looks like there is something else in ur code that is not working, maybe u can post all of it.
maybe u should try to Rebuild ur project

Android colouring app multiple background images

I am creating an android studio colouring app and need to have it so that the user can select which image they want to colour. I want it so that they click on an image button on one activity and this changes the background of the next activity that the button takes them to however I have no idea how to go about this. Any help would be greatly appreciated. Thankyou
From what I understand, you want to change the color of the next activity on button click of current activity. So, what you can do is:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredColor to get the color according to your logic
intent.putExtra("color", getDesiredColor());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("color")){
rootLayout.setBackgroundColor(intent.getExtra("color"));
}
...
}
Let me know in case you have any doubts.
UPDATE:
With drawable your code will become something like this:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("drawable")){
rootLayout.setBackground(intent.getExtra("drawable"));
//Or if you are using ImageView in your root layout to set the background image (I'm using Picasso here):
//Picasso.with(mContext).load(intent.getExtra("drawable")).into(myBackgroundImageView);
}
...
}
UPDATE 2:
You can have a hashmap mapping each imagebutton with a drawable.
e.g.
HashMap<Integer, Integer> mViewIdToDrawableMap = new HashMap<>();
mViewIdToDrawableMap.put(mImageButton1.getId(), R.drawable.image1);
mViewIdToDrawableMap.put(mImageButton2.getId(), R.drawable.image2);
mViewIdToDrawableMap.put(mImageButton3.getId(), R.drawable.image3);
public int getDesiredDrawable(View view){
return mViewIdToDrawableMap.get(view.getId());
}
How will you call this function:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable(view));
mContext.startActivity(intent);
}
});
Now, your last question what is rootLayout?
Lets say your activity2 where you want to show this image has somehitng like this as layyout:
<RelativeLayout>
<ImageView
...
android:id="id+/background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
...
/>
</RelativeLayout>
In your Activity2's onCreate, do somehting like this (after getting the drawable as I explained earlier):
//Here mBackgroundImageView is the background_imageview in your layout
Picasso.with(mContext).load(drawable).into(mBackgroundImageView);

Using setOnClickListener() in dynamically generated TextView

This is the TextView Layout.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trailer"
android:paddingTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp"/>
This is the main root layout.
<LinearLayout
android:id="#+id/review_layout"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
And this piece of code add the textViews as childView of the above linear layout, based on total number of response from server.
#Override
public void addData(List<MovieVideoData> movieVideoDataList) {
trailerLinearLayout.removeAllViews();
List<TextView> textViews = new ArrayList<TextView>();
for (MovieVideoData movieVideoData:movieVideoDataList){
View view = getActivity().getLayoutInflater().inflate(R.layout.trailer_text_view,trailerLinearLayout,false);
((TextView)view.findViewById(R.id.trailer)).setText(movieVideoData.getName());
textViews.add(((TextView)view.findViewById(R.id.trailer)));
trailerLinearLayout.addView(view);
}
}
Now, for each TextView objects which was added in the Linear Layout, on the click of those i want to open an youtube video link specific to that textView.
I was wondering, how should i use the setOnClickListener() on each textView as id's of all are same.
Any suggestions, would be appreciated.
I added this piece of code and it's working fine.
private void watchYoutubeVideo(String key){
try{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + key));
startActivity(intent);
}catch (ActivityNotFoundException ex){
Intent intent=new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v="+key));
startActivity(intent);
}
}
textView.setText(movieVideoData.getName());
trailerLinearLayout.addView(view);
final String key = movieVideoData.getKey();
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
watchYoutubeVideo(key);
}
});
On click of each textview, it's opening its respective link on youtube, which is fine.
What i am not sure of is, after movieVideoDataList completes looping, and then on click of text view, how it remembers the specific key, as it inside the loop.
Any type of clarification on this would be appreciated.
I might be missing something, but why not just do it like this:
#Override
public void addData(List<MovieVideoData> movieVideoDataList) {
trailerLinearLayout.removeAllViews();
List<TextView> textViews = new ArrayList<TextView>();
for (MovieVideoData movieVideoData:movieVideoDataList){
View view = getActivity().getLayoutInflater().inflate(R.layout.trailer_text_view,trailerLinearLayout,false);
TextView tv = (TextView)view.findViewById(R.id.trailer);
tv.setText(movieVideoData.getName());
textViews.add(tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the url of your video and action you want to perform
}
});
trailerLinearLayout.addView(view);
}
}

Android position buttons at bottom of screen

Hi i'm having an issue with placing buttons at the bottom of the screen i have laid it out and in the graphical layout in eclipse it looks exactly how i want it but when i run it on the device the buttons both go to the very bottom of the screening the same position. so only one button is visible. does anyone know why this is or do you know how to place thing s at the bottom with a bit of padding?
heres what i have tried
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/ic_background_credits"
android:orientation="vertical" >
<Button
android:id="#+id/fm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:background="#drawable/fmbtn" />
<Button
android:id="#+id/ph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fm"
android:layout_alignLeft="#+id/fm"
android:background="#drawable/visitphbtn" />
</RelativeLayout>
heres my code where i inflate this view
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Check Preferences which sets UI
setContentView(R.layout.singlenews);
findViewById(R.id.progress).setVisibility(View.GONE);
loadData();
Button backbtn = (Button) findViewById(R.id.backbtn);
//Listening to button event
backbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent previousScreen = new Intent(getApplicationContext(), InfoActivity.class);
startActivity(previousScreen);
}
});
}
public void loadData(){
name = getIntent().getStringExtra("name");
Log.v("lc", "infoname=" + name);
if (name.equals(name1")) {
NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.infodetail,
null);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("About name1");
TextView mainText = (TextView) NewsView.findViewById(R.id.maintext);
mainText.setText("name1");
ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2);
NewsImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_evostik));
}
if (name.equals("name2")) {
NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.infodetail,
null);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("About name2");
TextView mainText = (TextView) NewsView.findViewById(R.id.maintext);
mainText.setText("name2");
ImageView NewsImage = (ImageView)NewsView.findViewById(R.id.imageView2);
NewsImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_logo_pitchero));
}
if (name.equals("name3")) {
NewsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.creditsdetail,
null);
TextView headerText = (TextView) findViewById(R.id.header_text);
headerText.setText("name3");
Button phbtn=(Button)NewsView.findViewById(R.id.ph);
phbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Uri uri = Uri.parse("http://www.pitchero.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Button fmbtn=(Button)NewsView.findViewById(R.id.fm);
fmbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Uri uri = Uri.parse("http://www.fantasticmedia.co.uk/mobile/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
ListView list = getListView();
list.setVerticalFadingEdgeEnabled(false);
Log.v("BGThread", "Filled results");
adapter = new MergeAdapter();
adapter.addView(NewsView);
setListAdapter(adapter);
}
}
I think you have some attributes mixed up.
In your second button don't add the '+' operator to id because it's making a new one. Just remove it and leave the '#'.
Remember '+' adds to id and '#' references it.
Well relative layout is relative to the other items inside the RelativeLayout container which is a ViewGroup. So for example you could specify android:layout_leftOf="#+id/fm" in the description of pm. If you want padding you might also expiriment with margin and padding like inside the buttons.
android:layout_marginBottom="20dip"
android:layout_leftOf="#+id/fm"
android:paddingBottom="10dip"
I actually try your Layout in my project and the two buttons are correctly placed.
Are you sure you didn't make any other actions in your code ?

Changing Activity on a clickable layout

I have this clickable LinearLayout view, im trying to have it change Activity when clicked by every time i click the object i get a error.
final LinearLayout lindet = (LinearLayout) findViewById(R.id.detials);
lindet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(SellingActivity.this, DetailsActivity.class);
startActivity(i);
finish();
}
});
Did you remember to add the DetailsActivity to the AndroidManifest?
Supporting Macarse I would add that use SellingActivity.this.finish() instead of finish()
I believe this will prove to be your problem solver.

Categories

Resources