1. You want that $100.00 in your pocket or an Amazon Fire TV don't you? Well here's the deal. With our new updated look we are in desperate need of an updated logo. The 'old' one has certainly served us well, but it's time. Find all the details here: bit.ly/1q0k6Wa
  2. DroidForums.net is currently undergoing a major software upgrade. If you are experiencing any problems logging in please: Contact Us

Items not showing on layout?

Discussion in 'Android App Developers' started by kraxyk, Nov 2, 2010.

  1. kraxyk
    Offline

    kraxyk New Member

    Joined:
    Feb 8, 2010
    Messages:
    74
    Likes Received:
    0
    Trophy Points:
    0
    Hello I am using this code and for some reason the button doesn't show up. Why? Also I tried switching the setcontentView(); to the button. But when that happens the text doesn't show up. How do I show both?

    Code:
    package com.kmccmk9.CarVoice;
    
    import java.util.Locale;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.widget.Button;
    import android.widget.TextView;
    import android.os.Bundle;
    import android.speech.tts.*;
    import android.speech.tts.TextToSpeech.OnInitListener;
    
    public class CarVoice extends Activity implements OnInitListener {
        /** Called when the activity is first created. */
       private TextToSpeech tts;
       
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TextView lbl_tv = new TextView(this);
            lbl_tv.setText("Welcome to Car Voice. This app was designed for you to never have to use your hand for your phone while you are in the car.");
            lbl_tv.setTextSize(28);
            setContentView(lbl_tv);
            Button cmd_Button = new Button(this);
            cmd_Button.setText("Command");
            AlertDialog.Builder safetybuilder = new AlertDialog.Builder(this);
            safetybuilder.setMessage("Do not use this app while the vehicle is in motion. Doing so can seriously injur yourself or someone else. We are not responsible for anything that may happen. Please use this at your own risk.");
            safetybuilder.setCancelable(false);
            safetybuilder.setPositiveButton("Agree", new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
            });
            
            safetybuilder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    System.exit(0);
                }
            });
            AlertDialog safety = safetybuilder.create();
            safety.show();
            
            Intent checkIntent = new Intent();
            checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
            startActivityForResult(checkIntent, 0);
            tts = new TextToSpeech(this, this);
    
        }   
        
        public void onInit(int arg0) {
            // TODO Auto-generated method stub
            String speech1 = "Do not use this app while the vehicle is in motion, Doing so can seriously injur yourself or someone else, We are not responsible for anything that may happen, Please use this at your own risk.";
            String speech2 = "To use this app, Touch the command button, listen to the prompt and say your response, You can say things like, text, phone, or navigation.";
            tts.setLanguage(Locale.US);
            tts.speak(speech1, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(speech2, TextToSpeech.QUEUE_ADD, null);
        }
    
    
    
    }
    
  2. WrightPC
    Offline

    WrightPC New Member

    Joined:
    Aug 28, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Check out the RelativeLayout tutorial. Most likely you aren't defining where to draw the widgets in /res/layout/main.xml

    Sent from my DROID2 GLOBAL using DroidForums App
  3. alostpacket
    Offline

    alostpacket New Member

    Joined:
    Dec 7, 2009
    Messages:
    68
    Likes Received:
    0
    Trophy Points:
    0
  4. KoDA
    Offline

    KoDA New Member

    Joined:
    Feb 28, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Looking at your code I see some mistakes.
    I may recomend you to read manuals, but let me first answer your question :).
    So when you invoke setContentView(lbl_tv) you just replace R.layout.main. Is it really what you need? To show both TextView and Button you should write something like this:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // for example you have LinearLayout with android:id="@+id/mainLayout" in your main.xml layout
    LinearLayout layout = (
    LinearLayout) findViewById(R.id.mainLayout);
    TextView lbl_tv = new TextView(this);
    lbl_tv.setText("Welcome to Car Voice. This app was designed for you to never have to use your hand for your phone while you are in the car.");
    lbl_tv.setTextSize(28);
    layout.addView(lbl_tv);

    Button cmd_Button = new Button(this);
    cmd_Button.setText("Command");
    layout.addView(cmd_Button);


    Now it should work. Please note that you must invoke setContentView(...) before using findViewById(...). Also for simple layouts it's better to define them using XML.
Search tags for this page
addview not visiable
,
android a button not show up
,
android addview not showing
,
android button in layout isn't showing up
,
android developer tools main activity not showing up
,
android layout addview not show
,
android linearlayout addview does not show up
,

android onlayout example

,

android textview not showing up

,
button doesn't show up android
,
items doesnt show up in layout android
,
layout doesn't show in r.layout
,
linearlayout addview not visible
,
r.id button not showig up android
,
why does android emulator not show my textview