is it possible to start a context menu on the onCreate method? I know its probably bad design ethics but I have my resons!! I've tried the:
registerForContextMenu(this.getCurrentFocus());
But its not working.. So does anyone have any better ideas?
Many thanks in advance!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//Button button = new Button(this);
//button.setLayoutParams(new
//LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//button.setText("my button");
TextView text = new TextView(this);
text.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(text);
setContentView(layout);
registerForContextMenu(text);
openContextMenu(layout);
You need to do registerForContextMenu() for some widget, then use openContextMenu(). However, I agree with your conclusion that this is a bad design.
Related
I want to create multiple TextViews inside a LinearLayout.The following code builds successfully but gives a NullPointerException at the line root.addView(t[i]);
public class MainActivity extends ActionBarActivity {
TextView t[];
LinearLayout root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
root=(LinearLayout)findViewById(R.id.master);
t=new TextView[10];
LinearLayout.LayoutParams dim=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<10;i++)
{
t[i]=new TextView(this);
t[i].setLayoutParams(dim);
t[i].setText("YOHOHO: "+i);
root.addView(t[i]);
}
setContentView(root);
}
This really has no aim Iam just trying to learn things!
It's giving NPE because you are not setting your activity layout properly.
Do this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whereLinearLayoutMasterIs); // Add your layout here
root=(LinearLayout)findViewById(R.id.master);
t=new TextView[10];
LinearLayout.LayoutParams dim=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<10;i++)
{
t[i]=new TextView(this);
t[i].setLayoutParams(dim);
t[i].setText("YOHOHO: "+i);
root.addView(t[i]);
}
}
NOTER.layout.whereLinearLayoutMasterIs is indicative, use your layout in which R.id.master is
The problem is that root is null - this is because you've not yet set your Activity's content view via setContentView.
You need to do something like this:
super.onCreate(...);
setContentView(R.layout.yourLayoutName);
root=(LinearLayout)findViewById(R.id.master);
I want to dynamically set the layout. I get a question (String type) as shown below :
Enter any two colors -------- and ---------
So using this string I have to create the view. Here the '---------' must be an EditText and the rest of the string can be a TextView. The blanks, i.e, EditText must appear inline as it appears in the question string. I am new to Android any suggestions would be helpful.
use this class for creating your dynamic views.may be it will help you.
public class MainActivity extends Activity {
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(LinearLayout)findViewById(R.id.linear_layout);
LinearLayout layout=new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView text1=new TextView(this);
text1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text1.setText("Enter any two colors");
EditText edt1=new EditText(this);
edt1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
edt1.setHint("COLOR1");
TextView text2=new TextView(this);
text2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text2.setText("and");
EditText edt2=new EditText(this);
edt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
edt2.setHint("COLOR2");
layout.addView(text1);
layout.addView(edt1);
layout.addView(text2);
layout.addView(edt2);
l1.addView(layout);
}
}
I have an Android application in which i send data from one activity to another. I can do that part easily but the problem is i want to display some additional TextViews as well. So when i display the sent data using setContentView i can see only the sent data. I cannot see the additional TextViews. I want both of them to be displayed simultaneously.
I searched and tried hard to find a solution without any luck.I am attaching the code for reference.Thanks!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
TextView textViewci= new TextView(this);
textViewci.setText(message);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
When i use this code,i am unable to see anything and the activity remains blank.Actually i am using a TableLayout here so that i can display data in form of tables
You have to add text view on your layout.
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
You have to add the text view in the parentview. For example, if you have used the text view in the linear layout then add the text view in the linear layout and then use the addview(linearlayout).
Below is the example
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
I have just make a test program for you. first class code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondClass.class);
intent.putExtra("firstValue", "1");
intent.putExtra("secondValue", "2");
startActivity(intent);
}
});
Second class code :
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message1 = intent.getStringExtra("firstValue");
String message2 = intent.getStringExtra("secondValue");
RelativeLayout layout = new RelativeLayout(this);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
TextView textViewci= new TextView(this);
textViewci.setText(message1+""+message2);
Log.e("intent value is", ""+message1+""+message2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
layout.addView(textView1);
layout.addView(textView2);
addContentView(layout, lp);
Try and let me know. YOu have to make related layouts for both classes.
Hope this will help
are you getting any error messages? if yes then please post them here. and the others are right.you need to add yours views to the parent layout.
I have found out the answer by myself.It's fairly easy and there is no need here to use views in the parent layout.I modified the textViews in my xml file and got their values using findViewById.I am attaching the updated code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
textView=(TextView)findViewById(R.id.textViewci);
textView1=(TextView)findViewById(R.id.textViewmd);
textView2=(TextView)findViewById(R.id.textViewso);
textView.setText(message);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
textView.setTypeface(tf);
}
I want to add edittext dynamically to the android display. I want to make something like in the android contacts where you can dynamically add fields and remove them if you dont need them.
Thank you for your help
See Everything dynamically
TextView tv=new TextView(this);
tv.setText("TaskID");
TextView tv1=new TextView(this);
task=new EditText(this);
task.setMaxWidth(100);
task.setMinHeight(100);
tv1.setText("Task");
id=new EditText(this);
id.setMaxHeight(10);
TextView tv2=new TextView(this);
name=new EditText(this);
name.setMaxHeight(1);
tv2.setText("Name");
LinearLayout l=new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(l);
l.addView(tv);
l.addView(id);
l.addView(tv1);
l.addView(task);
l.addView(tv2);
I'm having trouble adding a button to a layout that I've created in XML. Here's what I want to achieve:
//some class
else {
startActivity(new Intent(StatisticsScreen.this, ScreenTemperature.class));
}
////
//ScreenTemperatureClass
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this is where I call another class that
//displays a nice graph
setContentView(new GraphTemperature(getApplicationContext()));
}
I want to add a Button to this new screen so that it'll appear below the graph.
I've tried creating a LinearLayout view, then create a Button and add it to this view but I just get NullPointerExceptions..
Any help would be appreciated. Thanks
EDIT#1
Here's what I've tried using that created a NullPointerException and 'force close':
Button buybutton;
LinearLayout layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphTemperature(getApplicationContext()));
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(buyButton);
}
And here's the logcat error:
ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weatherapp/com.weatherapp.ScreenTemperature}: java.lang.NullPointerException
ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(293): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(293): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(293): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
theres abviously more lines to do with this error in logcat, not sure if you want it?
EDIT#2
So i tried bhups method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphTemperature GT = new GraphTemperature(getApplicationContext());
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(GT); // line 27
layout.addView(buyButton);
setContentView(layout);
}
This method produced the same logcat error as above, NullPointerException, indicating it was something to do with line no. 27 which is the layout.addView line of code. Any ideas? Thanks again
If you just have included a layout file at the beginning of onCreate() inside setContentView and want to get this layout to add new elements programmatically try this:
ViewGroup linearLayout = (ViewGroup) findViewById(R.id.linearLayoutID);
then you can create a new Button for example and just add it:
Button bt = new Button(this);
bt.setText("A Button");
bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linerLayout.addView(bt);
This line:
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Looks for the "statsviewlayout" id in your current 'contentview'. Now you've set that here:
setContentView(new GraphTemperature(getApplicationContext()));
And i'm guessing that new "graphTemperature" does not set anything with that id.
It's a common mistake to think you can just find any view with findViewById. You can only find a view that is in the XML (or appointed by code and given an id).
The nullpointer will be thrown because the layout you're looking for isn't found, so
layout.addView(buyButton);
Throws that exception.
addition:
Now if you want to get that view from an XML, you should use an inflater:
layout = (LinearLayout) View.inflate(this, R.layout.yourXMLYouWantToLoad, null);
assuming that you have your linearlayout in a file called "yourXMLYouWantToLoad.xml"