Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:55:20+00:00 2026-05-26T02:55:20+00:00

I’ve got an imagebutton defined in an xml-file. During runtime, I want to make

  • 0

I’ve got an imagebutton defined in an xml-file. During runtime, I want to make it visible (possible with setVisibility(), so it was no problem) but I also want to center it horizontally, like the XML attribute android:layout_gravity="center_horizontal". How do you do that during runtime, in javacode, though? I couldn’t find any methods to use to set the gravity of the imagebutton programmatically.

EDIT: am using a RelativeLayout in the xml file

EDIT2: the XML code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:gravity="center_horizontal">
    <TextView android:text="Unknown" android:layout_width="wrap_content" android:id="@+id/screen_p2pcall_status_text" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_centerHorizontal="true"></TextView>

    <ImageView android:layout_width="wrap_content" android:src="@drawable/android_avatar_big" android:id="@+id/screen_p2pcall_android" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_status_icon" android:layout_centerHorizontal="true" android:layout_marginTop="36dp"></ImageView>

    <ImageView android:layout_width="wrap_content" android:src="@android:drawable/presence_invisible" android:id="@+id/screen_p2pcall_status_icon" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_status_text" android:layout_centerHorizontal="true"></ImageView>

    <TextView android:text="Unknown" android:id="@+id/screen_p2pcall_peer" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_android" android:layout_centerHorizontal="true"></TextView>

    <ImageButton android:layout_width="120dp" android:id="@+id/screen_p2pcall_hangup" android:src="@drawable/phone_hang_up_48" android:layout_height="50dp" android:layout_below="@+id/screen_p2pcall_peer" android:layout_toRightOf="@+id/screen_p2pcall_status_icon" android:layout_marginTop="18dp"></ImageButton>

    <ImageButton android:layout_width="120dp" android:id="@+id/screen_p2pcall_answer" android:src="@android:drawable/sym_action_call" android:layout_height="50dp" android:layout_alignTop="@+id/screen_p2pcall_hangup" android:layout_toLeftOf="@+id/screen_p2pcall_hangup"></ImageButton>

</RelativeLayout>

The java code, where I want to change to center the @+id/screen_p2pcall_hangup horizontally in runtime:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_p2pcall);
        final ImageButton hangup = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
        // ????
    }

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:55:20+00:00Added an answer on May 26, 2026 at 2:55 am

    When you use Relative Layout you should add rules to the layout params to center child views.
    To the relative layout params object you should add the the rule like this

    rlp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
    

    Other rules can be added according to your requirement.
    Gravity is not possible on relative layout.

    I assume this is the whole layout xml for the activity. If so, this should work,

    ImageButton hangupButton = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
    int width  = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
                 (float) 120, getResources().getDisplayMetrics()); 
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
                 (float) 50, getResources().getDisplayMetrics()); 
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(width,height);
    rlp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
    hangupButton.setLayoutParams(rlp);
    hangupButton.setVisibility(RelativeLayout.VISIBLE);
    

    Edit:Solution with Code

    I’ve edited you xml code a bit to test in my project. There are attributes used that are not usable, such as ‘orientation’. It only applies in a linearlayout. Next time when you post a question please break your xml into the next line so that we dont need to scroll left and right to read the whole thing.

    layout xml

    <?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">
        <TextView android:text="Unknown" android:layout_width="wrap_content" 
            android:id="@+id/screen_p2pcall_status_text" 
            android:textAppearance="?android:attr/textAppearanceLarge" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"/>
        <ImageView android:layout_width="wrap_content" 
            android:src="@drawable/icon" 
            android:id="@+id/screen_p2pcall_android" 
            android:layout_height="wrap_content" 
            android:layout_below="@+id/screen_p2pcall_status_icon" 
            android:layout_centerHorizontal="true" 
            android:layout_marginTop="36dp"/>
        <ImageView android:layout_width="wrap_content" 
            android:src="@android:drawable/presence_invisible" 
            android:id="@+id/screen_p2pcall_status_icon" 
            android:layout_height="wrap_content" 
            android:layout_below="@+id/screen_p2pcall_status_text"  
            android:layout_centerHorizontal="true"/>
        <TextView android:text="Unknown" android:id="@+id/screen_p2pcall_peer" 
            android:layout_width="wrap_content" 
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:layout_height="wrap_content" 
            android:layout_below="@+id/screen_p2pcall_android" 
            android:layout_centerHorizontal="true"/>
        <ImageButton android:layout_width="120dp" 
            android:id="@+id/screen_p2pcall_hangup" 
            android:src="@android:drawable/sym_call_missed" 
            android:layout_height="50dp" 
            android:layout_below="@+id/screen_p2pcall_peer" 
            android:layout_toRightOf="@+id/screen_p2pcall_status_icon" 
            android:layout_marginTop="18dp"/>
        <ImageButton android:layout_width="120dp" 
            android:id="@+id/screen_p2pcall_answer" 
            android:src="@android:drawable/sym_action_call" 
            android:layout_height="50dp" 
            android:layout_alignTop="@+id/screen_p2pcall_hangup" 
            android:layout_toLeftOf="@+id/screen_p2pcall_hangup"/>
    </RelativeLayout>
    

    Now I used this code in my onCreate method and it hides the left bottom button and horizontally centers the right bottom button

    ImageButton hangupButton = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
    int width  = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
            (float) 120, getResources().getDisplayMetrics()); 
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
            (float) 50, getResources().getDisplayMetrics()); 
    int marginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
            (float) 18, getResources().getDisplayMetrics());
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(width,height);
    rlp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
    rlp.addRule(RelativeLayout.BELOW, R.id.screen_p2pcall_peer);
    rlp.topMargin = marginTop; 
    
    hangupButton.setLayoutParams(rlp);
    ((ImageButton)findViewById(R.id.screen_p2pcall_answer))
            .setVisibility(RelativeLayout.INVISIBLE);
    

    This should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I
I'm parsing an XML file, the creators of it stuck in a bunch social
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.