Android: Why TextField is not showing? - java

TextView t = new TextView(this);
t.setX(0);
t.setY(0);
t.setText("Tap");
t.setTextColor(Color.GREEN);
t.setTextSize(25);
t.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
setContentView(t);
I'am in ActionBarActivity, that why I'am using "this" as context, can't understand why the textField isn't showing, is here something wrong?

Try this one
TextView t= new TextView(this);
t.setX(0);
t.setY(0);
t.setText("Tap");
t.setTextColor(Color.GREEN);
t.setTextSize(25);
addContentView(t, new FrameLayout.LayoutParams(100, 100));

Create xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_layout"
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="in.appkart.flashlight.MainActivity" >
</FrameLayout>
and MainActivity
package in.appkart.flashlight;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View frameLayout = findViewById(R.id.frame_layout);
TextView t = new TextView(this);
t.setX(0);
t.setY(0);
t.setText("Tap");
t.setTextColor(Color.GREEN);
t.setTextSize(25);
t.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
t.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
((FrameLayout) frameLayout).addView(t);
}
}

Related

List not displaying in android studio

Main Activity layout
<?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="wrap_content"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="#color/white"
android:background="#color/colorPrimary"
android:id="#+id/ToolbarMain"
tools:targetApi="honeycomb">
</android.support.v7.widget.Toolbar>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:dividerHeight="10dp"
android:divider="#null"
android:layout_below="#+id/ToolbarMain">
</ListView>
</RelativeLayout>
main activity.java
package com.example.admin.ttabledemo;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupIViews();
iniToolbar();
}
private void setupIViews(){
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.ToolbarMain);
listview = (ListView)findViewById(R.id.lvMain);
}
private void iniToolbar(){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("TIMETABLE APP");
}
I am trying to build a time table app. List is not displaying. Just the titlebar.
Although in design listview is visible. please help.
Nothing is showing on the screen. I can't figure out what is wrong.
This is my first attempt to build an app. I am a complete beginner.
Use setAdapter() like this way.
String data[] = new String[]{"Most Popular", "UpComing", "Top Rated"};
ListView listview = (ListView) findViewById(R.id.lvMain);
YourAdapterName cma = new YourAdapterName(this, android.R.layout.simple_list_item_1, data);
listview .setAdapter(cma);
You have to add something to show. Try the following code:
String items[]={"one","two","three","four","five"};
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listview.setAdapter(arrayAdapter);

My app is crashing because of the "addView": the error on it is: Method invocation may produce nullPointException

// *** See the error marked below ***
package com.example.android.miwok;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ArrayList<String> words = new ArrayList<String>();
words.add(0,"One");
words.add(1, "Two");
words.add(2, "Three");
words.add(3,"Four");
words.add(4,"Five");
words.add(5,"Six");
words.add(6,"Seven");
words.add(7,"Eight");
words.add(8,"Nine");
words.add(9,"Ten");
LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
TextView wordsView = new TextView(this);
wordsView.setText(words.get(0));
// ***
// Here is the addView method that's not working:
// ***
rootView.addView(wordsView);
}
}
I don't know why this crash is happening. When I want to test my app, it crashes and says that it has stopped and when I remove the code from "calling the linear layout" to "after the "rootView.addView(wordsView);" "
I just tested your code:
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> words = new ArrayList<String>();
words.add(0,"One");
words.add(1,"Two");
words.add(2,"Three");
words.add(3,"Four");
words.add(4,"Five");
words.add(5,"Six");
words.add(6,"Seven");
words.add(7,"Eight");
words.add(8,"Nine");
words.add(9,"Ten");
LinearLayout rootView = findViewById(R.id.layout);
TextView textView = new TextView(this);
textView.setText(words.get(0));
rootView.addView(textView);
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/layout"
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</android.support.constraint.ConstraintLayout>
As you can see the code works fine for me. Also you should provide your XML.

Programmatically create scroll view in android studio with buttons.!

I am trying to make an app with this look enter image description here
but programmatically such that I can change the number of shown boxes in runtime.
I have tried the following, but no success-
1)MainActivity.java
package com.bigx.dynamictesting;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.activity_relative);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for(int i = 0; i < 4; i++)
{
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(300 * getResources().getDisplayMetrics().density));
layoutParams.setMargins(0, 0, 0, (int)(20 * getResources().getDisplayMetrics().density));
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
textView.setText("TextView" + i);
linearLayout.addView(textView);
Button buttonA = new Button(this);
Button buttonB = new Button(this);
RelativeLayout.LayoutParams layoutParamsA = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_END, textView.getId());
RelativeLayout.LayoutParams layoutParamsB = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_LEFT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_START, textView.getId());
buttonA.setLayoutParams(layoutParamsA);
buttonA.setText(String.valueOf(i));
linearLayout.addView(buttonA);
buttonB.setLayoutParams(layoutParamsB);
buttonB.setText(String.valueOf(i));
linearLayout.addView(buttonB);
}
relativeLayout.addView(linearLayout);
}
2) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:fillViewport="false"
android:background="#color/colorAccent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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.bigx.dynamictesting">
</RelativeLayout>
</ScrollView>
Which yields this result-enter image description here
Kindly suggest me how to achieve my objective.
Thank you.
P.S.-Sorry for my bad English.

in this program passing ArrayList from one activity to another not working

first activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button bt1;
private Button bt2;
private EditText ed1;
private EditText ed2;
private TextView tv3;
static ArrayList<String> s = new ArrayList<>();
static ArrayList<Integer> i = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
bt2 = (Button)findViewById(R.id.button2);
tv3 = (TextView)findViewById(R.id.textView3);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
s.add(ed1.getText().toString());
i.add(Integer.parseInt(ed2.getText().toString()));
}
});
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
Intent intent = new Intent(MainActivity.this, second.class);
intent.putExtra("key", s);
startActivity(intent);
}
catch(Exception e) {
tv3.setText(e.getMessage());
}
}
});
}
}
Second activity:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.widget.TextView;
import java.util.ArrayList;
public class second extends AppCompatActivity {
private TextView tv1;
private TextView tv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv1 = (TextView)findViewById(R.id.textView);
tv2 = (TextView)findViewById(R.id.textView2);
ArrayList<String> s = (ArrayList<String>)getIntent().getSerializableExtra("key");
for(int j=0;j<=s.size();j++) {
tv1.setText(s.get(j));
}
}
}
I don't understand the problem in this code. when I click on bt2 to pass the ArrayList from on activity to another then the app just shutdown. I am not able to understand the problem in this code.
Please help me,
I have also updated the manifest.xml for second class.
Are you sure
i.add(Integer.parseInt(ed2.getText().toString()));
in the onClick() call doesnt throw an Exception?
Use a try-catch block around your code in Listener.onClick().
Btw., you can easily use Lambda Exrpessions for ActionListeners.
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Please use following construct:-
While Sending:
intent.putStringArrayListExtra("key", s)
While Receiving:
ArrayList<String> s = getIntent().getStringArrayListExtra("key");
Replace your for loop with below, use j< s.size and not j<=s.size
for(int j=0;j<s.size();j++)
{
tv1.setText(s.get(j));
}
You can do like this :
Bundle info= new Bundle();
ArrayList<Product> mas = new ArrayList<Product>();
info.putSerializable("product", mas);
intent.putExtras(info);
And get the values with this:
ArrayList<Product> prod = getIntent().getSerializableExtra(key);
Product class like a serializable object
private class Product implements Serializable {
}
Here it is just how you can pass arraylist between activities.Hope you wil get an idea to solve your issues.
intent.putExtra("key", s);-- this is wrong, you are sending a string but not your arrayList
Try sending with-- intent.putStringArrayListExtra("key", s);
and receiving -- ArrayList<String> stringArray = getIntent().getStringArrayListExtra("key");
hope this will solve your problem...
In your second activity onCreate, add the following code:
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// Add view
// Must add check extras if null.
Bundle extras = getIntent().getExtras();
ArrayList<String> s = extras.getStringArrayListExtra("key");
// do something
}
In your first activity, send intent with:
Intent intent = new Intent(MainActivity.this, second.class);
intent.putStringArrayListExtra("key", s);
startActivity(intent);
I've tried an sample app with your code. Hope this will help you.
Kindly check my codes.
First Activity
package com.arindam.testapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class FirstActivity extends AppCompatActivity {
Button bt1;
EditText ed1;
EditText ed2;
TextView tv3;
ArrayList<String> s = new ArrayList<>();
ArrayList<String> i = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
bt1 = (Button)findViewById(R.id.button);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
tv3 = (TextView)findViewById(R.id.textView);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
s.add(ed1.getText().toString());
i.add(ed2.getText().toString());
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("key", s);
intent.putExtra("value", i);
startActivity(intent);
}
catch(Exception e)
{
tv3.setText(e.getMessage());
}
}
});
}
}
First Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#679667"
android:orientation="vertical"
tools:context="com.arindam.testapp.FirstActivity">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
Second Activity
package com.arindam.testapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.ArrayList;
public class SecondActivity extends AppCompatActivity {
private TextView tv1;
private TextView tv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv1 = (TextView)findViewById(R.id.textView);
tv2 = (TextView)findViewById(R.id.textView2);
ArrayList<String> s = getIntent().getStringArrayListExtra("key");
for(int j=0;j<s.size();j++)
{
tv1.setText(s.get(j));
}
ArrayList<String> i = getIntent().getStringArrayListExtra("value");
for(int k=0;k<i.size();k++)
{
tv2.setText(i.get(k));
}
}
}
Second Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#229597"
android:orientation="vertical"
tools:context="com.arindam.testapp.SecondActivity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>
If you want to work with key value pairs, go for hashMap.

Unable to dynamically add TextView when java class extends AppCompatActivity

I am trying to add a TextView dynamically to my activity, but when i run the app it is not showing up. There are no errors, and the app does not crash, but the TextView does not show up. My java class extends AppCompatActivty, and i noticed that when i change it to extending Activity my code works, and the TextView shows up. My question is, why does the following code work for Activity but not AppCompatActivity?
My java class FormActivty.java
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FormActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_form);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);
TextView textView = new TextView(this);
textView.setText("Hello world");
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
}
My XML activity_form.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/formLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
I am not sure why this is the case, but if i remove PersistableBundle persistentState from the onCreate method, then it works. my java class now looks like
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FormActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);
TextView textView = new TextView(this);
textView.setText("Hello world");
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
}

Categories

Resources