Android position buttons at bottom of screen - android

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 ?

Related

I want to use clickable text (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);

startActivity() inside setOnClickListener not doing anything

I have a button inside a view that checks some constraints, and then starts another activity, however the startActivity() call does not do anything, the new activity's onCreate() never gets called, and the code then continues past it. I have checked using logging and breakpoints, and the conditions are being met and startActivity() is being called. Both activities are defined in the application manifest.
From the source activity's onCreate():
int[] winning_player;
int next_player;
int[] scores;
[...]
final Button end_turn_button = (Button) findViewById(R.id.end_turn);
end_turn_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (winning_player[0] == next_player) {
Intent intent = new Intent(getApplicationContext(), FinalScreenActivity.class);
intent.putExtra("SCORES", scores);
startActivity(intent);
}
}
});
FinalScreenActivity.java
public class FinalScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_screen);
Intent intent = getIntent();
final int[] scores = intent.getIntArrayExtra("SCORES");
// put the scoreboard in ascending order
Arrays.sort(scores);
// find the scoreboard in the view
TableLayout scoreboard = (TableLayout) findViewById(R.id.scoreboard);
// get the string resources to be formatted
Resources res = getResources();
String player_name_format = res.getString(R.string.player);
// Set the scoreboard in the view
Log.d("brains.PlayActivity", "onCreate: Creating scoreboard");
// for each player entry in the scoreboard
for (int i = 0; i < scores.length; i++) {
Log.d("brains.PlayActivity", "onCreate: adding scoreboard entry "+String.valueOf(i));
// create 2 TextViews
TextView player_name = new TextView(FinalScreenActivity.this);
TextView player_score = new TextView(FinalScreenActivity.this);
// Set the size of the TextViews
player_name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
// set the first one to the player's name string resource
player_name.setText(String.format(player_name_format, i + 1));
// set the second one to the player's score resource, which takes the score twice (once for the number itself, and once to work out which plural is needed)
player_score.setText(res.getQuantityString(R.plurals.brains, scores[i], scores[i]));
// Create a TableRow to put the score into
TableRow scoreboard_entry = new TableRow(FinalScreenActivity.this);
// Set the size of the TableRow
scoreboard_entry.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
// Add the TextViews to the TableRow
scoreboard_entry.addView(player_name);
scoreboard_entry.addView(player_score);
// Add the TableRow to the TableLayout
scoreboard.addView(scoreboard_entry);
}
// Get the title text
TextView title_text = (TextView) findViewById(R.id.title_text);
title_text.setText(res.getQuantityString(R.plurals.win_brains, scores[scores.length - 1], scores.length - 1, scores[scores.length - 1]));
// set the play again button
Button play_again_button = (Button) findViewById(R.id.play_again_button);
play_again_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), SelectPlayersActivity.class);
startActivity(intent);
}
});
}
}
and activity_final_screen.xml
<?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-auto"
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="uk.co.bluesapphiremedia.android.zombiedice.FinalScreenActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/title_text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/scoreboard"
android:layout_below="#+id/title_text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/play_again"
android:id="#+id/play_again_button"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
end_turn_button.setOnClickListener(this);
//override onClick() method in activity implement interface View.OnClickListner
public void onClick(View v) {
if(v.getId() == R.id.end_turn){
if (winning_player[0] == next_player) {
Intent intent = new Intent(this,FinalScreenActivity.class);
intent.putExtra("SCORES", scores);
startActivity(intent);
}
}
}

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);
}
}

change textview background until select another textview in android

I have to develop an android application.
I have using below textview:
<TextView
android:id="#+id/deals"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:background="#drawable/best_deal_bg"
android:text="Deals"
/>
This is background of these textview:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/menu_active" android:state_enabled="true"
android:state_pressed="true" />
</selector>
Here .,If i have to click these textview means that tiime ly shows background.
But i wish to need the output like.have to shows background until select another textview.
How can i do ?? please give me solution for these ???
EDIT:
i have set that background on android textview onclik functionality:
deals = (TextView) findViewById(R.id.deals);
deals.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
deals.setBackground(getResources().getDrawable(R.drawable.best_deal_bg));
Intent intent = new Intent(getParent(),Deals.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("BestDeals",intent);
}
});
Here am getting below error :
*07-11 10:07:50.593: E/AndroidRuntime(816): java.lang.NoSuchMethodError: android.widget.TextView.setBackground
*
EDIT:
deals = (TextView) findViewById(R.id.deals);
deals.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
View lastclickedview=null;
if(v != lastclickedview) {
v.setBackground(getResources().getDrawable(R.drawable.best_deal_bg));
lastclickedview.setBackground(getResources().getDrawable(R.drawable.message_icon));
}
lastclickedview = v;
Intent intent = new Intent(getParent(),Deals.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("BestDeals",intent);
}
});
Here am getting below error :
E/AndroidRuntime(883): java.lang.NoSuchMethodError: android.view.View.setBackground
What's worng in my code.pls give me solution for these ???
Dynamically change it in your code on selection of another TextView use
yourTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.best_deal_bg));
and if you want remove the background use
yourTextView.setBackground(null);
first change your textview's background color from your onclickListener and create a view object to save last clicked text view, in second time click on the texview check change the backgound of last clicked view to deafault color
textview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v != lastclickedview) {
v.setBackground("new color");
lastclickedview.setBackground("default");
}
//your Action
lastclickedview = v;
}
});

How to obtain and pass the value of a button

I want to know how I can obtain the value of a button and then use it on another activity to display content accordingly.
Example:
I have 3 buttons Image1, Image2 & Image3 on my MainActivity.
Now based on what button the user clicks (Image1, Image2 & Image3), a corresponding image is displayed on a new activity.
I know how to create these buttons, the new activity and also how to display an image on the new activity. How do I display the image based on what button the user clicks?
Your class should implement OnClickListener and override onClick()
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new OnClickListener(this));
button2.setOnClickListener(new OnClickListener(this));
button3.setOnClickListener(new OnClickListener(this));
#Override
public void onClick(View v){
switch(v.getId()) //get the id which is an int
{
case R.id.button1 : //if its button1 that is clicked
Intent i= new Intent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
// use intents to pass information to secondActivity and display the image there
break;
case R.id.button2 :
Intent i= new Intent("com.example.secondActivity");
startActivity(i)
//use intents to pass information to secondActivity and display the image there
break;
case R.id.button3 :
Intent i= new Intent("com.example.secondActivityy");
startActivity(i)
//use intents to pass information to secondActivity and display the image there
break;
}
}
To pass values using intents
On Button click
Intent i= new Intent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
In Second Activity retrieve it as below
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
int values= extras.getInt("key");
}
Why not use a switch case to determine which button was clicked by the user and show the corresponding / relevant image in the next activity? For example:
First, make your Activity implement the OnClickListener. Then, in the onCreate() cast your Buttons and set their setOnClickListener
#Override
public void onCreate(Bundle savedInstanceState) {
....
Button Image1 = (Button) findViewById(R.id.Image1);
Image1.setOnClickListener(this);
.... // THE REST OF THE BUTTONS
}
I am assuming you are passing a Bundle in the Intent for starting the next Activity. Change that code to pass information that contains which button was pressed.
For example:
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE1");
startActivity(showPhoto);
#Override
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Image1:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE1");
startActivity(showPhoto);
break;
case R.id.Image2:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE2");
startActivity(showPhoto);
break;
case R.id.Image3:
// RUN THE CODE TO START THE NEXT ACTIVITY
Intent showPhoto = new Intent(CurrentActivity.this, YOUR_SECOND_ACTIVITY.class);
showPhoto.putExtra("BUTTON_CLICKED", "IMAGE3");
startActivity(showPhoto);
break;
}
}
}
I have 3 buttons Image1, Image2 & Image3 on my MainActivity. Now based
on what button the user clicks (Image1, Image2 & Image3), a
corresponding image is displayed on a new activity.
=> As you already said yo know how to create buttons and initiate it.
Now you just need to assign OnClickListener to every buttons and then pass Image id or URL into the Intent by which you are calling new activity.
Check the code posted by #IceMAN above, now as I mentioned above, put ImageURL or Image ID into Intent by using putExtra() method.
For example:
Intent intent= new Intent(CurrentClass.this, ImageActivity.class);
intent.putExtra("ImageURL",strImageURL);
startActivity(i);
For example you have 3 Buttons and ImageView in Main layout:
Main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/btn1"
android:text="button 1"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/btn2"
android:text="button 2"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/btn3"
android:text="button 3"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/iv"
/>
I your activity metod onClick handle events from three buttons. And we need to recognize that the button has been pressed.
MyActivity.java
public class MyActivity extends Activity implements View.OnClickListener {
Button btn1;
Button btn2;
Button btn3;
ImageView iv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn1);
btn3 = (Button) findViewById(R.id.btn1);
iv = (ImageView) findViewById(R.id.iv);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
{
Toast toast = Toast.makeText(this, "onClickButton1", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_1);
break;
}
case R.id.btn2:
{
Toast toast = Toast.makeText(this, "onClickButton2", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_2);
break;
}
case R.id.btn3:
{
Toast toast = Toast.makeText(this, "onClickButton3", Toast.LENGTH_SHORT);
toast.show();
iv.setImageDrawable(R.drawable.my_image_3);
break;
}
}
}
}
Where my_image_1, my_image_2, my_image_3 - images, from your Drawable folder.
Hope its Help.
Add this in onCreate of your activity that contains the 3 buttons
Button image1 = (Button) findViewById(R.id.image1);
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image1"));
}
});
Button image2 = (Button) findViewById(R.id.image2);
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image2"));
}
});
Button image3 = (Button) findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), DisplayImagActivity.class)
.putExtra("ImageName", "Image3"));
}
});
Add this in onCreate of your activity where you will set the image:
String imageName = getIntent().getStringExtra("ImageName");
if(imageName.equals("Image1")) {
//set image 1
}
else if(imageName.equals("Image2")) {
//set image 2
}
else if(imageName.equals("Image3")) {
//set image 3
}
You simply implement one ClickListener for each button, which will start the corresponding activity. See an example here.
I know this is and old question, but there is another way, which I prefer, because you can use the same default method for all buttons.
First add a tag to each button.
<Button
android:id="#+id/imageOneButton"
android:tag="1"
android:onClick="chooseImage" />
Then use this tag number to alter the behavior of your method.
public void chooseImage(View view) {
int imageNumber = Integer.parseInt(view.getTag().toString());
// I'll just send this tag number to a toast, but you can send it in your intent
// as an "Extra"
Toast.makeText(this, view.getTag().toString(), Toast.LENGTH_SHORT).show();
}

Categories

Resources