Place an Imageview under a Texview after passing intent - java

I need to put an ImageView under a TextView that was constructed using java. The textview is displaying information from a previous activity. Here is my code:
package com.example.a_simple_ui;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
So now I need a picture under the textview above and to change the background color. Either by java or XML. Thank you.

You can use any approach from bellow two suggestion:
1) If your layout design is fixed than it is better to use xml based layout (static layout) rather than adding layout run-time.
For that first create xml layout main_Activity.xml like:
main_Activity.xml
<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:orientation="vertical" >
<TextView
android:id="#+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Imageview
android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher" />
</LinearLayout>
MainActivity2 .java
package com.example.a_simple_ui; public class MainActivity2 extends
Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(textView); Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView =(TextView)findViewById(R.id.tvDesc);
textView.setTextSize(40);
textView.setText(message); } }
2) You need to add TextView and Imageview in linearlayout then need to set that linearlayout in setContentView() like:
package com.example.a_simple_ui;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setImageResource(R.drawable.ic_launcher);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textview);
layout.addView(imageView);
setContentView(layout);
}
}

Ok, actually I can't understand, why you use TextView class as you content view. In that case, you can, offcourse, make you own textview extending default one and create particular layout with imageview for it, but, the easiest (and more logical) way is to create lauout file for your activity with TextView and ImageView.
For example, it can be something like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
And your activity onCreate method will be looks something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(textView);
Intent recieve = getIntent();
String message = recieve.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setTextSize(40);
textView.setText(message);
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
}
You need to call setContentView before finding views by id.
If you have to create texview dynamic, from java (I can't see any reason for it from your code) you can add id property to LinearLayout and than find it by id and add texview to that, and it will be placed after imageview.
p.s. Actually there are a lot of ways to do that, please, define your question more preciselly if my answer doesn't suits you
p.p.s. If you really have to create textView in runtime and you can't use xml layouts, you can create LinearLayout in runtime (LinearLayout layout = new LinearLayout(this) and after that, create textView and ImageView and add that two views to that layout, after that make set this layout as content view for the activity

Related

How can I target layout id from Java inflate

I am new to Android programming...
I want to programmatically add layouts to a ScrollView, so I use this code below
for(int i = 0; i < 10; i++){
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = (ImageView) getLayoutInflater().inflate(R.layout.img_view, null);
//set ImageView source
TextView txtView = (TextView) getLayoutInflater().inflate(R.layout.txt_view, null);
//set TextView text
myLayout.addView(imgView);
myLayout.addView(txtView);
scrllView.addView(myLayout);
}
The above code works fine. But I want to know if there is a shorter and easier way to do this, because my code will be bulky, and I will have to create a layout XML file for every view I want to add
Maybe I am thinking instead of creating multiple XML file in my layout folder, I can just have one XML file, having LinearLayout as the parent with ImageView and TextView as children, so my_layout.xml will look like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/img"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txt"/>
</LinearLayout>
Then I can inflate my_layout.xml file in my Java code add target the ImageView and TextView using their IDs
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = //find the child of myLayout (ImageView) with ID of img
//set ImageView source
TextView txtView = //find the child of myLayout (TextView) with ID of txt
//set TextView text
I want to know if it's possible to achieve this.
Okay... I finally got what I am looking for. The code below worked perfectly
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = myLayout.findViewById(R.id.img);
//set ImageView source
TextView txtView = myLayout.findViewById(R.id.txt);
//set TextView text

Use stylized action bar in every activity

I wanted to use a custom color and font and size for my app's name in the action bar, so I went in and stylized it in MainActivity.java like so
//stylize the action bar
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(R.string.Title);
tv.setTextSize(45);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "KGALittleSwag.ttf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);
updateOptionsMenu();
This did the trick, and then when I added another activity I copied this code into it, and it looked great. At this point my app is almost ready to release, but I'm doing some refactoring and I now have 6 or so activities, and it feels more than a little redundant to put this code in each of them. Is there a better practice for applying these changes to the action bar universally?
This is what inheritance is for.
Create an abstract BaseActivity Class, in which you'll fo all these processing. All your activities where you want to apply these styles will inherit for BaseActivity.
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
//stylize the action bar
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(R.string.Title);
tv.setTextSize(45);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "KGALittleSwag.ttf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);
updateOptionsMenu();
}
}
Then your children activities:
public abstract class ChildActivity extends BaseActivity {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState); // Here it calls the parent onCreate method and therefore executes the styling code
}
}
What you can do is just make a XML layout file for your custom action bar, and than use that in all activities.
<?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"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/actionBarTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:maxLines="1"
android:text="your text"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
make sure that your TextView is centered, and that layout_width and layout_hight for your TextView is set to wrap_content
Than put this into every activity that you want to use custom action bar
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);

Get users text and display it in a text view in a different class [duplicate]

I have an Activity and associated with it layout. Also I have another layout with some Views. I want to initialize a variable (TextView) from my Activity using a View from that standalone layout. I always get null.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button); // This is OK
// because R.id.button is from R.layout.main layout
tvOne = (TextView) findViewById(R.id.first_item); // This is not OK
// because R.id.first_item is from another layout.
}
You can't instantiate a view within another layout, but you can instantiate a layout which can be the view you want.
LayoutInflater inflater = getLayoutInflater();
TextView text = (TextView) inflater.inflate(R.layout.contenttext, yourRootView, false);
Here would be the corresponding layout (contenttext.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
This is the correct behaviour, because findViewById() will only search for views set in the view hierarchy passed to setContentView()

Android add button to fragmentView from activity

I've created a simplified example of the problem i've encountered. What i want is add a button to my fragment view when clicking on another button in the fragmentview.
Java
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
}
public void addSubmitButton(View view){
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.activityLayout);
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lparams);
rLayout.addView(tv1);
}
XML ActivityMain
<fragment
android:id="#+id/fragment"
tools:layout="#layout/fragment_main" />
XML fragment_main
<RelativeLayout
android:id="#+id/activityLayout"
tools:context=".MainActivityFragment">
<Button
android:id="#+id/button"
android:onClick="addSubmitButton" />
</RelativeLayout>
In the codes above I've removed not relevant code (like width height etc)
When i click the button onclick fires addSubmitButton, then the app crashes on a nullpointer for the line rLayout.addView(tv1); in addSubmitButton.
why is it null?
I think, this is not the correct way.
First, add the proper definition in your fragment view declaration in your activity's layout
android:name="com.example.android.fragments.MainActivityFragment"
You'd have to use a Fragment Manager to find the fragment populated into your activity. Once you have this fragment, you can get its view, in your case the RelativeLayout and then add the desired Button to this view.
The following might help:
FragmentManager fm = getFragmentManager();
fm.findFragmentById(R.id.yourFragmentId).getView().findViewById(R.id.activityLayout)
If you are using the support fragment use the getSupportFragmentManager()
Looks like you are missing the fully qualified name in your fragment element:
<fragment android:name="com.example.android.fragments.MyFragment"
android:id="#+id/fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />

Android, set text for an EditText on startup

So I have a simple android app where I take text from a text file (that part is working) and I want to set that text to EditText.setText(). The problem I'm having is that I cannot do this in the onCreate() method because the EditText field and the UI hasn't been created yet(from what it seems).
My question is where would be a good place to do this? Is there a function that is called right after the GUI elements are created?
public class MainActivity extends ActionBarActivity
{
private final String FILE_NAME = "awayReply";
private EditText myEditMessage;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
protected void onStart()
{
String message = readMessage();
myEditMessage = (EditText)findViewById(R.id.edit_message);
if(myEditMessage != null)
{
myEditMessage.setText(message, TextView.BufferType.EDITABLE);
}
}
XML Layout:
<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:orientation="horizontal"
android:paddingBottom="16dp"
android:paddingTop="16dp" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/lbl_message"
android:inputType="textImeMultiLine" />
<Button
android:id="#+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:onClick="updateMessage"
android:text="#string/lbl_update" />
<ToggleButton
android:id="#+id/toggle_away"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/lbl_enable"
android:layout_alignBottom="#+id/lbl_enable"
android:layout_toRightOf="#+id/edit_message"
android:textOff="Disabled"
android:textOn="Enabled"
android:onClick="toggleEnabled" />
</RelativeLayout>
You can't set your text in a TextView until you inflate your layout with
setContentView(R.layout.main_activity);
in the Activity's OnCreate
Once that happens you can do:
EditText editText = (EditText)findViewById(R.id.myEditText)
Full Example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // this needs to happen first
EditText editText = (EditText)findViewById(R.id.myEditText)
editText.setText("my text", TextView.BufferType.EDITABLE); // <-- needs that second parameter
}
Edit: updated for EditText instead of TextView
I think I figured out my problem. I need to do this work in the PlaceholderFragment where onCreateView is defined. When I call findviewbyid here it dosen't return null.

Categories