Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26654] Hyperloop Android: How to get access the views in the inflate layout

GitHub Issuen/a
TypeStory
PriorityNone
StatusClosed
ResolutionFixed
Resolution Date2019-05-01T23:04:25.000+0000
Affected Version/sRelease 7.5.0
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterAminul Islam
AssigneeGary Mathews
Created2018-12-14T12:47:59.000+0000
Updated2019-05-01T23:04:42.000+0000

Description

Hello! How to get access the views in the inflate layout on android hyperloop? We have a button in inflate layout and we want to set button text form our js file. Test code :
var window = Titanium.UI.createWindow();

var OnValueChangeListener = require('android.widget.NumberPicker.OnValueChangeListener');
var OnClickListener = require('android.view.View.OnClickListener');
var Activity = require('android.app.Activity');
var Button = require('android.widget.Button');
var Context = require('android.content.Context');
var LinearLayout = require("android.widget.LinearLayout");
var Inflater = require('android.view.LayoutInflater');
// var numberOnclickListener =
// require('android.content.NumberPicker.OnClickListener');

var activity = new Activity(Ti.Android.currentActivity);
var layout = new LinearLayout(activity);

var viewLayout = new LinearLayout(activity);

var okButton = new Button(activity);
// var field = new Field();
var cancelButton = new Button(activity);

var inflater = Inflater.cast(activity.getApplicationContext().getSystemService(
		Context.LAYOUT_INFLATER_SERVICE));

var resId = activity.getResources().getIdentifier('button', 'layout',
		activity.getPackageName());
var view = inflater.inflate(resId, null);
// numberPicker1 = activity.getResources().getIdentifier("button", "id",
// activity.getPackageName());
// numberPicker1.setMinValue(1);
// numberPicker1.setMaxValue(9);
var resid = Titanium.App.Android.R.id.cancelButtton;
// var id =
// Ti.Android.currentActivity.getIdentifier(Ti.Android.R.cancelButtton);
var myButton = new Button(view.findViewById(resid));
myButton.setText("Hello!");
window.add(view);

window.open();
Place it to the [app]/platform/android/res/layout/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical" android:gravity="center_horizontal" android:id="@+id/parent_view">
	<LinearLayout android:id="@+id/numberPickerViews" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal">
		<NumberPicker android:id="@+id/numberPicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="visible" />
		<NumberPicker android:id="@+id/numberPicker2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="visible" />
		<NumberPicker android:id="@+id/numberPicker3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="visible" />
	</LinearLayout>
	<LinearLayout android:id="@+id/timeSetView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal">
		<Button android:id="@+id/setButtton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_blue_light" android:text="OK" android:textAlignment="center_horizontal" android:gravity="left" android:textStyle="bold" android:textAlignment="center" />
		<Button android:id="@+id/cancelButtton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_blue_light" android:textStyle="bold" android:gravity="right" android:textAlignment="center" />
	</LinearLayout>
</LinearLayout>
Test Environment:
Operating System
  Name                        = Microsoft Windows 10 Pro
  Version                     = 10.0.17134
  Architecture                = 32bit
  # CPUs                      = 4
  Memory                      = 17091956736
Node.js
  Node.js Version             = 8.9.1
  npm Version                 = 5.5.1
Titanium CLI
  CLI Version                 = 5.1.1
Titanium SDK
  SDK Version                 = 7.5.0.GA
Thanks

Comments

  1. Gary Mathews 2018-12-14

       const window = Titanium.UI.createWindow();
       
       const Activity = require('android.app.Activity');
       const Button = require('android.widget.Button');
       const Context = require('android.content.Context');
       const Inflater = require('android.view.LayoutInflater');
       
       const activity = new Activity(Ti.Android.currentActivity);
       
       const inflater = Inflater.cast(activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
       const layoutId = activity.getResources().getIdentifier('test', 'layout', activity.getPackageName());
       console.log('### test.xml layout resourceId: ' + layoutId);
       
       const view = inflater.inflate(layoutId, null);
       console.log('### inflated layout view: ' + view);
       
       const cancelButttonId = Titanium.App.Android.R.id.cancelButtton;
       console.log('### cancelButtton resourceId: ' + cancelButttonId);
       
       const cancelButttonView = view.findViewById(cancelButttonId);
       console.log('### cancelButtton view: ' + cancelButttonView);
       
       const cancelButtton = Button.cast(cancelButttonView);
       cancelButtton.setText('Hello!');
       
       window.add(view);
       window.open();
       
  2. Motiur Rahman 2018-12-15

    Thanks for your code. It's working for me.

JSON Source