0

hi programmers my big respect to all of you. im new here in android programming.

im having a problem in working my radio button with a function of gravity in my textview by aligning them from center, left and right.

My API is 23

and im following this tutorial youtube tutorial but mine is not working

package com.example.testavd;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener {
    TextView txtV;
    RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtV = (TextView) findViewById(R.id.textOut);
        rg = (RadioGroup) findViewById(R.id.rdG);
        rg.setOnCheckedChangeListener(this);

        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                txtV.setText("boom");

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch(checkedId){
        case R.id.rbCenter:
            txtV.setGravity(Gravity.CENTER);
            break;
        case R.id.rbLeft:
            txtV.setGravity(Gravity.LEFT);
            break;
        case R.id.rbRight:
            txtV.setGravity(Gravity.RIGHT);
            break;

        }
    }
}

My XML

<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"
    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="com.example.testavd.MainActivity" >

    <RadioGroup
        android:id="@+id/rdG"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
         >
        <RadioButton
        android:id="@+id/rbRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rbLeft"
        android:layout_below="@+id/rbLeft"
        android:layout_marginTop="36dp"
        android:text="Right" />

        <RadioButton
        android:id="@+id/rbLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:layout_toLeftOf="@+id/button1"
        android:text="Left" />

    <RadioButton
        android:id="@+id/rbCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignLeft="@+id/rbRight"
        android:layout_marginBottom="16dp"
        android:text="Center" />

    </RadioGroup>



    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="32dp"
        android:text="Output"
        android:textAppearance="?android:attr/textAppearanceLarge" />



</RelativeLayout>
Icer Exiomo
  • 49
  • 1
  • 9

3 Answers3

1

Use android:gravity="center_horizontal" on the RadioGroup to center the radiobuttons.Also gravity doesn't work in RelativeLayout You have to align according to parent or other widgets. If want to use gravity or layout_gravity use linearlayout.

0

Your TextView parent is a RelativeLayout, you cannot use gravity with RelativeLayout.

Try using in xml layout file

android:layout_centerHorizontal="true"

or in Activity

 case R.id.rbCenter:
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,   ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        txtV.setLayoutParams(params);
            break;
giannisf
  • 2,479
  • 1
  • 17
  • 29
0

Solution 1 :

android:gravity sets the gravity of the content of the View its used on.

you have used gravity in TextView having wrap_content width, so it will not make any diferrence as view is having width as much as content.

try it with match_parent width and you will see difference.

android:layout_width="macth_parent"

Solution 2 :

use android:layout_gravity, it sets the gravity of the View or Layout in its parent.

no matter how much content your TextView is having, it will set whole TextView in center/right/left aligned.

To make it more affective make sure following things :

  1. When you are using android:gravity use width of view as match_parent.
  2. When you are using android:layout_gravity use width of view as wrap_content.

refer this to programmatically set layout_gravity : How to set layout_gravity PROGRAMATICALLY?

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183